console-blackjack 1.0.7 → 1.1.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.
@@ -1,14 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  RSpec.describe Blackjack do
4
- let(:shoe) { build(:shoe, :new_regular) }
5
- let(:blackjack) { build(:blackjack, shoe: shoe) }
4
+ let(:blackjack) { build(:blackjack, shoe: build(:shoe, :new_regular)) }
6
5
  let(:player_hand) { build(:player_hand, blackjack: blackjack) }
7
6
  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) }
7
+ let(:ace) { build(:card, :ace, blackjack: blackjack) }
8
+ let(:ten) { build(:card, :ten, blackjack: blackjack) }
12
9
 
13
10
  describe '#current_player_hand' do
14
11
  it 'returns the current hand' do
@@ -30,7 +27,7 @@ RSpec.describe Blackjack do
30
27
 
31
28
  describe '#getc' do
32
29
  it 'get a single character from stdin' do
33
- allow(STDIN).to receive(:getc).and_return('q')
30
+ allow($stdin).to receive(:getc).and_return('q')
34
31
  c = described_class.getc
35
32
  expect(c).to eq('q')
36
33
  end
@@ -44,13 +41,24 @@ RSpec.describe Blackjack do
44
41
  end
45
42
 
46
43
  describe '#need_to_play_dealer_hand?' do
47
- it 'returns false' do
48
- expect(blackjack).to_not be_need_to_play_dealer_hand
44
+ before do
45
+ blackjack.player_hands << player_hand
49
46
  end
50
47
 
51
- it 'returns true' do
52
- blackjack.player_hands << player_hand
53
- expect(blackjack).to be_need_to_play_dealer_hand
48
+ context 'when busted' do
49
+ it 'returns false' do
50
+ allow(player_hand).to receive(:blackjack?).and_return(false)
51
+ allow(player_hand).to receive(:busted?).and_return(true)
52
+ expect(blackjack).to_not be_need_to_play_dealer_hand
53
+ end
54
+ end
55
+
56
+ context 'when blackjack' do
57
+ it 'returns false' do
58
+ allow(player_hand).to receive(:busted?).and_return(false)
59
+ allow(player_hand).to receive(:blackjack?).and_return(true)
60
+ expect(blackjack).to_not be_need_to_play_dealer_hand
61
+ end
54
62
  end
55
63
  end
56
64
 
@@ -93,37 +101,56 @@ RSpec.describe Blackjack do
93
101
 
94
102
  describe '#save_game' do
95
103
  let(:file) { instance_double('File') }
96
- let(:content) { "#{blackjack.num_decks}|#{blackjack.money}|#{blackjack.current_bet}" }
97
104
 
98
- it 'opens and put save file data' do
105
+ before do
99
106
  allow(File).to receive(:open).with(SAVE_FILE, 'w').and_yield(file)
100
107
  allow(file).to receive(:puts)
108
+ end
109
+
110
+ it 'opens and put save file data' do
101
111
  blackjack.save_game
102
- expect(file).to have_received(:puts).with(content)
112
+ fields = %i[num_decks deck_type face_type money current_bet]
113
+ expected = fields.map { |f| blackjack.send(f) }.join('|')
114
+ expect(file).to have_received(:puts).with(expected)
103
115
  end
104
116
  end
105
117
 
106
118
  describe '#load_game' do
107
- let(:content) { '8|2000|1000' }
119
+ context 'with a unreadable save file' do
120
+ it 'fails to load save file' do
121
+ allow(File).to receive(:read).with(SAVE_FILE)
122
+ allow(File).to receive(:readable?).with(SAVE_FILE).and_return(false)
108
123
 
109
- before do
110
- allow(File).to receive(:readable?).with(SAVE_FILE).and_return(true)
111
- allow(File).to receive(:read).with(SAVE_FILE).and_return(content)
124
+ blackjack.load_game
125
+ expect(File).to_not have_received(:read).with(SAVE_FILE)
126
+ end
112
127
  end
