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.
@@ -0,0 +1,164 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Shoe do
4
+ let(:shoe) { build(:shoe) }
5
+
6
+ describe '.new' do
7
+ it 'creates a shoe' do
8
+ expect(shoe).to be
9
+ end
10
+
11
+ it 'has a number of decks' do
12
+ expect(shoe.num_decks).to eq(1)
13
+ end
14
+ end
15
+
16
+ describe '#needs_to_shuffle?' do
17
+ context 'with an empty shoe' do
18
+ it 'returns true' do
19
+ expect(shoe).to be_needs_to_shuffle
20
+ end
21
+ end
22
+
23
+ context 'with a new regular shoe' do
24
+ let(:shoe) { build(:shoe, :new_regular) }
25
+
26
+ it 'returns false' do
27
+ expect(shoe).to_not be_needs_to_shuffle
28
+ end
29
+ end
30
+
31
+ context 'with 42 cards being dealt' do
32
+ let(:shoe) { build(:shoe, :new_regular) }
33
+
34
+ before do
35
+ 42.times { shoe.next_card }
36
+ end
37
+
38
+ it 'returns true' do
39
+ expect(shoe).to be_needs_to_shuffle
40
+ end
41
+ end
42
+ end
43
+
44
+ describe '#shuffle' do
45
+ let(:shoe) { build(:shoe) }
46
+
47
+ it 'calls shuffle' do
48
+ cards = instance_double(Array)
49
+ allow(cards).to receive(:shuffle!)
50
+ allow(shoe).to receive(:cards).and_return(cards)
51
+ shoe.shuffle
52
+ expect(cards).to have_received(:shuffle!).exactly(7).times
53
+ end
54
+ end
55
+
56
+ describe '#new_regular' do
57
+ let(:shoe) { build(:shoe) }
58
+
59
+ it 'creates a shoe' do
60
+ shoe.new_regular
61
+ expect(shoe.cards.size).to eq(52)
62
+ end
63
+
64
+ it 'calls shuffle' do
65
+ allow(shoe).to receive(:shuffle)
66
+ shoe.new_regular
67
+ expect(shoe).to have_received(:shuffle)
68
+ end
69
+ end
70
+
71
+ describe '#new_aces' do
72
+ let(:shoe) { build(:shoe) }
73
+
74
+ it 'creates a shoe' do
75
+ shoe.new_aces
76
+ expect(shoe.cards.size).to eq(40)
77
+ end
78
+
79
+ it 'calls shuffle' do
80
+ allow(shoe).to receive(:shuffle)
81
+ shoe.new_aces
82
+ expect(shoe).to have_received(:shuffle)
83
+ end
84
+ end
85
+
86
+ describe '#new_jacks' do
87
+ let(:shoe) { build(:shoe) }
88
+
89
+ it 'creates a shoe' do
90
+ shoe.new_jacks
91
+ expect(shoe.cards.size).to eq(40)
92
+ end
93
+
94
+ it 'calls shuffle' do
95
+ allow(shoe).to receive(:shuffle)
96
+ shoe.new_jacks
97
+ expect(shoe).to have_received(:shuffle)
98
+ end
99
+ end
100
+
101
+ describe '#new_aces_jacks' do
102
+ let(:shoe) { build(:shoe) }
103
+
104
+ it 'creates a shoe' do
105
+ shoe.new_aces_jacks
106
+ expect(shoe.cards.size).to eq(80)
107
+ end
108
+
109
+ it 'calls shuffle' do
110
+ allow(shoe).to receive(:shuffle)
111
+ shoe.new_aces_jacks
112
+ expect(shoe).to have_received(:shuffle)
113
+ end
114
+ end
115
+
116
+ describe '#new_sevens' do
117
+ let(:shoe) { build(:shoe) }
118
+
119
+ it 'creates a shoe' do
120
+ shoe.new_sevens
121
+ expect(shoe.cards.size).to eq(40)
122
+ end
123
+
124
+ it 'calls shuffle' do
125
+ allow(shoe).to receive(:shuffle)
126
+ shoe.new_sevens
127
+ expect(shoe).to have_received(:shuffle)
128
+ end
129
+ end
130
+
131
+ describe '#new_eights' do
132
+ let(:shoe) { build(:shoe) }
133
+
134
+ it 'creates a shoe' do
135
+ shoe.new_eights
136
+ expect(shoe.cards.size).to eq(40)
137
+ end
138
+
139
+ it 'calls shuffle' do
140
+ allow(shoe).to receive(:shuffle)
141
+ shoe.new_eights
142
+ expect(shoe).to have_received(:shuffle)
143
+ end
144
+ end
145
+
146
+ describe '#next_card' do
147
+ let(:shoe) { build(:shoe, :new_regular) }
148
+
149
+ it 'removes the next card' do
150
+ shoe.next_card
151
+ expect(shoe.cards.size).to eq(51)
152
+ end
153
+
154
+ it 'returns a Card' do
155
+ expect(shoe.next_card).to be_an_instance_of(Card)
156
+ end
157
+ end
158
+
159
+ describe '.shuffle_specs' do
160
+ it 'returns when to shuffle' do
161
+ expect(described_class.shuffle_specs[0]).to eq([95, 8])
162
+ end
163
+ end
164
+ end
@@ -0,0 +1,871 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Blackjack do
4
+ let(:shoe) { build(:shoe, :new_regular) }
5
+ let(:blackjack) { build(:blackjack, shoe: shoe) }
6
+ let(:player_hand) { build(:player_hand, blackjack: blackjack) }
7
+ let(:dealer_hand) { build(:dealer_hand, blackjack: blackjack) }
8
+ let(:ace) { build(:card, :ace) }
9
+ let(:seven) { build(:card, :seven) }
10
+ let(:six) { build(:card, :six) }
11
+ let(:ten) { build(:card, :ten) }
12
+
13
+ describe '#current_player_hand' do
14
+ it 'returns the current hand' do
15
+ blackjack.player_hands << player_hand
16
+ expect(blackjack.current_player_hand).to eq(player_hand)
17
+ end
18
+ end
19
+
20
+ describe '#more_hands_to_play?' do
21
+ it 'returns false' do
22
+ expect(blackjack).to_not be_more_hands_to_play
23
+ end
24
+
25
+ it 'returns true' do
26
+ blackjack.player_hands << player_hand << player_hand
27
+ expect(blackjack).to be_more_hands_to_play
28
+ end
29
+ end
30
+
31
+ describe '#getc' do
32
+ it 'get a single character from stdin' do
33
+ allow(STDIN).to receive(:getc).and_return('q')
34
+ c = described_class.getc
35
+ expect(c).to eq('q')
36
+ end
37
+ end
38
+
39
+ describe '#format_money' do
40
+ it 'returns a formatted string' do
41
+ str = described_class.format_money(1)
42
+ expect(str).to eq('1.00')
43
+ end
44
+ end
45
+
46
+ describe '.all_bets?' do
47
+ it 'returns 10' do
48
+ blackjack.player_hands << player_hand << player_hand
49
+ expect(blackjack.all_bets).to eq(1000)
50
+ end
51
+ end
52
+
53
+ describe '#need_to_play_dealer_hand?' do
54
+ it 'returns false' do
55
+ expect(blackjack).to_not be_need_to_play_dealer_hand
56
+ end
57
+
58
+ it 'returns true' do
59
+ blackjack.player_hands << player_hand
60
+ expect(blackjack).to be_need_to_play_dealer_hand
61
+ end
62
+ end
63
+
64
+ describe '#normalize_current_bet' do
65
+ it 'reduces the current bet to money' do
66
+ blackjack.current_bet = blackjack.money + 1
67
+ blackjack.normalize_current_bet
68
+ expect(blackjack.current_bet).to eq(blackjack.money)
69
+ end
70
+
71
+ it 'reduces the current bet to MAX_BET' do
72
+ blackjack.money = MAX_BET + 1
73
+ blackjack.current_bet = MAX_BET + 1
74
+ blackjack.normalize_current_bet
75
+ expect(blackjack.current_bet).to eq(MAX_BET)
76
+ end
77
+
78
+ it 'increases the current bet to MIN_BET' do
79
+ blackjack.current_bet = MIN_BET - 1
80
+ blackjack.normalize_current_bet
81
+ expect(blackjack.current_bet).to eq(MIN_BET)
82
+ end
83
+ end
84
+
85
+ describe '#clear' do
86
+ it 'calls system' do
87
+ ENV['CLEAR_TERM'] = '1'
88
+ allow(blackjack).to receive(:system)
89
+ blackjack.clear
90
+ expect(blackjack).to have_received(:system).with('export TERM=linux; clear')
91
+ end
92
+
93
+ it 'does not call system' do
94
+ ENV['CLEAR_TERM'] = '0'
95
+ allow(blackjack).to receive(:system)
96
+ blackjack.clear
97
+ expect(blackjack).to_not have_received(:system)
98
+ end
99
+ end
100
+
101
+ describe '#save_game' do
102
+ let(:file) { instance_double('File') }
103
+ let(:content) { "#{blackjack.num_decks}|#{blackjack.money}|#{blackjack.current_bet}" }
104
+
105
+ it 'opens and put save file data' do
106
+ allow(File).to receive(:open).with(SAVE_FILE, 'w').and_yield(file)
107
+ allow(file).to receive(:puts)
108
+ blackjack.save_game
109
+ expect(file).to have_received(:puts).with(content)
110
+ end
111
+ end
112
+
113
+ describe '#load_game' do
114
+ let(:content) { '8|2000|1000' }
115
+
116
+ before do
117
+ allow(File).to receive(:readable?).with(SAVE_FILE).and_return(true)
118
+ allow(File).to receive(:read).with(SAVE_FILE).and_return(content)
119
+ end
120
+
121
+ it 'loads num_decks from save file data' do
122
+ blackjack.load_game
123
+ expect(blackjack.num_decks).to eq(8)
124
+ end
125
+
126
+ it 'loads money from save file data' do
127
+ blackjack.load_game
128
+ expect(blackjack.money).to eq(2000)
129
+ end
130
+
131
+ it 'loads current_bet from save file data' do
132
+ blackjack.load_game
133
+ expect(blackjack.current_bet).to eq(1000)
134
+ end
135
+ end
136
+
137
+ describe '#run' do
138
+ before do
139
+ allow(blackjack).to receive(:load_game)
140
+ allow(blackjack).to receive(:deal_new_hand)
141
+ allow(Shoe).to receive(:new)
142
+ end
143
+
144
+ it 'calls load_game' do
145
+ blackjack.run
146
+ expect(blackjack).to have_received(:load_game)
147
+ end
148
+
149
+ it 'creates a new shoe' do
150
+ blackjack.run
151
+ expect(Shoe).to have_received(:new)
152
+ end
153
+
154
+ it 'deals a new hand' do
155
+ blackjack.run
156
+ expect(blackjack).to have_received(:deal_new_hand)
157
+ end
158
+ end
159
+
160
+ describe '#draw_bet_options' do
161
+ before do
162
+ allow(blackjack).to receive(:puts)
163
+ end
164
+
165
+ context 'when dealing a new hand' do
166
+ it 'deals a new hand' do
167
+ allow(described_class).to receive(:getc).and_return('d')
168
+ allow(blackjack).to receive(:deal_new_hand)
169
+ blackjack.draw_bet_options
170
+ expect(blackjack).to have_received(:deal_new_hand)
171
+ end
172
+ end
173
+
174
+ context 'when updating current bet' do
175
+ it 'deals a new hand' do
176
+ allow(described_class).to receive(:getc).and_return('b')
177
+ allow(blackjack).to receive(:new_bet)
178
+ blackjack.draw_bet_options
179
+ expect(blackjack).to have_received(:new_bet)
180
+ end
181
+ end
182
+
183
+ context 'when updating blackjack options' do
184
+ before do
185
+ blackjack.dealer_hand = dealer_hand
186
+ allow(described_class).to receive(:getc).and_return('o')
187
+ end
188
+
189
+ it 'draws blackjack options' do
190
+ allow(blackjack).to receive(:draw_game_options)
191
+ blackjack.draw_bet_options
192
+ expect(blackjack).to have_received(:draw_game_options)
193
+ end
194
+
195
+ it 'draws hands' do
196
+ allow(blackjack).to receive(:draw_game_options)
197
+ allow(blackjack).to receive(:draw_hands)
198
+ blackjack.draw_bet_options
199
+ expect(blackjack).to have_received(:draw_hands)
200
+ end
201
+
202
+ it 'clears the screen' do
203
+ allow(blackjack).to receive(:draw_game_options)
204
+ allow(blackjack).to receive(:draw_hands)
205
+ allow(blackjack).to receive(:clear)
206
+ blackjack.draw_bet_options
207
+ expect(blackjack).to have_received(:clear)
208
+ end
209
+ end
210
+
211
+ context 'when quitting' do
212
+ it 'clears the screen and exits' do
213
+ allow(described_class).to receive(:getc).and_return('q')
214
+ allow(blackjack).to receive(:clear)
215
+ expect { blackjack.draw_bet_options }.to raise_error(SystemExit)
216
+ expect(blackjack).to have_received(:clear)
217
+ end
218
+ end
219
+
220
+ context 'when invalid input' do
221
+ it 'gets the action again' do
222
+ blackjack.dealer_hand = dealer_hand
223
+ allow(described_class).to receive(:getc).and_return('x', 'q')
224
+ expect { blackjack.draw_bet_options }.to raise_error(SystemExit)
225
+ end
226
+ end
227
+ end
228
+
229
+ describe '#build_new_hand' do
230
+ before do
231
+ blackjack.build_new_hand
232
+ end
233
+
234
+ it 'creates a player hand' do
235
+ expect(blackjack.player_hands.size).to eq(1)
236
+ end
237
+
238
+ it 'sets current hand to zero' do
239
+ expect(blackjack.current_hand).to be_zero
240
+ end
241
+
242
+ it 'creates a dealerhand' do
243
+ expect(blackjack.dealer_hand).to be
244
+ end
245
+
246
+ it 'player hand has two cards' do
247
+ expect(blackjack.player_hands.first.cards.size).to eq(2)
248
+ end
249
+
250
+ it 'dealer hand has two cards' do
251
+ expect(blackjack.dealer_hand.cards.size).to eq(2)
252
+ end
253
+ end
254
+
255
+ describe '#deal_new_hand' do
256
+ before do
257
+ blackjack.player_hands << player_hand
258
+ blackjack.dealer_hand = dealer_hand
259
+ allow(blackjack).to receive(:draw_hands)
260
+ end
261
+
262
+ context 'when shuffling may be required' do
263
+ before do
264
+ dealer_hand.cards << ten << ten
265
+ allow(blackjack).to receive(:build_new_hand).and_return(player_hand)
266
+ allow(player_hand).to receive(:action?)
267
+ allow(shoe).to receive(:new_regular)
268
+ end
269
+
270
+ it 'shuffles' do
271
+ allow(shoe).to receive(:needs_to_shuffle?).and_return(true)
272
+ blackjack.deal_new_hand
273
+ expect(shoe).to have_received(:new_regular)
274
+ end
275
+
276
+ it 'does not shuffle' do
277
+ allow(shoe).to receive(:needs_to_shuffle?).and_return(false)
278
+ blackjack.deal_new_hand
279
+ expect(shoe).to_not have_received(:new_regular)
280
+ end
281
+ end
282
+
283
+ context 'when the dealer upcard is ace and player hand is not busted' do
284
+ before do
285
+ allow(blackjack).to receive(:build_new_hand).and_return(player_hand)
286
+ dealer_hand.cards << ace << ten
287
+ end
288
+
289
+ it 'asks about insurance' do
290
+ allow(blackjack).to receive(:ask_insurance)
291
+ blackjack.deal_new_hand
292
+ expect(blackjack).to have_received(:ask_insurance)
293
+ end
294
+ end
295
+
296
+ context 'when the player hand is done' do
297
+ before do
298
+ allow(blackjack).to receive(:build_new_hand).and_return(player_hand)
299
+ allow(player_hand).to receive(:action?)
300
+ allow(dealer_hand).to receive(:upcard_is_ace?).and_return(false)
301
+ allow(blackjack).to receive(:draw_bet_options)
302
+ allow(player_hand).to receive(:done?).and_return(true)
303
+ end
304
+
305
+ it 'shows dealer down card' do
306
+ blackjack.deal_new_hand
307
+ expect(dealer_hand.hide_down_card).to be_falsey
308
+ end
309
+
310
+ it 'hands are paid' do
311
+ allow(blackjack).to receive(:pay_hands)
312
+ blackjack.deal_new_hand
313
+ expect(blackjack).to have_received(:pay_hands)
314
+ end
315
+
316
+ it 'hands are drawn' do
317
+ blackjack.deal_new_hand
318
+ expect(blackjack).to have_received(:draw_hands)
319
+ end
320
+
321
+ it 'bet options are drawn' do
322
+ blackjack.deal_new_hand
323
+ expect(blackjack).to have_received(:draw_bet_options)
324
+ end
325
+ end
326
+ end
327
+
328
+ describe '#play_more_hands' do
329
+ before do
330
+ blackjack.player_hands << player_hand
331
+ allow(blackjack).to receive(:current_player_hand).and_return(player_hand)
332
+ end
333
+
334
+ context 'when current hand is done' do
335
+ it 'processes current player hand' do
336
+ allow(player_hand).to receive(:done?).and_return(true)
337
+ allow(player_hand).to receive(:process)
338
+ blackjack.play_more_hands
339
+ expect(player_hand).to have_received(:process)
340
+ end
341
+ end
342
+
343
+ context 'when current hand is not done' do
344
+ before do
345
+ allow(player_hand).to receive(:done?).and_return(false)
346
+ allow(blackjack).to receive(:draw_hands)
347
+ allow(blackjack).to receive(:draw_bet_options)
348
+ allow(player_hand).to receive(:action?)
349
+ blackjack.dealer_hand = dealer_hand
350
+ blackjack.play_more_hands
351
+ end
352
+
353
+ it 'draws hands' do
354
+ expect(blackjack).to have_received(:draw_hands)
355
+ end
356
+
357
+ it 'gets current player hand action' do
358
+ expect(player_hand).to have_received(:action?)
359
+ end
360
+ end
361
+ end
362
+
363
+ describe '#draw_hands' do
364
+ before do
365
+ player_hand.cards << ace << ten
366
+ blackjack.player_hands << player_hand
367
+ dealer_hand.cards << ten << ten
368
+ blackjack.dealer_hand = dealer_hand
369
+ allow(blackjack).to receive(:puts)
370
+ end
371
+
372
+ it 'draws the hands' do
373
+ blackjack.draw_hands
374
+ expected = "\n Dealer:\n 🂪 🂠 ⇒ 10\n\n Player $100.00:\n 🂡 🂪 ⇒ 21 $5.00 ⇐ \n\n"
375
+ expect(blackjack).to have_received(:puts).with(expected)
376
+ end
377
+ end
378
+
379
+ describe '#new_bet' do
380
+ before do
381
+ blackjack.dealer_hand = dealer_hand
382
+ allow(STDIN).to receive(:gets).and_return('10')
383
+ allow(described_class).to receive(:getc).and_return('s', 'q')
384
+ allow(blackjack).to receive(:print)
385
+ allow(blackjack).to receive(:puts)
386
+ allow(blackjack).to receive(:deal_new_hand)
387
+ allow(blackjack).to receive(:clear)
388
+ allow(blackjack).to receive(:draw_hands)
389
+ allow(blackjack).to receive(:normalize_current_bet)
390
+ end
391
+
392
+ it 'clears the screen' do
393
+ blackjack.new_bet
394
+ expect(blackjack).to have_received(:clear)
395
+ end
396
+
397
+ it 'draws hands' do
398
+ blackjack.new_bet
399
+ expect(blackjack).to have_received(:draw_hands)
400
+ end
401
+
402
+ it 'draws the current bet' do
403
+ blackjack.new_bet
404
+ expected = " Current Bet: $5.00\n"
405
+ expect(blackjack).to have_received(:puts).with(expected)
406
+ end
407
+
408
+ it 'updates current bet' do
409
+ blackjack.new_bet
410
+ expect(blackjack.current_bet).to eq(1000)
411
+ end
412
+
413
+ it 'normalizes the bet' do
414
+ blackjack.new_bet
415
+ expect(blackjack).to have_received(:normalize_current_bet)
416
+ end
417
+
418
+ it 'deals a new hand' do
419
+ blackjack.new_bet
420
+ expect(blackjack).to have_received(:deal_new_hand)
421
+ end
422
+ end
423
+
424
+ describe '#draw_game_options' do
425
+ before do
426
+ blackjack.dealer_hand = dealer_hand
427
+ end
428
+
429
+ context 'when updating number of decks' do
430
+ before do
431
+ allow(described_class).to receive(:getc).and_return('n')
432
+ allow(blackjack).to receive(:clear)
433
+ allow(blackjack).to receive(:draw_hands)
434
+ allow(blackjack).to receive(:new_num_decks)
435
+ allow(blackjack).to receive(:puts)
436
+ end
437
+
438
+ it 'draws the blackjack options' do
439
+ blackjack.draw_game_options
440
+ expected = ' (N) Number of Decks (T) Deck Type (B) Back'
441
+ expect(blackjack).to have_received(:puts).with(expected)
442
+ end
443
+
444
+ it 'clears the screen' do
445
+ blackjack.draw_game_options
446
+ expect(blackjack).to have_received(:clear)
447
+ end
448
+
449
+ it 'draws the hands' do
450
+ blackjack.draw_game_options
451
+ expect(blackjack).to have_received(:draw_hands)
452
+ end
453
+
454
+ it 'updates number of decks' do
455
+ blackjack.draw_game_options
456
+ expect(blackjack).to have_received(:new_num_decks)
457
+ end
458
+ end
459
+
460
+ context 'when updating the deck type' do
461
+ before do
462
+ allow(described_class).to receive(:getc).and_return('t')
463
+ allow(blackjack).to receive(:clear)
464
+ allow(blackjack).to receive(:draw_hands)
465
+ allow(blackjack).to receive(:new_deck_type)
466
+ allow(blackjack).to receive(:draw_bet_options)
467
+ allow(blackjack).to receive(:puts)
468
+ end
469
+
470
+ it 'clears the screen' do
471
+ blackjack.draw_game_options
472
+ expect(blackjack).to have_received(:clear).twice
473
+ end
474
+
475
+ it 'draws the hands' do
476
+ blackjack.draw_game_options
477
+ expect(blackjack).to have_received(:draw_hands).twice
478
+ end
479
+
480
+ it 'updates deck type' do
481
+ blackjack.draw_game_options
482
+ expect(blackjack).to have_received(:new_deck_type)
483
+ end
484
+
485
+ it 'draws the bet options' do
486
+ blackjack.draw_game_options
487
+ expect(blackjack).to have_received(:draw_bet_options)
488
+ end
489
+ end
490
+
491
+ context 'when going back to previous menu' do
492
+ before do
493
+ allow(described_class).to receive(:getc).and_return('b')
494
+ allow(blackjack).to receive(:clear)
495
+ allow(blackjack).to receive(:draw_hands)
496
+ allow(blackjack).to receive(:draw_bet_options)
497
+ allow(blackjack).to receive(:puts)
498
+ end
499
+
500
+ it 'clears the screen' do
501
+ blackjack.draw_game_options
502
+ expect(blackjack).to have_received(:clear)
503
+ end
504
+
505
+ it 'draws the hands' do
506
+ blackjack.draw_game_options
507
+ expect(blackjack).to have_received(:draw_hands)
508
+ end
509
+
510
+ it 'draws the bet options' do
511
+ blackjack.draw_game_options
512
+ expect(blackjack).to have_received(:draw_bet_options)
513
+ end
514
+ end
515
+
516
+ context 'when invalid input' do
517
+ before do
518
+ allow(described_class).to receive(:getc).and_return('x', 'b')
519
+ allow(blackjack).to receive(:puts)
520
+ allow(blackjack).to receive(:new_bet)
521
+ allow(blackjack).to receive(:draw_bet_options)
522
+ allow(blackjack).to receive(:draw_hands)
523
+ end
524
+
525
+ it 'gets the action again' do
526
+ blackjack.draw_game_options
527
+ allow(blackjack).to receive(:draw_game_options)
528
+ expect(blackjack).to have_received(:draw_bet_options).twice
529
+ end
530
+ end
531
+ end
532
+
533
+ describe '#new_num_decks' do
534
+ before do
535
+ blackjack.dealer_hand = dealer_hand
536
+ allow(STDIN).to receive(:gets).and_return('2')
537
+ allow(described_class).to receive(:getc).and_return('b', 'q')
538
+ allow(blackjack).to receive(:print)
539
+ allow(blackjack).to receive(:puts)
540
+ allow(blackjack).to receive(:clear)
541
+ allow(blackjack).to receive(:draw_hands)
542
+ allow(blackjack).to receive(:draw_game_options)
543
+ allow(blackjack).to receive(:normalize_num_decks)
544
+ end
545
+
546
+ it 'clears screen' do
547
+ blackjack.new_num_decks
548
+ expect(blackjack).to have_received(:clear)
549
+ end
550
+
551
+ it 'draws hands' do
552
+ blackjack.new_num_decks
553
+ expect(blackjack).to have_received(:draw_hands)
554
+ end
555
+
556
+ it 'gets new number of decks' do
557
+ blackjack.new_num_decks
558
+ expect(blackjack.num_decks).to eq(2)
559
+ end
560
+
561
+ it 'normalizes number of decks' do
562
+ blackjack.new_num_decks
563
+ expect(blackjack).to have_received(:normalize_num_decks)
564
+ end
565
+ end
566
+
567
+ describe '#normalize_num_decks' do
568
+ it 'increases the value' do
569
+ blackjack.num_decks = 0
570
+ blackjack.normalize_num_decks
571
+ expect(blackjack.num_decks).to eq(1)
572
+ end
573
+
574
+ it 'decreases the value' do
575
+ blackjack.num_decks = 9
576
+ blackjack.normalize_num_decks
577
+ expect(blackjack.num_decks).to eq(8)
578
+ end
579
+
580
+ it 'leaves the value unaltered' do
581
+ blackjack.num_decks = 2
582
+ blackjack.normalize_num_decks
583
+ expect(blackjack.num_decks).to eq(2)
584
+ end
585
+ end
586
+
587
+ describe '#new_deck_type' do
588
+ before do
589
+ blackjack.dealer_hand = dealer_hand
590
+ allow(blackjack).to receive(:puts)
591
+ end
592
+
593
+ context 'when choosing a new deck type' do
594
+ it 'draws options' do
595
+ allow(described_class).to receive(:getc).and_return('1')
596
+ blackjack.new_deck_type
597
+ expected = ' (1) Regular (2) Aces (3) Jacks (4) Aces & Jacks (5) Sevens (6) Eights'
598
+ expect(blackjack).to have_received(:puts).with(expected)
599
+ end
600
+
601
+ it 'builds a new regular' do
602
+ allow(described_class).to receive(:getc).and_return('1')
603
+ allow(shoe).to receive(:new_regular)
604
+ blackjack.new_deck_type
605
+ expect(shoe).to have_received(:new_regular)
606
+ end
607
+
608
+ it 'builds a new aces' do
609
+ allow(described_class).to receive(:getc).and_return('2')
610
+ allow(shoe).to receive(:new_aces)
611
+ blackjack.new_deck_type
612
+ expect(shoe).to have_received(:new_aces)
613
+ end
614
+
615
+ it 'builds a new jacks' do
616
+ allow(described_class).to receive(:getc).and_return('3')
617
+ allow(shoe).to receive(:new_jacks)
618
+ blackjack.new_deck_type
619
+ expect(shoe).to have_received(:new_jacks)
620
+ end
621
+
622
+ it 'builds a new aces_jacks' do
623
+ allow(described_class).to receive(:getc).and_return('4')
624
+ allow(shoe).to receive(:new_aces_jacks)
625
+ blackjack.new_deck_type
626
+ expect(shoe).to have_received(:new_aces_jacks)
627
+ end
628
+
629
+ it 'builds a new sevens' do
630
+ allow(described_class).to receive(:getc).and_return('5')
631
+ allow(shoe).to receive(:new_sevens)
632
+ blackjack.new_deck_type
633
+ expect(shoe).to have_received(:new_sevens)
634
+ end
635
+
636
+ it 'builds a new eights' do
637
+ allow(described_class).to receive(:getc).and_return('6')
638
+ allow(shoe).to receive(:new_eights)
639
+ blackjack.new_deck_type
640
+ expect(shoe).to have_received(:new_eights)
641
+ end
642
+ end
643
+
644
+ context 'when invalid input' do
645
+ it 'gets the action again' do
646
+ allow(described_class).to receive(:getc).and_return('x', '1')
647
+ allow(blackjack).to receive(:draw_hands)
648
+ blackjack.new_deck_type
649
+ expect(blackjack).to have_received(:draw_hands)
650
+ end
651
+ end
652
+ end
653
+
654
+ describe '#ask_insurance' do
655
+ before do
656
+ # blackjack.dealer_hand = dealer_hand
657
+ allow(blackjack).to receive(:puts)
658
+ allow(blackjack).to receive(:insure_hand)
659
+ allow(blackjack).to receive(:no_insurance)
660
+ end
661
+
662
+ context 'when choosing to take insurance' do
663
+ it 'draws options' do
664
+ allow(described_class).to receive(:getc).and_return('y')
665
+ blackjack.ask_insurance
666
+ expected = ' Insurance? (Y) Yes (N) No'
667
+ expect(blackjack).to have_received(:puts).with(expected)
668
+ end
669
+
670
+ it 'insures hand' do
671
+ allow(described_class).to receive(:getc).and_return('y')
672
+ blackjack.ask_insurance
673
+ expect(blackjack).to have_received(:insure_hand)
674
+ end
675
+
676
+ it 'does not insure hand' do
677
+ allow(described_class).to receive(:getc).and_return('n')
678
+ blackjack.ask_insurance
679
+ expect(blackjack).to have_received(:no_insurance)
680
+ end
681
+ end
682
+
683
+ context 'when invalid input' do
684
+ it 'asks again' do
685
+ allow(described_class).to receive(:getc).and_return('x', 'y')
686
+ allow(blackjack).to receive(:draw_hands)
687
+ blackjack.ask_insurance
688
+ expect(blackjack).to have_received(:draw_hands)
689
+ end
690
+ end
691
+ end
692
+
693
+ describe '#insure_hand' do
694
+ before do
695
+ blackjack.dealer_hand = dealer_hand
696
+ blackjack.player_hands << player_hand
697
+ allow(blackjack).to receive(:draw_hands)
698
+ allow(blackjack).to receive(:draw_bet_options)
699
+ end
700
+
701
+ it 'reduces hand bet' do
702
+ blackjack.insure_hand
703
+ expect(player_hand.bet).to eq(250)
704
+ end
705
+
706
+ it 'sets hand as played' do
707
+ blackjack.insure_hand
708
+ expect(player_hand.played).to be_truthy
709
+ end
710
+
711
+ it 'sets hand as payed' do
712
+ blackjack.insure_hand
713
+ expect(player_hand.payed).to be_truthy
714
+ end
715
+
716
+ it 'sets hand status as LOST' do
717
+ blackjack.insure_hand
718
+ expect(player_hand.status).to eq(LOST)
719
+ end
720
+
721
+ it 'updates blackjack money' do
722
+ blackjack.insure_hand
723
+ expect(blackjack.money).to eq(9750)
724
+ end
725
+
726
+ it 'draws hands' do
727
+ blackjack.insure_hand
728
+ expect(blackjack).to have_received(:draw_hands)
729
+ end
730
+
731
+ it 'draws bet options' do
732
+ blackjack.insure_hand
733
+ expect(blackjack).to have_received(:draw_bet_options)
734
+ end
735
+ end
736
+
737
+ describe '#no_insurance' do
738
+ before do
739
+ blackjack.dealer_hand = dealer_hand
740
+ blackjack.player_hands << player_hand
741
+ allow(player_hand).to receive(:action?)
742
+ allow(blackjack).to receive(:draw_bet_options)
743
+ allow(blackjack).to receive(:draw_hands)
744
+ allow(blackjack).to receive(:pay_hands)
745
+ end
746
+
747
+ context 'when dealer hand is blackjack' do
748
+ before do
749
+ dealer_hand.cards << ace << ten
750
+ end
751
+
752
+ it 'shows dealer down card' do
753
+ blackjack.no_insurance
754
+ expect(dealer_hand.hide_down_card).to be_falsey
755
+ end
756
+
757
+ it 'pays hands' do
758
+ blackjack.no_insurance
759
+ expect(blackjack).to have_received(:pay_hands)
760
+ end
761
+
762
+ it 'draws hands' do
763
+ blackjack.no_insurance
764
+ expect(blackjack).to have_received(:draw_hands)
765
+ end
766
+
767
+ it 'draws bet options' do
768
+ blackjack.no_insurance
769
+ expect(blackjack).to have_received(:draw_bet_options)
770
+ end
771
+ end
772
+
773
+ context 'when dealer hand is not blackjack' do
774
+ before do
775
+ dealer_hand.cards << ace << seven
776
+ allow(blackjack).to receive(:play_dealer_hand)
777
+ end
778
+
779
+ it 'plays dealer hand' do
780
+ allow(player_hand).to receive(:done?).and_return(true)
781
+ blackjack.no_insurance
782
+ expect(blackjack).to have_received(:play_dealer_hand)
783
+ end
784
+
785
+ it 'draws hands' do
786
+ allow(player_hand).to receive(:done?).and_return(false)
787
+ blackjack.no_insurance
788
+ expect(blackjack).to have_received(:draw_hands)
789
+ end
790
+
791
+ it 'gets player hand action' do
792
+ allow(player_hand).to receive(:done?).and_return(false)
793
+ blackjack.no_insurance
794
+ expect(player_hand).to have_received(:action?)
795
+ end
796
+ end
797
+ end
798
+
799
+ describe '#play_dealer_hand' do
800
+ before do
801
+ blackjack.dealer_hand = dealer_hand
802
+ allow(dealer_hand).to receive(:play)
803
+ allow(blackjack).to receive(:draw_bet_options)
804
+ allow(blackjack).to receive(:draw_hands)
805
+ end
806
+
807
+ it 'plays dealer hand' do
808
+ blackjack.play_dealer_hand
809
+ expect(dealer_hand).to have_received(:play)
810
+ end
811
+
812
+ it 'draws bet options' do
813
+ blackjack.play_dealer_hand
814
+ expect(blackjack).to have_received(:draw_bet_options)
815
+ end
816
+
817
+ it 'draws hands' do
818
+ blackjack.play_dealer_hand
819
+ expect(blackjack).to have_received(:draw_hands)
820
+ end
821
+ end
822
+
823
+ describe '#split_current_hand' do
824
+ before do
825
+ blackjack.dealer_hand = dealer_hand
826
+ blackjack.player_hands << player_hand
827
+ allow(blackjack).to receive(:current_player_hand).and_return(player_hand)
828
+ end
829
+
830
+ context 'when current hand can split' do
831
+ before do
832
+ player_hand.cards << six << six
833
+ allow(described_class).to receive(:getc).and_return('s', 's')
834
+ allow(blackjack).to receive(:draw_bet_options)
835
+ allow(blackjack).to receive(:draw_hands)
836
+ allow(player_hand).to receive(:action?)
837
+ end
838
+
839
+ it 'splits hand' do
840
+ blackjack.split_current_hand
841
+ expect(blackjack.player_hands.size).to eq(2)
842
+ end
843
+
844
+ it 'first hand is done' do
845
+ allow(player_hand).to receive(:done?).and_return(true)
846
+ blackjack.split_current_hand
847
+ expect(player_hand).to have_received(:done?).twice
848
+ end
849
+ end
850
+
851
+ context 'when current hand cannot split' do
852
+ before do
853
+ player_hand.cards << ace << six
854
+ allow(described_class).to receive(:getc).and_return('s')
855
+ allow(blackjack).to receive(:draw_bet_options)
856
+ allow(blackjack).to receive(:draw_hands)
857
+ allow(player_hand).to receive(:action?)
858
+ end
859
+
860
+ it 'draws hands' do
861
+ blackjack.split_current_hand
862
+ expect(blackjack).to have_received(:draw_hands)
863
+ end
864
+
865
+ it 'gets player hand action' do
866
+ blackjack.split_current_hand
867
+ expect(player_hand).to have_received(:action?)
868
+ end
869
+ end
870
+ end
871
+ end