Module Gambler::ClientHelper
In: lib/gambler/client/client_helper.rb

Helpful methods used in Gambler::Client.

Methods

Constants

WIDTH = 50   Various headers will be centered with WIDTH.

Public Instance methods

Add an AI Player as an opponent.

[Source]

    # File lib/gambler/client/client_helper.rb, line 9
 9:     def add_ai_player
10:       @bots << setup_player(:bot => true)
11:     end

Simple debugging console.

[Source]

    # File lib/gambler/client/client_helper.rb, line 14
14:     def debug_console
15:       stop_debug = false
16:       code = Array.new
17: 
18:       @output.puts # spacer
19:       @output.puts '== DEBUG CONSOLE =='
20:       @output.puts 'Commands:'
21:       @output.puts '  /run  - Quit debugger and run the code.'
22:       @output.puts '  /show - Show the code currently in the buffer.'
23:       @output.puts '  /quit - Quit debugger without running the code.'
24: 
25:       until stop_debug do
26:         @output.print "debug:#{code.size + 1}> "
27:         line = @input.gets.chomp
28: 
29:         case line
30:         when '/run':
31:           run_code(code)
32:           stop_debug = true
33:         when '/show':
34:           show_code_buffer(code)
35:         when '/quit':
36:           stop_debug = true
37:         else # not a command, must be some Ruby code.
38:           code << line
39:         end
40:       end
41:     end

Display a menu for the Blackjack game.

[Source]

     # File lib/gambler/client/client_helper.rb, line 119
119:     def display_blackjack_menu
120:       @output.puts # spacer
121:       @output.puts '--------------------------'.center(WIDTH)
122:       @output.puts '| (B)et | (H)it | (S)tay |'.center(WIDTH)
123:       @output.puts '--------------------------'.center(WIDTH)
124:       @output.puts '----------------------------------'.center(WIDTH)
125:       @output.puts '| (D)ebug | (M)ain Menu | (Q)uit |'.center(WIDTH)
126:       @output.puts '----------------------------------'.center(WIDTH)
127:       @output.puts # spacer
128:       @output.print 'Command: '
129:     end

Client game menu.

[Source]

     # File lib/gambler/client/client_helper.rb, line 104
104:     def display_game_menu
105:       @output.puts '-----------------------'.center(WIDTH)
106:       @output.puts '|  1) Blackjack       |'.center(WIDTH)
107:       @output.puts '|  2) Stud Poker      |'.center(WIDTH)
108:       @output.puts "|  3) Texas Hold 'em  |".center(WIDTH)
109:       @output.puts '|  A) Add AI Player   |'.center(WIDTH)
110:       @output.puts '|  D) Debug Console   |'.center(WIDTH)
111:       @output.puts '|  P) Setup Player    |'.center(WIDTH)
112:       @output.puts '|  Q) Quit            |'.center(WIDTH)
113:       @output.puts '-----------------------'.center(WIDTH)
114:       @output.puts # spacer
115:       @output.print 'Command: '
116:     end

Prints all Player hands.

[Source]

    # File lib/gambler/client/client_helper.rb, line 55
55:     def display_hands
56:       @output.puts '--'
57:       @output.puts "|  Pot: $#{@game.pot}"
58:       @output.puts "| Ante: $#{@game.ante}"
59:       @output.puts '--'
60: 
61:       @output.puts @player
62:       @output.puts "#{@player.view_hand(:format => :string)}".rjust(5)
63: 
64:       bots = @game.players[1..@game.players.size]
65:       bots.each do |bot|
66:         @output.puts ''.center(WIDTH, '-')
67:         @output.puts bot
68:         hand = bot.view_hand
69:         # Hide the first card for all bots.  No cheating!
70:         public_hand = hand[1..hand.size].join(' ')
71:         # public_hand = bot.view_hand.reject do |c|
72:         #   c == bot.view_hand.first
73:         # end.join(' ')
74:         @output.puts "** #{public_hand}".rjust(5)
75:       end
76:     end