113
128
 
114
- it 'loads num_decks from save file data' do
115
- blackjack.load_game
116
- expect(blackjack.num_decks).to eq(8)
117
- end
129
+ context 'with a readabale save file' do
130
+ before do
131
+ allow(File).to receive(:readable?).with(SAVE_FILE).and_return(true)
132
+ allow(File).to receive(:read).with(SAVE_FILE).and_return('8|1|1|2000|1000')
133
+ end
118
134
 
119
- it 'loads money from save file data' do
120
- blackjack.load_game
121
- expect(blackjack.money).to eq(2000)
122
- end
135
+ it 'loads num_decks from save file data' do
136
+ blackjack.load_game
137
+ expect(blackjack.num_decks).to eq(8)
138
+ end
123
139
 
124
- it 'loads current_bet from save file data' do
125
- blackjack.load_game
126
- expect(blackjack.current_bet).to eq(1000)
140
+ it 'loads face_type from save file data' do
141
+ blackjack.load_game
142
+ expect(blackjack.face_type).to eq(1)
143
+ end
144
+
145
+ it 'loads money from save file data' do
146
+ blackjack.load_game
147
+ expect(blackjack.money).to eq(2000)
148
+ end
149
+
150
+ it 'loads current_bet from save file data' do
151
+ blackjack.load_game
152
+ expect(blackjack.current_bet).to eq(1000)
153
+ end
127
154
  end
128
155
  end
129
156
 
@@ -257,19 +284,19 @@ RSpec.describe Blackjack do
257
284
  dealer_hand.cards << ten << ten
258
285
  allow(blackjack).to receive(:build_new_hand).and_return(player_hand)
259
286
  allow(player_hand).to receive(:action?)
260
- allow(shoe).to receive(:new_regular)
287
+ allow(blackjack.shoe).to receive(:new_regular)
261
288
  end
262
289
 
263
290
  it 'shuffles' do
264
- allow(shoe).to receive(:needs_to_shuffle?).and_return(true)
291
+ allow(blackjack.shoe).to receive(:needs_to_shuffle?).and_return(true)
265
292
  blackjack.deal_new_hand
266
- expect(shoe).to have_received(:new_regular)
293
+ expect(blackjack.shoe).to have_received(:new_regular)
267
294
  end
268
295
 
269
296
  it 'does not shuffle' do
270
- allow(shoe).to receive(:needs_to_shuffle?).and_return(false)
297
+ allow(blackjack.shoe).to receive(:needs_to_shuffle?).and_return(false)
271
298
  blackjack.deal_new_hand
272
- expect(shoe).to_not have_received(:new_regular)
299
+ expect(blackjack.shoe).to_not have_received(:new_regular)
273
300
  end
274
301
  end
275
302
 
@@ -372,7 +399,7 @@ RSpec.describe Blackjack do
372
399
  describe '#new_bet' do
373
400
  before do
374
401
  blackjack.dealer_hand = dealer_hand
375
- allow(STDIN).to receive(:gets).and_return('10')
402
+ allow($stdin).to receive(:gets).and_return('10')
376
403
  allow(described_class).to receive(:getc).and_return('s', 'q')
377
404
  allow(blackjack).to receive(:print)
378
405
  allow(blackjack).to receive(:puts)
@@ -430,7 +457,7 @@ RSpec.describe Blackjack do
430
457
 
431
458
  it 'draws the blackjack options' do
432
459
  blackjack.draw_game_options
433
- expected = ' (N) Number of Decks (T) Deck Type (B) Back'
460
+ expected = ' (N) Number of Decks (T) Deck Type (F) Face Type (B) Back'
434
461
  expect(blackjack).to have_received(:puts).with(expected)
435
462
  end
436
463
 
@@ -481,6 +508,37 @@ RSpec.describe Blackjack do
481
508
  end
482
509
  end
483
510
 
