console-blackjack 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +108 -0
- data/LICENSE +21 -0
- data/README.md +28 -0
- data/Rakefile +5 -0
- data/bin/console-blackjack +5 -0
- data/bj.png +0 -0
- data/bj.txt +1 -0
- data/console-blackjack.gemspec +25 -0
- data/lib/blackjack.rb +382 -0
- data/lib/blackjack/card.rb +29 -0
- data/lib/blackjack/dealer_hand.rb +63 -0
- data/lib/blackjack/hand.rb +33 -0
- data/lib/blackjack/player_hand.rb +184 -0
- data/lib/blackjack/shoe.rb +108 -0
- data/spec/factories/blackjack_factory.rb +13 -0
- data/spec/factories/card_factory.rb +42 -0
- data/spec/factories/dealer_hand_factory.rb +10 -0
- data/spec/factories/hand_factory.rb +11 -0
- data/spec/factories/player_hand_factory.rb +13 -0
- data/spec/factories/shoe_factory.rb +14 -0
- data/spec/lib/blackjack/card_spec.rb +53 -0
- data/spec/lib/blackjack/dealer_hand_spec.rb +223 -0
- data/spec/lib/blackjack/hand_spec.rb +53 -0
- data/spec/lib/blackjack/player_hand_spec.rb +563 -0
- data/spec/lib/blackjack/shoe_spec.rb +164 -0
- data/spec/lib/blackjack_spec.rb +871 -0
- data/spec/spec_helper.rb +36 -0
- metadata +75 -0
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe Card do
|
4
|
+
let(:card) { build(:card) }
|
5
|
+
|
6
|
+
describe '.new' do
|
7
|
+
it 'creates a card' do
|
8
|
+
expect(card).to be
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'has a value' do
|
12
|
+
expect(card.value).to eq(0)
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'has a suite' do
|
16
|
+
expect(card.suite).to eq(0)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#to_s' do
|
21
|
+
it 'returns a string value' do
|
22
|
+
expect(card.to_s).to eq('🂡')
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '#ace?' do
|
27
|
+
it 'returns true' do
|
28
|
+
expect(card).to be_ace
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'returns false' do
|
32
|
+
card = build(:card, :two)
|
33
|
+
expect(card).not_to be_ace
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '#ten?' do
|
38
|
+
it 'returns true' do
|
39
|
+
card = build(:card, :ten)
|
40
|
+
expect(card).to be_ten
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'returns false' do
|
44
|
+
expect(card).not_to be_ten
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '.faces' do
|
49
|
+
it 'returns a five of clubs' do
|
50
|
+
expect(described_class.faces[4][3]).to eq('🃕')
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,223 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe DealerHand do
|
4
|
+
let(:shoe) { build(:shoe, :new_regular) }
|
5
|
+
let(:blackjack) { build(:blackjack) }
|
6
|
+
let(:dealer_hand) { build(:dealer_hand, blackjack: blackjack) }
|
7
|
+
let(:ace) { build(:card, :ace) }
|
8
|
+
let(:five) { build(:card, :five) }
|
9
|
+
let(:six) { build(:card, :six) }
|
10
|
+
let(:seven) { build(:card, :seven) }
|
11
|
+
let(:eight) { build(:card, :eight) }
|
12
|
+
let(:nine) { build(:card, :nine) }
|
13
|
+
let(:ten) { build(:card, :ten) }
|
14
|
+
|
15
|
+
describe '.new' do
|
16
|
+
it 'creates a dealer_hand' do
|
17
|
+
expect(dealer_hand).to be
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'has a blackjack' do
|
21
|
+
expect(dealer_hand.blackjack).to eq(blackjack)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#busted?' do
|
26
|
+
it 'returns false' do
|
27
|
+
expect(dealer_hand).to_not be_busted
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'returns true' do
|
31
|
+
dealer_hand.cards << ten << ten << ten
|
32
|
+
dealer_hand.hide_down_card = false
|
33
|
+
expect(dealer_hand).to be_busted
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '#value' do
|
38
|
+
context 'with a soft count' do
|
39
|
+
it 'returns 10' do
|
40
|
+
dealer_hand.cards << ten << ace
|
41
|
+
expect(dealer_hand.value(SOFT)).to eq(10)
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'returns 11' do
|
45
|
+
dealer_hand.cards << ace << ten
|
46
|
+
expect(dealer_hand.value(SOFT)).to eq(11)
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'returns 12' do
|
50
|
+
dealer_hand.cards << ten << ace << ace
|
51
|
+
dealer_hand.hide_down_card = false
|
52
|
+
expect(dealer_hand.value(SOFT)).to eq(12)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'with a hard count' do
|
57
|
+
it 'returns 10' do
|
58
|
+
dealer_hand.cards << ten << ace
|
59
|
+
expect(dealer_hand.value(HARD)).to eq(10)
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'returns 1' do
|
63
|
+
dealer_hand.cards << ace << ten
|
64
|
+
expect(dealer_hand.value(HARD)).to eq(1)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe '#upcard_is_ace?' do
|
70
|
+
it 'returns false' do
|
71
|
+
dealer_hand.cards << ten << ace
|
72
|
+
expect(dealer_hand).to_not be_upcard_is_ace
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'returns true' do
|
76
|
+
dealer_hand.cards << ace << ten
|
77
|
+
expect(dealer_hand).to be_upcard_is_ace
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe '#draw' do
|
82
|
+
it 'returns " 🂪 🂠⇒ 10"' do
|
83
|
+
dealer_hand.cards << ten << ace
|
84
|
+
expected = ' 🂪 🂠⇒ 10'
|
85
|
+
expect(dealer_hand.draw).to eq(expected)
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'returns " 🂡 🂠⇒ 11"' do
|
89
|
+
dealer_hand.cards << ace << ten
|
90
|
+
expected = ' 🂡 🂠⇒ 11'
|
91
|
+
expect(dealer_hand.draw).to eq(expected)
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'returns " 🂡 🂪 ⇒ 21"' do
|
95
|
+
dealer_hand.cards << ace << ten
|
96
|
+
dealer_hand.hide_down_card = false
|
97
|
+
expected = ' 🂡 🂪 ⇒ 21'
|
98
|
+
expect(dealer_hand.draw).to eq(expected)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe '#both_values' do
|
103
|
+
context 'with a soft count' do
|
104
|
+
it 'returns [10, 10]' do
|
105
|
+
dealer_hand.cards << ten << ace
|
106
|
+
expect(dealer_hand.both_values).to eq([10, 10])
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'returns [11, 1]' do
|
110
|
+
dealer_hand.cards << ace << ten
|
111
|
+
expect(dealer_hand.both_values).to eq([11, 1])
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'returns [12, 12]' do
|
115
|
+
dealer_hand.cards << ten << ace << ace
|
116
|
+
dealer_hand.hide_down_card = false
|
117
|
+
expect(dealer_hand.both_values).to eq([12, 12])
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context 'with a hard count' do
|
122
|
+
it 'returns [10, 10]' do
|
123
|
+
dealer_hand.cards << ten << ace
|
124
|
+
expect(dealer_hand.both_values).to eq([10, 10])
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'returns [11, 1]' do
|
128
|
+
dealer_hand.cards << ace << ten
|
129
|
+
expect(dealer_hand.both_values).to eq([11, 1])
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
describe '#deal_required_cards' do
|
135
|
+
let(:shoe) { build(:shoe, :new_regular) }
|
136
|
+
|
137
|
+
before do
|
138
|
+
blackjack.shoe = shoe
|
139
|
+
end
|
140
|
+
|
141
|
+
context 'when soft is < 18' do
|
142
|
+
it 'deals cards' do
|
143
|
+
dealer_hand.cards << ace << seven
|
144
|
+
dealer_hand.deal_required_cards
|
145
|
+
expect(dealer_hand.cards.size >= 3).to be_truthy
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
context 'when hard is < 17' do
|
150
|
+
it 'deals cards' do
|
151
|
+
dealer_hand.cards << ace << ten << five
|
152
|
+
dealer_hand.deal_required_cards
|
153
|
+
expect(dealer_hand.cards.size >= 4).to be_truthy
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
describe '#play' do
|
159
|
+
before do
|
160
|
+
blackjack.dealer_hand = dealer_hand
|
161
|
+
end
|
162
|
+
|
163
|
+
it 'plays the dealer hand' do
|
164
|
+
dealer_hand.play
|
165
|
+
expect(dealer_hand.played).to be_truthy
|
166
|
+
end
|
167
|
+
|
168
|
+
it 'pays hands' do
|
169
|
+
allow(blackjack).to receive(:pay_hands)
|
170
|
+
dealer_hand.play
|
171
|
+
expect(blackjack).to have_received(:pay_hands)
|
172
|
+
end
|
173
|
+
|
174
|
+
context 'when does not need to play dealer hand' do
|
175
|
+
before do
|
176
|
+
allow(blackjack).to receive(:need_to_play_dealer_hand?).and_return(false)
|
177
|
+
end
|
178
|
+
|
179
|
+
it 'hides down card when no blackjack' do
|
180
|
+
allow(dealer_hand).to receive(:blackjack?).and_return(false)
|
181
|
+
dealer_hand.play
|
182
|
+
expect(dealer_hand.hide_down_card).to be_truthy
|
183
|
+
end
|
184
|
+
|
185
|
+
it 'shows down card if blackjack' do
|
186
|
+
allow(dealer_hand).to receive(:blackjack?).and_return(true)
|
187
|
+
dealer_hand.play
|
188
|
+
expect(dealer_hand.hide_down_card).to be_falsey
|
189
|
+
end
|
190
|
+
|
191
|
+
it 'does not deal any cards' do
|
192
|
+
allow(dealer_hand).to receive(:deal_required_cards)
|
193
|
+
dealer_hand.play
|
194
|
+
expect(dealer_hand).to_not have_received(:deal_required_cards)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
context 'when need to play dealer hand' do
|
199
|
+
before do
|
200
|
+
blackjack.shoe = shoe
|
201
|
+
allow(blackjack).to receive(:need_to_play_dealer_hand?).and_return(true)
|
202
|
+
end
|
203
|
+
|
204
|
+
it 'shows down card if not blackjack' do
|
205
|
+
allow(dealer_hand).to receive(:blackjack?).and_return(false)
|
206
|
+
dealer_hand.play
|
207
|
+
expect(dealer_hand.hide_down_card).to be_falsey
|
208
|
+
end
|
209
|
+
|
210
|
+
it 'shows down card if blackjack' do
|
211
|
+
allow(dealer_hand).to receive(:blackjack?).and_return(true)
|
212
|
+
dealer_hand.play
|
213
|
+
expect(dealer_hand.hide_down_card).to be_falsey
|
214
|
+
end
|
215
|
+
|
216
|
+
it 'deals required cards' do
|
217
|
+
allow(dealer_hand).to receive(:deal_required_cards)
|
218
|
+
dealer_hand.play
|
219
|
+
expect(dealer_hand).to have_received(:deal_required_cards)
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe Hand do
|
4
|
+
let(:shoe) { build(:shoe, :new_regular) }
|
5
|
+
let(:blackjack) { build(:blackjack, shoe: shoe) }
|
6
|
+
let(:hand) { build(:hand, blackjack: blackjack) }
|
7
|
+
|
8
|
+
describe '.new' do
|
9
|
+
it 'creates a hand' do
|
10
|
+
expect(hand).to be
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'has a blackjack' do
|
14
|
+
expect(hand.blackjack).to eq(blackjack)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#deal_card' do
|
19
|
+
it 'adds a card to the hand' do
|
20
|
+
expect {
|
21
|
+
hand.deal_card
|
22
|
+
}.to change { hand.cards.size }.by(1)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '#blackjack?' do
|
27
|
+
context 'with an empty hand' do
|
28
|
+
it 'returns false' do
|
29
|
+
expect(hand).to_not be_blackjack
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'with two cards' do
|
34
|
+
let(:ace) { build(:card, :ace) }
|
35
|
+
let(:ten) { build(:card, :ten) }
|
36
|
+
|
37
|
+
it 'an ace and a ten returns true' do
|
38
|
+
hand.cards << ace << ten
|
39
|
+
expect(hand).to be_blackjack
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'a ten and an ace returns true' do
|
43
|
+
hand.cards << ten << ace
|
44
|
+
expect(hand).to be_blackjack
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'two aces returns false' do
|
48
|
+
hand.cards << ace << ace
|
49
|
+
expect(hand).to_not be_blackjack
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,563 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
RSpec.describe PlayerHand do
|
5
|
+
let(:shoe) { build(:shoe, :new_regular) }
|
6
|
+
let(:blackjack) { build(:blackjack, shoe: shoe) }
|
7
|
+
let(:player_hand) { build(:player_hand, blackjack: blackjack) }
|
8
|
+
let(:dealer_hand) { build(:dealer_hand, blackjack: blackjack) }
|
9
|
+
let(:ace) { build(:card, :ace) }
|
10
|
+
let(:six) { build(:card, :six) }
|
11
|
+
let(:seven) { build(:card, :seven) }
|
12
|
+
let(:eight) { build(:card, :eight) }
|
13
|
+
let(:nine) { build(:card, :nine) }
|
14
|
+
let(:ten) { build(:card, :ten) }
|
15
|
+
|
16
|
+
describe '.new' do
|
17
|
+
it 'creates a player_hand' do
|
18
|
+
expect(player_hand).to be
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'has a blackjack' do
|
22
|
+
expect(player_hand.blackjack).to eq(blackjack)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'has a bet' do
|
26
|
+
expect(player_hand.bet).to eq(500)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'has an unknown status' do
|
30
|
+
expect(player_hand.status).to eq(UNKNOWN)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'has not been payed' do
|
34
|
+
expect(player_hand.payed).to be_falsey
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '#busted?' do
|
39
|
+
it 'returns false' do
|
40
|
+
expect(player_hand).to_not be_busted
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'returns true' do
|
44
|
+
player_hand.cards << ten << ten << ten
|
45
|
+
expect(player_hand).to be_busted
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe '#value' do
|
50
|
+
context 'with a soft count' do
|
51
|
+
it 'returns 21' do
|
52
|
+
player_hand.cards << ten << ace
|
53
|
+
expect(player_hand.value(SOFT)).to eq(21)
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'returns 12' do
|
57
|
+
player_hand.cards << ten << ace << ace
|
58
|
+
expect(player_hand.value(SOFT)).to eq(12)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'with a hard count' do
|
63
|
+
it 'returns 11' do
|
64
|
+
player_hand.cards << ten << ace
|
65
|
+
expect(player_hand.value(HARD)).to eq(11)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe '#done?' do
|
71
|
+
context 'when played' do
|
72
|
+
it 'returns true' do
|
73
|
+
player_hand.played = true
|
74
|
+
expect(player_hand).to be_done
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'when stood' do
|
79
|
+
it 'returns true' do
|
80
|
+
player_hand.stood = true
|
81
|
+
expect(player_hand).to be_done
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'when blackjack' do
|
86
|
+
it 'returns true' do
|
87
|
+
player_hand.cards << ace << ten
|
88
|
+
expect(player_hand).to be_done
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
context 'when busted' do
|
93
|
+
it 'returns true' do
|
94
|
+
player_hand.cards << ten << ten << ten
|
95
|
+
expect(player_hand).to be_done
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
context 'when soft count of 21' do
|
100
|
+
it 'returns true' do
|
101
|
+
player_hand.cards << ace << ace << nine
|
102
|
+
expect(player_hand).to be_done
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context 'when hard count of 21' do
|
107
|
+
it 'returns true' do
|
108
|
+
player_hand.cards << ace << ten << ten
|
109
|
+
expect(player_hand).to be_done
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
context 'when pair of sevens' do
|
114
|
+
it 'returns false' do
|
115
|
+
player_hand.cards << seven << seven
|
116
|
+
expect(player_hand).to_not be_done
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
describe '#can_split?' do
|
122
|
+
context 'when stood' do
|
123
|
+
it 'returns false' do
|
124
|
+
player_hand.stood = true
|
125
|
+
expect(player_hand).to_not be_can_split
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
context 'when MAX_PLAYER_HANDS' do
|
130
|
+
it 'returns false' do
|
131
|
+
6.times { blackjack.player_hands << build(:player_hand) }
|
132
|
+
expect(player_hand).to_not be_can_split
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
context 'when not enough money' do
|
137
|
+
it 'returns false' do
|
138
|
+
blackjack.money = 9999
|
139
|
+
expect(player_hand).to_not be_can_split
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
context 'when more than 2 cards' do
|
144
|
+
it 'returns false' do
|
145
|
+
player_hand.cards << ace << ace << ace
|
146
|
+
expect(player_hand).to_not be_can_split
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
context 'when card values do not match' do
|
151
|
+
it 'returns false' do
|
152
|
+
player_hand.cards << seven << nine
|
153
|
+
expect(player_hand).to_not be_can_split
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
context 'when card values match' do
|
158
|
+
it 'returns true' do
|
159
|
+
player_hand.cards << seven << seven
|
160
|
+
expect(player_hand).to be_can_split
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
describe '#can_dbl?' do
|
166
|
+
context 'when not enough money' do
|
167
|
+
it 'returns false' do
|
168
|
+
blackjack.money = 9999
|
169
|
+
expect(player_hand).to_not be_can_dbl
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
context 'when stood' do
|
174
|
+
it 'returns false' do
|
175
|
+
player_hand.stood = true
|
176
|
+
expect(player_hand).to_not be_can_dbl
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
context 'when more than 2 cards' do
|
181
|
+
it 'returns false' do
|
182
|
+
player_hand.cards << ace << ace << ace
|
183
|
+
expect(player_hand).to_not be_can_dbl
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
context 'when blackjack' do
|
188
|
+
it 'returns false' do
|
189
|
+
player_hand.cards << ace << ten
|
190
|
+
expect(player_hand).to_not be_can_dbl
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
context 'when a pair of sixes' do
|
195
|
+
it 'returns true' do
|
196
|
+
player_hand.cards << six << six
|
197
|
+
expect(player_hand).to be_can_dbl
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
describe '#can_stand?' do
|
203
|
+
context 'when stood' do
|
204
|
+
it 'returns false' do
|
205
|
+
player_hand.stood = true
|
206
|
+
expect(player_hand).to_not be_can_stand
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
context 'when blackjack' do
|
211
|
+
it 'returns false' do
|
212
|
+
player_hand.cards << ace << ten
|
213
|
+
expect(player_hand).to_not be_can_stand
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
context 'when busted' do
|
218
|
+
it 'returns false' do
|
219
|
+
player_hand.cards << eight << eight << eight
|
220
|
+
expect(player_hand).to_not be_can_stand
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
context 'when a pair of sixes' do
|
225
|
+
it 'returns true' do
|
226
|
+
player_hand.cards << six << six
|
227
|
+
expect(player_hand).to be_can_stand
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
describe '#can_hit?' do
|
233
|
+
context 'when played' do
|
234
|
+
it 'returns false' do
|
235
|
+
player_hand.played = true
|
236
|
+
expect(player_hand).to_not be_can_hit
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
context 'when stood' do
|
241
|
+
it 'returns false' do
|
242
|
+
player_hand.stood = true
|
243
|
+
expect(player_hand).to_not be_can_hit
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
context 'when blackjack' do
|
248
|
+
it 'returns false' do
|
249
|
+
player_hand.cards << ace << ten
|
250
|
+
expect(player_hand).to_not be_can_hit
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
context 'when busted' do
|
255
|
+
it 'returns false' do
|
256
|
+
player_hand.cards << eight << eight << eight
|
257
|
+
expect(player_hand).to_not be_can_hit
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
context 'when a hard 21' do
|
262
|
+
it 'returns false' do
|
263
|
+
player_hand.cards << seven << seven << seven
|
264
|
+
expect(player_hand).to_not be_can_hit
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
context 'when a pair of sixes' do
|
269
|
+
it 'returns true' do
|
270
|
+
player_hand.cards << six << six
|
271
|
+
expect(player_hand).to be_can_hit
|
272
|
+
end
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
describe '#draw' do
|
277
|
+
it 'draws the hand' do
|
278
|
+
player_hand.cards << ace << ten
|
279
|
+
expected = " 🂡 🂪 ⇒ 21 $5.00 \n\n"
|
280
|
+
expect(player_hand.draw(1)).to eq(expected)
|
281
|
+
end
|
282
|
+
|
283
|
+
it 'draws a lost hand' do
|
284
|
+
player_hand.cards << ace << ace
|
285
|
+
player_hand.status = LOST
|
286
|
+
expected = " 🂡 🂡 ⇒ 12 -$5.00 Lose!\n\n"
|
287
|
+
expect(player_hand.draw(1)).to eq(expected)
|
288
|
+
end
|
289
|
+
|
290
|
+
it 'draws a won hand' do
|
291
|
+
player_hand.cards << ace << ace
|
292
|
+
player_hand.status = WON
|
293
|
+
expected = " 🂡 🂡 ⇒ 12 +$5.00 Won!\n\n"
|
294
|
+
expect(player_hand.draw(1)).to eq(expected)
|
295
|
+
end
|
296
|
+
|
297
|
+
it 'draws a push hand' do
|
298
|
+
player_hand.cards << ace << ace
|
299
|
+
player_hand.status = PUSH
|
300
|
+
expected = " 🂡 🂡 ⇒ 12 $5.00 Push\n\n"
|
301
|
+
expect(player_hand.draw(1)).to eq(expected)
|
302
|
+
end
|
303
|
+
end
|
304
|
+
|
305
|
+
describe '#process' do
|
306
|
+
context 'with more hands to play' do
|
307
|
+
before do
|
308
|
+
allow(blackjack).to receive(:more_hands_to_play?).and_return(true)
|
309
|
+
allow(blackjack).to receive(:play_more_hands)
|
310
|
+
end
|
311
|
+
|
312
|
+
it 'plays more hands' do
|
313
|
+
player_hand.process
|
314
|
+
expect(blackjack).to have_received(:play_more_hands)
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
318
|
+
context 'with no more hands to play' do
|
319
|
+
before do
|
320
|
+
allow(blackjack).to receive(:more_hands_to_play?).and_return(false)
|
321
|
+
allow(blackjack).to receive(:play_dealer_hand)
|
322
|
+
end
|
323
|
+
|
324
|
+
it 'plays dealer hand' do
|
325
|
+
player_hand.process
|
326
|
+
expect(blackjack).to have_received(:play_dealer_hand)
|
327
|
+
end
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
describe '#hit' do
|
332
|
+
before do
|
333
|
+
blackjack.dealer_hand = dealer_hand
|
334
|
+
end
|
335
|
+
|
336
|
+
context 'when done' do
|
337
|
+
let(:dealer_hand) { build(:dealer_hand, blackjack: blackjack) }
|
338
|
+
|
339
|
+
before do
|
340
|
+
allow(player_hand).to receive(:done?).and_return(true)
|
341
|
+
allow(player_hand).to receive(:process)
|
342
|
+
end
|
343
|
+
|
344
|
+
it 'adds a card to the hand' do
|
345
|
+
expect { player_hand.hit }.to change { player_hand.cards.size }.by(1)
|
346
|
+
end
|
347
|
+
end
|
348
|
+
|
349
|
+
context 'when not done' do
|
350
|
+
before do
|
351
|
+
allow(player_hand).to receive(:done?).and_return(false)
|
352
|
+
allow(blackjack).to receive(:current_player_hand).and_return(player_hand)
|
353
|
+
allow(player_hand).to receive(:action?)
|
354
|
+
allow(blackjack).to receive(:draw_hands)
|
355
|
+
end
|
356
|
+
|
357
|
+
it 'adds a card to the hand' do
|
358
|
+
expect { player_hand.hit }.to change { player_hand.cards.size }.by(1)
|
359
|
+
end
|
360
|
+
end
|
361
|
+
end
|
362
|
+
|
363
|
+
describe '#dbl' do
|
364
|
+
before do
|
365
|
+
blackjack.dealer_hand = dealer_hand
|
366
|
+
allow(blackjack).to receive(:draw_hands)
|
367
|
+
allow(blackjack).to receive(:draw_bet_options)
|
368
|
+
allow(player_hand).to receive(:process)
|
369
|
+
end
|
370
|
+
|
371
|
+
context 'when done' do
|
372
|
+
before do
|
373
|
+
allow(player_hand).to receive(:done?).and_return(true)
|
374
|
+
player_hand.dbl
|
375
|
+
end
|
376
|
+
|
377
|
+
it 'adds a card to the hand' do
|
378
|
+
expect(player_hand.cards.size).to eq(1)
|
379
|
+
end
|
380
|
+
|
381
|
+
it 'sets hand to played' do
|
382
|
+
expect(player_hand.played).to be_truthy
|
383
|
+
end
|
384
|
+
|
385
|
+
it 'doubles the bet' do
|
386
|
+
expect(player_hand.bet).to eq(1000)
|
387
|
+
end
|
388
|
+
|
389
|
+
it 'calls process' do
|
390
|
+
expect(player_hand).to have_received(:process)
|
391
|
+
end
|
392
|
+
end
|
393
|
+
|
394
|
+
context 'when not done' do
|
395
|
+
before do
|
396
|
+
allow(player_hand).to receive(:done?).and_return(false)
|
397
|
+
player_hand.dbl
|
398
|
+
end
|
399
|
+
|
400
|
+
it 'does not call process' do
|
401
|
+
expect(player_hand).to_not have_received(:process)
|
402
|
+
end
|
403
|
+
end
|
404
|
+
end
|
405
|
+
|
406
|
+
describe '#stand' do
|
407
|
+
before do
|
408
|
+
allow(player_hand).to receive(:process)
|
409
|
+
allow(blackjack).to receive(:draw_hands)
|
410
|
+
allow(blackjack).to receive(:draw_bet_options)
|
411
|
+
blackjack.dealer_hand = dealer_hand
|
412
|
+
player_hand.stand
|
413
|
+
end
|
414
|
+
|
415
|
+
it 'sets the hand as stood' do
|
416
|
+
expect(player_hand.stood).to be_truthy
|
417
|
+
end
|
418
|
+
|
419
|
+
it 'sets the hand as played' do
|
420
|
+
expect(player_hand.played).to be_truthy
|
421
|
+
end
|
422
|
+
|
423
|
+
it 'calls process' do
|
424
|
+
expect(player_hand).to have_received(:process)
|
425
|
+
end
|
426
|
+
end
|
427
|
+
|
428
|
+
describe '#action?' do
|
429
|
+
before do
|
430
|
+
player_hand.cards << ace << ace
|
431
|
+
blackjack.player_hands << player_hand
|
432
|
+
blackjack.dealer_hand = dealer_hand
|
433
|
+
allow(player_hand).to receive(:puts)
|
434
|
+
end
|
435
|
+
|
436
|
+
context 'when standing' do
|
437
|
+
before do
|
438
|
+
allow(Blackjack).to receive(:getc).and_return('s', 'q')
|
439
|
+
allow(player_hand).to receive(:stand)
|
440
|
+
end
|
441
|
+
|
442
|
+
it 'stands the hand' do
|
443
|
+
player_hand.action?
|
444
|
+
expect(player_hand).to have_received(:stand)
|
445
|
+
end
|
446
|
+
end
|
447
|
+
|
448
|
+
context 'when hitting' do
|
449
|
+
before do
|
450
|
+
allow(Blackjack).to receive(:getc).and_return('h', 'q')
|
451
|
+
allow(player_hand).to receive(:hit)
|
452
|
+
end
|
453
|
+
|
454
|
+
it 'hits the hand' do
|
455
|
+
player_hand.action?
|
456
|
+
expect(player_hand).to have_received(:hit)
|
457
|
+
end
|
458
|
+
end
|
459
|
+
|
460
|
+
context 'when doubling' do
|
461
|
+
before do
|
462
|
+
allow(Blackjack).to receive(:getc).and_return('d', 'q')
|
463
|
+
allow(player_hand).to receive(:dbl)
|
464
|
+
end
|
465
|
+
|
466
|
+
it 'doubles the hand' do
|
467
|
+
player_hand.action?
|
468
|
+
expect(player_hand).to have_received(:dbl)
|
469
|
+
end
|
470
|
+
end
|
471
|
+
|
472
|
+
context 'when splitting' do
|
473
|
+
before do
|
474
|
+
allow(Blackjack).to receive(:getc).and_return('p', 'q')
|
475
|
+
allow(blackjack).to receive(:split_current_hand)
|
476
|
+
end
|
477
|
+
|
478
|
+
it 'splits the hand' do
|
479
|
+
player_hand.action?
|
480
|
+
expect(blackjack).to have_received(:split_current_hand)
|
481
|
+
end
|
482
|
+
end
|
483
|
+
|
484
|
+
context 'when invalid input' do
|
485
|
+
before do
|
486
|
+
allow(Blackjack).to receive(:getc).and_return('x', 's', 'q')
|
487
|
+
allow(blackjack).to receive(:clear)
|
488
|
+
allow(blackjack).to receive(:draw_hands)
|
489
|
+
allow(blackjack).to receive(:puts)
|
490
|
+
end
|
491
|
+
|
492
|
+
it 'gets the action again' do
|
493
|
+
expect { player_hand.action? }.to raise_error(SystemExit)
|
494
|
+
end
|
495
|
+
end
|
496
|
+
end
|
497
|
+
|
498
|
+
describe '#pay' do
|
499
|
+
it 'returns if already payed' do
|
500
|
+
player_hand.payed = true
|
501
|
+
allow(player_hand).to receive(:value)
|
502
|
+
player_hand.pay(18, false)
|
503
|
+
expect(player_hand).to_not have_received(:value)
|
504
|
+
end
|
505
|
+
|
506
|
+
context 'when dealer busted' do
|
507
|
+
it 'hand is won' do
|
508
|
+
player_hand.pay(22, true)
|
509
|
+
expect(player_hand.status).to eq(WON)
|
510
|
+
end
|
511
|
+
end
|
512
|
+
|
513
|
+
context 'when dealer is not busted' do
|
514
|
+
it 'hand is set to payed' do
|
515
|
+
player_hand.cards << ten << ten
|
516
|
+
player_hand.pay(18, false)
|
517
|
+
expect(player_hand.payed).to be_truthy
|
518
|
+
end
|
519
|
+
|
520
|
+
it 'hand status is won' do
|
521
|
+
player_hand.cards << ten << ten
|
522
|
+
player_hand.pay(18, false)
|
523
|
+
expect(player_hand.status).to eq(WON)
|
524
|
+
end
|
525
|
+
|
526
|
+
it 'blackjack money is increased by player hand bet' do
|
527
|
+
player_hand.cards << ten << ten
|
528
|
+
player_hand.pay(18, false)
|
529
|
+
expect(blackjack.money).to eq(10_500)
|
530
|
+
end
|
531
|
+
|
532
|
+
it 'hand bet is * 1.5' do
|
533
|
+
player_hand.cards << ace << ten
|
534
|
+
player_hand.pay(18, false)
|
535
|
+
expect(player_hand.bet).to eq(750)
|
536
|
+
end
|
537
|
+
|
538
|
+
it 'hand is lost' do
|
539
|
+
player_hand.cards << ten << seven
|
540
|
+
player_hand.pay(18, false)
|
541
|
+
expect(player_hand.status).to eq(LOST)
|
542
|
+
end
|
543
|
+
|
544
|
+
it 'blackjack money is descreased by player hand bet' do
|
545
|
+
player_hand.cards << ten << seven
|
546
|
+
player_hand.pay(18, false)
|
547
|
+
expect(blackjack.money).to eq(9500)
|
548
|
+
end
|
549
|
+
|
550
|
+
it 'hand is push' do
|
551
|
+
player_hand.cards << ten << seven
|
552
|
+
player_hand.pay(17, false)
|
553
|
+
expect(player_hand.status).to eq(PUSH)
|
554
|
+
end
|
555
|
+
|
556
|
+
it 'blackjack money is unaltered' do
|
557
|
+
player_hand.cards << ten << seven
|
558
|
+
player_hand.pay(17, false)
|
559
|
+
expect(blackjack.money).to eq(10_000)
|
560
|
+
end
|
561
|
+
end
|
562
|
+
end
|
563
|
+
end
|