Client header.

[Source]

    # File lib/gambler/client/client_helper.rb, line 44
44:     def display_header
45:       @output.puts # spacer
46:       @output.puts ''.center(WIDTH, '=')
47:       @output.puts " Gambler v#{Gambler::VERSION} ".center(WIDTH, '=')
48:       @output.puts ''.center(WIDTH, '=')
49:       @output.puts '----------------------------------------'.center(WIDTH)
50:       @output.puts '| Feeding yet another human addiction. |'.center(WIDTH)
51:       @output.puts '----------------------------------------'.center(WIDTH)
52:     end

Pretty print the player‘s stats.

[Source]

     # File lib/gambler/client/client_helper.rb, line 79
 79:     def display_player_stats
 80:       @output.puts '- Players -'.center(WIDTH)
 81:       seperator = "\t"
 82:       stat_width = WIDTH - 5
 83:       stats     = String.new
 84:       bot_stats = String.new
 85: 
 86:       stats << "Player: #{@player.name}".ljust(WIDTH/2)
 87:       stats << seperator
 88:       stats << "Chips: #{@player.chips}"
 89:       @output.puts stats.center(stat_width)
 90: 
 91:       if @bots.empty?
 92:         @output.puts 'No AI opponents.'.center(WIDTH)
 93:       else
 94:         @bots.each do |bot|
 95:           bot_stats  = "AI: #{bot.name}".ljust(WIDTH/2)
 96:           bot_stats << seperator
 97:           bot_stats << "Chips: #{bot.chips}"
 98:           @output.puts bot_stats.center(stat_width)
 99:         end
100:       end
101:     end

Play a quick game of Blackjack.

[Source]

     # File lib/gambler/client/client_helper.rb, line 132
132:     def play_blackjack
133:       # This block creates the players array and the game instance.
134:       begin
135:         players = Array.new
136:         players << @player
137:         players << @bots
138:         players.flatten!
139:         @game = Gambler::Game::Blackjack.new(:players => players)
140:       rescue Gambler::Exceptions::InvalidPlayerSize
141:         @output.puts 'Need at least 2 players for blackjack.'
142:         until @bots.size >= 1
143:           @bots << setup_player(:bot => true)
144:         end
145:         retry
146:       end
147: 
148:       @output.puts # spacer
149:       @output.puts ''.center(WIDTH, '=')
150:       @output.puts ' Blackjack '.center(WIDTH, '=')
151:       @output.puts ''.center(WIDTH, '=')
152:       @output.puts # spacer
153: 
154:       @game.start_round!
155: 
156:       # Main game loop.
157:       loop do
158:         display_hands
159:         display_blackjack_menu
160: 
161:         choice = @input.gets.chomp
162:         case choice
163:         when /b/i: # Bet
164:           begin
165:             print 'Amount: '
166:             amount = @input.gets.chomp.to_i
167:             @game.place_bet(@player, amount)
168:           rescue Gambler::Exceptions::NotEnoughChips
169:             @output.puts "You only have $#{@player.chips} left!"
170:             retry
171:           end
172:         when /h/i: # Hit
173:           begin
174:             @game.hit(@player)
175:             player_finished = false
176:           rescue Gambler::Exceptions::PlayerBust
177:             @output.puts 'BUST!'
178:             player_finished = true
179:           rescue Gambler::Exceptions::DeckEmpty
180:             reshuffle_deck
181:             retry
182:           end
183:         when /s/i: # Stay
184:           @output.puts "Staying with #{@player.view_hand(:format => :string)}"
185:           player_finished = true
186:         # -- These options aren't related to the game. --
187:         when /d/i: debug_console
188:         when /m/i: break # Return to main menu.
189:         when /q/i: # Quit the Client.
190:           $player_quits = true
191:           break # from this loop
192:         else
193:           @output.puts 'Invalid choice, dumbass.'
194:         end # of case
195: 
196:         if player_finished
197:           play_bot_hands
198:           begin
199:             @game.finish_round!
200:             # Show the winner of the round.
201:             @output.puts ''.center(WIDTH, '=') # spacer
202:             @output.puts "The winner of that round was: #{@game.round_winner}"
203:             @game.players.each do |player|
204:               @output.puts "#{player}: #{player.view_hand(:format => :string)}"
205:             end
206:             @output.puts ''.center(WIDTH, '=') # spacer
207:           rescue Gambler::Exceptions::NoWinner
208:             @output.puts ''.center(WIDTH, '=') # spacer
209:             @output.put 'Nobody won.  Everyone fucking sucks.'.center(WIDTH)
210:             @output.puts ''.center(WIDTH, '=') # spacer
211:           end
212: 
213:           choice = nil
214:           @game.start_round!
215:         end
216:       end # of main game loop.
217:     end

