-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackjack_2012-09-05.rb
executable file
·353 lines (291 loc) · 8.26 KB
/
blackjack_2012-09-05.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/env ruby
def prompt(*args)
print(*args)
gets.strip
end
FACES = {
:ace => {:name => :ace, :display => 'A', :value => 1},
:two => {:name => :two, :display => '2', :value => 2},
:three => {:name => :three, :display => '3', :value => 3},
:four => {:name => :four, :display => '4', :value => 4},
:five => {:name => :five, :display => '5', :value => 5},
:six => {:name => :six, :display => '6', :value => 6},
:seven => {:name => :seven, :display => '7', :value => 7},
:eight => {:name => :eight, :display => '8', :value => 8},
:nine => {:name => :nine, :display => '9', :value => 9},
:ten => {:name => :ten, :display => '10', :value => 10},
:jack => {:name => :jack, :display => 'J', :value => 10, :rank => 11},
:queen => {:name => :queen, :display => 'Q', :value => 10, :rank => 12},
:king => {:name => :king, :display => 'K', :value => 10, :rank => 13}
}
SUITS = {
:spades => {:name => :spades, :display => "\u2660", :rank => 1},
:hearts => {:name => :hearts, :display => "\u2661", :rank => 2},
:diamonds => {:name => :diamonds, :display => "\u2662", :rank => 3},
:clubs => {:name => :clubs, :display => "\u2663", :rank => 4}
}
class Card
attr_accessor :_face, :_suit
def initialize(face, suit)
@_face = face
@_suit = suit
end
def face
@_face[:name]
end
def suit
@_suit[:name]
end
def value
@_face[:value]
end
def to_s
"#{@_face[:display]}#{@_suit[:display]}"
end
def inspect
"<Card:#{self.object_id} #{self.face} of #{self.suit}>"
end
def +(other)
if other.is_a?(Card)
self.value + other.value
elsif other.is_a?(Numeric)
self.value + other
else
raise TypeError("#{other.class} can't be coerced into a Card or Numeric")
end
end
def coerce(other)
# assume other is always a numeric value ...
# we want an error if other is anything other than Numeric
[self.value, other]
end
def <=>(other)
result = @_suit[:rank] <=> other._suit[:rank]
return result unless result == 0
self_rank = @_face[:rank] == nil ? self.value : @_face[:rank]
other_rank = other._face[:rank] == nil ? other.value : other._face[:rank]
self_rank <=> other_rank
end
end
class Deck
attr_accessor :cards
def initialize(faces=FACES, suits=SUITS)
@cards = []
faces.each do |face_type, face|
suits.each do |suit_type, suit|
@cards << Card.new(face, suit)
end
end
end
def shuffle!
@cards.shuffle!
end
def deal
@cards.shift
end
def size
@cards.length
end
def each(&block)
@cards.each(&block)
end
def sort!
@cards.sort!
end
def to_s
"<Deck:#{self.object_id} #{self.size} Cards>"
end
end
class Shoe < Deck
attr_accessor :cards
def initialize(num_decks=1, faces=FACES, suits=SUITS)
raise "A blackjack shoe must have 1, 2, 4, 6, or 8 decks" if
![1,2,4,6,8].include?(num_decks)
@cards = []
(1..num_decks).each { |n| @cards.concat( Deck.new(faces, suits).cards ) }
end
def to_s
"<Shoe:#{self.object_id} #{self.size} Cards>"
end
end
class Hand
attr_accessor :cards
def initialize
@cards = []
end
def to_s
@cards.collect { |card| card.to_s }.join(' ')
end
def inspect
"<Hand:#{self.object_id} #{@cards.inspect}>"
end
def size
@cards.length
end
def hit!(card)
@cards.push(card)
end
def include?(face)
@cards.select { |card| card.face == face }.length > 0
end
def blackjack?
@cards.length == 2 && self.include?(:ace) &&
(self.include?(:ten) || self.include?(:jack) ||
self.include?(:queen) || self.include?(:king))
end
def value
return 0 if @cards.length == 0
value = @cards.collect { |card| card.value }.inject(:+)
aces = @cards.select { |card| card.face == :ace }
while aces.length > 0 && value < 12 do
value += 10
aces.pop
end
value
end
def bust?
self.value > 21
end
end
PLAYER = {:wins => 0, :pushes => 0}
class Blackjack
attr_accessor :shoe, :house, :players
def initialize
@house = {:games => 0}
@players = {}
end
def play
puts
puts "$$ CASINO RUBY BLACKJACK $$".center(80)
puts "Practice against Casino Ruby before losing it all in Vegas".center(80)
puts
shoe_size = prompt('How many decks in the shoe? [1/2/4/6/8] ').to_i
@shoe = Shoe.new(shoe_size)
@shoe.shuffle!
num_players = prompt('How many players? [between 1 and 7] ').to_i
raise "#{num_players} is an invalid number of players" if
num_players < 1 || num_players > 7
(1..num_players).each { |i| @players[i] = PLAYER.clone }
continue = true
while continue
continue = self.next_game
end
puts "\n"
puts "+++++ The Final Tally +++++".center(80)
puts "Out of #{@house[:games]} games played:"
@players.each do |player_num, player|
puts "Player #{player_num} won #{player[:wins]} " +
"and pushed #{player[:pushes]}."
end
end
def next_game
@house[:games] += 1
continue = true
puts "\n"
puts "===== GAME #{@house[:games]} =====".center(80)
if @shoe.size <= (6 * (@players.size + 1))
puts "FINAL ROUND!".center(80)
continue = false
end
self.deal_initial_hands
# each player plays their turn
busts = 0
@players.each do |player_num, player|
self.print_status
hand = player[:hand]
while !(hand.blackjack? || hand.bust?) && hand.value < 21 do
if @shoe.size == 1
puts "Player #{player_num}: Sorry! Last card in shoe goes to HOUSE."
break
end
response = prompt("Player #{player_num}: Hit or Stand? [h/s] ").downcase
raise "'#{response}' is an invalid response" if
!(response.start_with?('h') || response.start_with?('s'))
break if response.start_with?('s')
hand.hit!(@shoe.deal)
self.print_player_status(player_num, player)
end
busts += 1 if hand.bust?
end
# the house plays, if necessary
if busts < @players.size
hand = @house[:hand]
while @shoe.size > 0 &&
!(hand.blackjack? || hand.bust?) && hand.value < 18 do
hand.hit!(@shoe.deal)
puts "HOUSE hits..."
end
end
# show results
puts "-- Game #{@house[:games]} Results --".center(80)
self.print_status
@players.each do |player_num, player|
result = "Loss!"
print "Player #{player_num}: "
if @house[:hand].blackjack?
if player[:hand].blackjack?
player[:pushes] += 1
result = "push"
end
elsif player[:hand].blackjack?
player[:wins] += 1
result = "Win!"
elsif player[:hand].bust? && @house[:hand].bust?
player[:pushes] += 1
result = "push"
else
if @house[:hand].bust?
player[:wins] += 1
result = "Win!"
elsif player[:hand].bust?
result = "Loss!"
elsif player[:hand].value == @house[:hand].value
player[:pushes] += 1
result = "push"
elsif player[:hand].value > @house[:hand].value
player[:wins] += 1
result = "Win!"
end
end
puts result
end
# another game?
if continue
response = prompt("\n\nPlay another round? (y/n) ")
if !response.downcase.start_with?('y')
continue = false
end
end
continue
end
def deal_initial_hands
@players.each { |player_num, player| player[:hand] = Hand.new }
self.deal_one_card_to_each_player
@house[:hand] = Hand.new
@house[:hand].hit!( @shoe.deal )
self.deal_one_card_to_each_player
end
def deal_one_card_to_each_player
@players.each { |player_num, player| player[:hand].hit!( @shoe.deal ) }
end
def print_status
puts "\nHOUSE: #{@house[:hand]} #{self.blackjack_or_bust(@house[:hand])}"
@players.each do |player_num, player|
self.print_player_status(player_num, player)
end
puts
end
def print_player_status(player_num, player)
puts "Player #{player_num}: #{player[:hand]} " +
"#{self.blackjack_or_bust(player[:hand])}"
end
def blackjack_or_bust(hand)
return "** Blackjack! **" if hand.blackjack?
return "** Bust! **" if hand.bust?
return ""
end
end
if __FILE__ == $0
game = Blackjack.new
game.play
end