This repository has been archived by the owner on Apr 4, 2018. It is now read-only.
forked from RubyoffRails/Episode1-Summer2012
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackjack.rb
341 lines (283 loc) · 7.19 KB
/
blackjack.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
require 'rspec'
class Card
attr_reader :suit, :value
def initialize(suit, value)
@suit = suit
@value = value
@game_done = false
end
def value
return 10 if ["J", "Q", "K"].include?(@value)
return 11 if @value == "A"
return @value
end
def to_s
"#{suit.to_s[0].capitalize}#{@value}"
end
def game_done!
@game_done = true
end
end
class DownCard < Card
def initialize(suit, value)
super(suit, value)
end
def value
if @game_done
super
else
0
end
end
def to_s
if @game_done
super
else
"XX"
end
end
end
class Deck
attr_reader :cards
def initialize
@cards = Deck.build_cards
end
def self.build_cards
cards = []
[:clubs, :diamonds, :spades, :hearts].each do |suit|
(2..10).each do |number|
cards << Card.new(suit, number)
end
["J", "Q", "K", "A"].each do |facecard|
cards << Card.new(suit, facecard)
end
end
cards.shuffle
end
end
class Hand
attr_reader :cards
def initialize
@cards = []
end
def hit!(deck)
@cards << deck.cards.shift
end
def value
cards.inject(0) {|sum, card| sum += card.value }
end
def play_as_dealer(deck)
@cards.each { |card| card.game_done! }
if value < 16
hit!(deck)
play_as_dealer(deck)
end
end
end
class DealerHand < Hand
def hit!(deck)
if @cards.length == 0
downcard = deck.cards.shift
@cards << DownCard.new(downcard.suit, downcard.value)
else
@cards << deck.cards.shift
end
end
end
class Game
attr_reader :player_hand, :dealer_hand
def initialize
@deck = Deck.new
@player_hand = Hand.new
@dealer_hand = DealerHand.new
2.times { @player_hand.hit!(@deck) }
2.times { @dealer_hand.hit!(@deck) }
end
def hit
@player_hand.hit!(@deck)
if status[:player_value] > 21 then
stand
end
end
def stand
@dealer_hand.play_as_dealer(@deck)
@winner = determine_winner(@player_hand.value, @dealer_hand.value)
end
def status
{:player_cards=> @player_hand.cards,
:player_value => @player_hand.value,
:dealer_cards => @dealer_hand.cards,
:dealer_value => @dealer_hand.value,
:winner => @winner}
end
def determine_winner(player_value, dealer_value)
return :dealer if player_value > 21
return :player if dealer_value > 21
if player_value == dealer_value
:push
elsif player_value > dealer_value
:player
else
:dealer
end
end
def inspect
status
end
end
describe Card do
it "should accept suit and value when building" do
card = Card.new(:clubs, 10)
card.suit.should eq(:clubs)
card.value.should eq(10)
end
it "should have a value of 10 for facecards" do
facecards = ["J", "Q", "K"]
facecards.each do |facecard|
card = Card.new(:hearts, facecard)
card.value.should eq(10)
end
end
it "should have a value of 4 for the 4-clubs" do
card = Card.new(:clubs, 4)
card.value.should eq(4)
end
it "should return 11 for Ace" do
card = Card.new(:diamonds, "A")
card.value.should eq(11)
end
it "should be formatted nicely" do
card = Card.new(:diamonds, "A")
card.to_s.should eq("DA")
end
end
describe DownCard do
it "should show XX before the game is done" do
downcard = DownCard.new(:diamonds, "K")
downcard.to_s.should eq("XX")
end
it "should show the actual card after the game is done" do
downcard = DownCard.new(:diamonds, "K")
downcard.game_done!
downcard.to_s.should eq("DK")
end
it "should return a value of 0 before the game is done" do
downcard = DownCard.new(:hearts, 9)
downcard.value.should eq(0)
end
it "should return the actual value after the game is done" do
downcard = DownCard.new(:hearts, 9)
downcard.game_done!
downcard.value.should eq(9)
end
end
describe Deck do
it "should build 52 cards" do
Deck.build_cards.length.should eq(52)
end
it "should have 52 cards when new deck" do
Deck.new.cards.length.should eq(52)
end
end
describe Hand do
it "should calculate the value correctly" do
deck = mock(:deck, :cards => [Card.new(:clubs, 4), Card.new(:diamonds, 10)])
hand = Hand.new
2.times { hand.hit!(deck) }
hand.value.should eq(14)
end
it "should take from the top of the deck" do
club4 = Card.new(:clubs, 4)
diamond7 = Card.new(:diamonds, 7)
clubK = Card.new(:clubs, "K")
deck = mock(:deck, :cards => [club4, diamond7, clubK])
hand = Hand.new
2.times { hand.hit!(deck) }
hand.cards.should eq([club4, diamond7])
end
describe "#play_as_dealer" do
it "should hit blow 16" do
deck = mock(:deck, :cards => [Card.new(:clubs, 4), Card.new(:diamonds, 4), Card.new(:clubs, 2), Card.new(:hearts, 6)])
hand = Hand.new
2.times { hand.hit!(deck) }
hand.play_as_dealer(deck)
hand.value.should eq(16)
end
it "should not hit above" do
deck = mock(:deck, :cards => [Card.new(:clubs, 8), Card.new(:diamonds, 9)])
hand = Hand.new
2.times { hand.hit!(deck) }
hand.play_as_dealer(deck)
hand.value.should eq(17)
end
it "should stop on 21" do
deck = mock(:deck, :cards => [Card.new(:clubs, 4),
Card.new(:diamonds, 7),
Card.new(:clubs, "K")])
hand = Hand.new
2.times { hand.hit!(deck) }
hand.play_as_dealer(deck)
hand.value.should eq(21)
end
end
end
describe DealerHand do
describe "#hit!" do
it "should have a downcard on the first hit" do
deck = Deck.new
dealer_hand = DealerHand.new.hit!(deck)
dealer_hand[0].to_s.should eq("XX")
end
end
end
describe Game do
it "should have a players hand" do
Game.new.player_hand.cards.length.should eq(2)
end
it "should have a dealers hand" do
Game.new.dealer_hand.cards.length.should eq(2)
end
it "should have a status" do
Game.new.status.should_not be_nil
end
it "should hit when I tell it to" do
game = Game.new
game.hit
game.player_hand.cards.length.should eq(3)
end
it "should play the dealer hand when I stand" do
game = Game.new
game.stand
game.status[:winner].should_not be_nil
end
it "should not show the full dealer hand before the game is done" do
game = Game.new
game.status[:dealer_cards].to_s.should include("XX")
end
it "should show the full dealer hand after the game is done" do
game = Game.new
game.stand
game.status[:dealer_cards].to_s.should_not include("XX")
end
it "should #stand for the player if they bust" do
game = Game.new
game.should_receive(:stand)
while game.status[:player_value] <= 21
game.hit
end
end
describe "#determine_winner" do
it "should have dealer win when player busts" do
Game.new.determine_winner(22, 15).should eq(:dealer)
end
it "should player win if dealer busts" do
Game.new.determine_winner(18, 22).should eq(:player)
end
it "should have player win if player > dealer" do
Game.new.determine_winner(18, 16).should eq(:player)
end
it "should have push if tie" do
Game.new.determine_winner(16, 16).should eq(:push)
end
end
end