Loops through the @bots and plays their hand (with some shoddy AI, mind you).

[Source]

     # File lib/gambler/client/client_helper.rb, line 220
220:     def play_bot_hands
221:       @bots.each do |bot|
222:         while @game.hand_value(bot.hand) <= 17
223:           begin
224:             @game.hit(bot)
225:             @output.puts "#{bot} signals for a hit and gets a #{bot.hand.last}"
226:           rescue Gambler::Exceptions::DeckEmpty
227:             reshuffle_deck
228:             retry
229:           rescue Gambler::Exceptions::PlayerBust
230:             @output.puts "#{bot} has busted!"
231:           end
232:         end
233:       end
234:     end

Used to reshuffle a deck after Gambler::Exceptions::DeckEmtpy is thrown.

[Source]

     # File lib/gambler/client/client_helper.rb, line 237
237:     def reshuffle_deck
238:       @output.puts ''.center(WIDTH, '-')
239:       @output.puts 'Shuffling new deck.'
240:       @output.puts ''.center(WIDTH, '-')
241:       @game.deck = Gambler::Deck.new
242:       3.times { @game.deck.shuffle! }
243:     end

Takes an array of code lines and eval‘s them.

[Source]

     # File lib/gambler/client/client_helper.rb, line 246
246:     def run_code(code)
247:       @output.puts # spacer
248:       begin
249:         @output.puts " BEGIN DEBUG ".center(WIDTH, '=')
250:         eval(code.join("\n")) # Need to join, since +code+ is an Array.
251:         @output.puts " END DEBUG ".center(WIDTH, '=')
252:       rescue Exception => error
253:         @output.puts " DEBUG FAILED ".center(WIDTH, '=')
254:         @output.puts error
255:       end
256:       @output.puts # spacer
257:     end

Setup a new Player instance.

[Source]

     # File lib/gambler/client/client_helper.rb, line 260
260:     def setup_player(options = {})
261:       default_chips = 100
262:       if options[:bot]
263:         who = 'AI player'
264:         default_name = 'Kenny'
265:       else
266:         who = 'your player'
267:         default_name = 'Human'
268:       end
269: 
270:       @output.puts "Setting up #{who}."
271:       @output.print "Name [#{default_name}]: "
272:       name = @input.gets.chomp
273:       name = default_name if name.empty?
274: 
275:       @output.print "Chips [#{default_chips}]: "
276:       chips = @input.gets.chomp.to_i
277:       chips = default_chips if chips.zero?
278: 
279:       return Gambler::Player.new(name, :chips => chips)
280:     end

Takes an array of code lines and prints them.

[Source]

     # File lib/gambler/client/client_helper.rb, line 283
283:     def show_code_buffer(code)
284:       return (@output.puts "Buffer empty.") if code.size.zero?
285:       @output.puts "== BUFFER ==\n"
286:       code.each_with_index do |buf, line_num|
287:         @output.print "#{line_num + 1}:  ".rjust(5)
288:         @output.puts buf
289:       end
290:     end

[Validate]