511
+ context 'when updating the face type' do
512
+ before do
513
+ allow(described_class).to receive(:getc).and_return('f')
514
+ allow(blackjack).to receive(:clear)
515
+ allow(blackjack).to receive(:draw_hands)
516
+ allow(blackjack).to receive(:new_face_type)
517
+ allow(blackjack).to receive(:draw_bet_options)
518
+ allow(blackjack).to receive(:puts)
519
+ end
520
+
521
+ it 'clears the screen' do
522
+ blackjack.draw_game_options
523
+ expect(blackjack).to have_received(:clear).twice
524
+ end
525
+
526
+ it 'draws the hands' do
527
+ blackjack.draw_game_options
528
+ expect(blackjack).to have_received(:draw_hands).twice
529
+ end
530
+
531
+ it 'updates face type' do
532
+ blackjack.draw_game_options
533
+ expect(blackjack).to have_received(:new_face_type)
534
+ end
535
+
536
+ it 'draws the bet options' do
537
+ blackjack.draw_game_options
538
+ expect(blackjack).to have_received(:draw_bet_options)
539
+ end
540
+ end
541
+
484
542
  context 'when going back to previous menu' do
485
543
  before do
486
544
  allow(described_class).to receive(:getc).and_return('b')
@@ -526,7 +584,7 @@ RSpec.describe Blackjack do
526
584
  describe '#new_num_decks' do
527
585
  before do
528
586
  blackjack.dealer_hand = dealer_hand
529
- allow(STDIN).to receive(:gets).and_return('2')
587
+ allow($stdin).to receive(:gets).and_return('2')
530
588
  allow(described_class).to receive(:getc).and_return('b', 'q')
531
589
  allow(blackjack).to receive(:print)
532
590
  allow(blackjack).to receive(:puts)
@@ -581,6 +639,7 @@ RSpec.describe Blackjack do
581
639
  before do
582
640
  blackjack.dealer_hand = dealer_hand
583
641
  allow(blackjack).to receive(:puts)
642
+ allow(blackjack).to receive(:save_game)
584
643
  end
585
644
 
586
645
  context 'when choosing a new deck type' do
@@ -593,44 +652,50 @@ RSpec.describe Blackjack do
593
652
 
594
653
  it 'builds a new regular' do
595
654
  allow(described_class).to receive(:getc).and_return('1')
596
- allow(shoe).to receive(:new_regular)
655
+ allow(blackjack.shoe).to receive(:new_regular)
597
656
  blackjack.new_deck_type
598
- expect(shoe).to have_received(:new_regular)
657
+ expect(blackjack.shoe).to have_received(:new_regular)
658
+ expect(blackjack).to have_received(:save_game)
599
659
  end
600
660
 
601
661
  it 'builds a new aces' do
602
662
  allow(described_class).to receive(:getc).and_return('2')
603
- allow(shoe).to receive(:new_aces)
663
+ allow(blackjack.shoe).to receive(:new_aces)
604
664
  blackjack.new_deck_type
605
- expect(shoe).to have_received(:new_aces)
665
+ expect(blackjack.shoe).to have_received(:new_aces)
666
+ expect(blackjack).to have_received(:save_game)
606
667
  end
607
668
 
608
669
  it 'builds a new jacks' do
609
670
  allow(described_class).to receive(:getc).and_return('3')
610
- allow(shoe).to receive(:new_jacks)
671
+ allow(blackjack.shoe).to receive(:new_jacks)
611
672
  blackjack.new_deck_type
612
- expect(shoe).to have_received(:new_jacks)
673
+ expect(blackjack.shoe).to have_received(:new_jacks)
674
+ expect(blackjack).to have_received(:save_game)
613
675
  end
614
676
 
615
677
  it 'builds a new aces_jacks' do
616
678
  allow(described_class).to receive(:getc).and_return('4')
617
- allow(shoe).to receive(:new_aces_jacks)
679
+ allow(blackjack.shoe).to receive(:new_aces_jacks)
618
680
  blackjack.new_deck_type
