-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathblackjack.rb
More file actions
242 lines (201 loc) · 5.22 KB
/
blackjack.rb
File metadata and controls
242 lines (201 loc) · 5.22 KB
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
require 'rspec'
class Card
attr_reader :suit, :value
def initialize(suit, value)
@suit = suit
@value = value
end
def value
return 10 if ["J", "Q", "K"].include?(@value)
return 11 if @value == "A"
return @value
end
def to_s
"#{@value}-#{suit}"
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)
if value < 16
hit!(deck)
play_as_dealer(deck)
end
end
end
class Game
attr_reader :player_hand, :dealer_hand
def initialize
@deck = Deck.new
@player_hand = Hand.new
@dealer_hand = Hand.new
2.times { @player_hand.hit!(@deck) }
2.times { @dealer_hand.hit!(@deck) }
end
def hit
@player_hand.hit!(@deck)
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("A-diamonds")
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 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
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