619
- expect(shoe).to have_received(:new_aces_jacks)
681
+ expect(blackjack.shoe).to have_received(:new_aces_jacks)
682
+ expect(blackjack).to have_received(:save_game)
620
683
  end
621
684
 
622
685
  it 'builds a new sevens' do
623
686
  allow(described_class).to receive(:getc).and_return('5')
624
- allow(shoe).to receive(:new_sevens)
687
+ allow(blackjack.shoe).to receive(:new_sevens)
625
688
  blackjack.new_deck_type
626
- expect(shoe).to have_received(:new_sevens)
689
+ expect(blackjack.shoe).to have_received(:new_sevens)
690
+ expect(blackjack).to have_received(:save_game)
627
691
  end
628
692
 
629
693
  it 'builds a new eights' do
630
694
  allow(described_class).to receive(:getc).and_return('6')
631
- allow(shoe).to receive(:new_eights)
695
+ allow(blackjack.shoe).to receive(:new_eights)
632
696
  blackjack.new_deck_type
633
- expect(shoe).to have_received(:new_eights)
697
+ expect(blackjack.shoe).to have_received(:new_eights)
698
+ expect(blackjack).to have_received(:save_game)
634
699
  end
635
700
  end
636
701
 
@@ -640,13 +705,56 @@ RSpec.describe Blackjack do
640
705
  allow(blackjack).to receive(:draw_hands)
641
706
  blackjack.new_deck_type
642
707
  expect(blackjack).to have_received(:draw_hands)
708
+ expect(blackjack).to have_received(:save_game).twice
709
+ end
710
+ end
711
+ end
712
+
713
+ describe '#new_face_type' do
714
+ before do
715
+ blackjack.dealer_hand = dealer_hand
716
+ allow(blackjack).to receive(:puts)
717
+ end
718
+
719
+ context 'when choosing a new face type' do
720
+ it 'draws options' do
721
+ allow(described_class).to receive(:getc).and_return('1')
722
+ blackjack.new_face_type
723
+ expected = ' (1) 🂡 (2) A♠'
724
+ expect(blackjack).to have_received(:puts).with(expected)
725
+ end
726
+
727
+ it 'sets regular faces' do
728
+ allow(described_class).to receive(:getc).and_return('1')
729
+ allow(blackjack).to receive(:save_game)
730
+ allow(blackjack).to receive(:face_type=).with(1)
731
+ blackjack.new_face_type
732
+ expect(blackjack).to have_received(:face_type=).with(1)
733
+ expect(blackjack).to have_received(:save_game)
734
+ end
735
+
736
+ it 'sets alternate faces' do
737
+ allow(described_class).to receive(:getc).and_return('2')
738
+ allow(blackjack).to receive(:save_game)
739
+ allow(blackjack).to receive(:face_type=).with(2)
740
+ blackjack.new_face_type
741
+ expect(blackjack).to have_received(:face_type=).with(2)
742
+ expect(blackjack).to have_received(:save_game)
743
+ end
744
+ end
745
+
746
+ context 'when invalid input' do
747
+ it 'gets the action again' do
748
+ allow(described_class).to receive(:getc).and_return('x', '1')
749
+ allow(blackjack).to receive(:draw_hands)
750
+ blackjack.new_face_type
751
+ expect(blackjack).to have_received(:draw_hands)
643
752
  end
644
753
  end
645
754
  end
646
755
 
647
756
  describe '#ask_insurance' do
648
757
  before do
649
- # blackjack.dealer_hand = dealer_hand
650
758
  allow(blackjack).to receive(:puts)
651
759
  allow(blackjack).to receive(:insure_hand)
652
760
  allow(blackjack).to receive(:no_insurance)
@@ -765,7 +873,7 @@ RSpec.describe Blackjack do
765
873
 
766
874
  context 'when dealer hand is not blackjack' do
767
875
  before do
768
- dealer_hand.cards << ace << seven
876
+ dealer_hand.cards << ace << build(:card, :seven)
769
877
  allow(blackjack).to receive(:play_dealer_hand)
770
878
  end
771
879
 
@@ -822,7 +930,7 @@ RSpec.describe Blackjack do
822
930
 
823
931
  context 'when current hand can split' do
824
932
  before do
825
- player_hand.cards << six << six
933
+ player_hand.cards << build(:card, :six) << build(:card, :six)
826
934
  allow(described_class).to receive(:getc).and_return('s', 's')
827
935
  allow(blackjack).to receive(:draw_bet_options)
828
936
  allow(blackjack).to receive(:draw_hands)
@@ -843,7 +951,7 @@ RSpec.describe Blackjack do
843
951
 
844
952
  context 'when current hand cannot split' do
845
953
  before do
846
- player_hand.cards << ace << six
954
+ player_hand.cards << ace << build(:card, :six)
847
955
  allow(described_class).to receive(:getc).and_return('s')
848
956
  allow(blackjack).to receive(:draw_bet_options)
849
957
  allow(blackjack).to receive(:draw_hands)
data/spec/spec_helper.rb CHANGED
@@ -3,14 +3,13 @@
3
3
  require 'simplecov'
4
4
  SimpleCov.start do
5
5
  add_filter %r{/spec/}
6
+ enable_coverage :branch
7
+ primary_coverage :branch
6
8
  end
7
9
 
8
- require 'coveralls'
9
- Coveralls.wear!
10
-
11
10
  Dir.glob(File.join(File.dirname(__FILE__),
12
- '..' + File::SEPARATOR + 'lib', '**',
13
- '*.rb'), &method(:require))
11
+ "..#{File::SEPARATOR}lib", '**',
12
+ '*.rb')).each(&method(:require))
14
13
 
15
14
  require 'factory_bot'
16
15
  require 'pry'
data/ss1.png ADDED
Binary file
data/ss2.png ADDED
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: console-blackjack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.7
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Donald
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-02 00:00:00.000000000 Z
11
+ date: 2022-03-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Blackjack for your console, full version.
14
14
  email: gdonald@gmail.com
@@ -23,8 +23,10 @@ files:
23
23
  - README.md
24
24
  - Rakefile
25
25
  - bin/console-blackjack
26
- - bj.png
27
26
  - bj.txt
27
+ - console-blackjack-1.0.7.gem
28
+ - console-blackjack-1.0.8.gem
29
+ - console-blackjack-1.0.9.gem
28
30
  - console-blackjack.gemspec
29
31
  - lib/blackjack.rb
30
32
  - lib/blackjack/card.rb
@@ -52,11 +54,14 @@ files:
52
54
  - spec/lib/blackjack/shoe_spec.rb
53
55
  - spec/lib/blackjack_spec.rb
54
56
  - spec/spec_helper.rb
57
+ - ss1.png
58
+ - ss2.png
55
59
  homepage: https://github.com/gdonald/console-blackjack-ruby
56
60
  licenses:
57
61
  - MIT
58
62
  metadata:
59
63
  source_code_uri: https://github.com/gdonald/console-blackjack-ruby
64
+ rubygems_mfa_required: 'true'
60
65
  post_install_message: |2+
61
66
 
62
67
  Type `console-blackjack` to run!
@@ -68,14 +73,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
68
73
  requirements:
69
74
  - - ">="
70
75
  - !ruby/object:Gem::Version
71
- version: '0'
76
+ version: '3.0'
72
77
  required_rubygems_version: !ruby/object:Gem::Requirement
73
78
  requirements:
74
79
  - - ">="
75
80
  - !ruby/object:Gem::Version
76
81
  version: '0'
77
82
  requirements: []
78
- rubygems_version: 3.1.6
83
+ rubygems_version: 3.2.32
79
84
  signing_key:
80
85
  specification_version: 4
81
86
  summary: Console Blackjack