console-blackjack 1.0.0 → 1.0.5

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.
@@ -2,6 +2,15 @@
2
2
 
3
3
  require_relative 'card'
4
4
 
5
+ SHOES = {
6
+ 1 => :regular,
7
+ 2 => :aces,
8
+ 3 => :jacks,
9
+ 4 => :aces_jacks,
10
+ 5 => :sevens,
11
+ 6 => :eights
12
+ }.freeze
13
+
5
14
  class Shoe
6
15
  attr_accessor :num_decks, :cards
7
16
 
@@ -17,11 +26,7 @@ class Shoe
17
26
  cards_dealt = total_cards - cards.size
18
27
  used = cards_dealt / total_cards.to_f * 100.0
19
28
 
20
- Shoe.shuffle_specs.each do |spec|
21
- return true if used > spec.first && num_decks == spec.last
22
- end
23
-
24
- false
29
+ used > Shoe.shuffle_specs[num_decks - 1]
25
30
  end
26
31
 
27
32
  def shuffle
@@ -31,9 +36,9 @@ class Shoe
31
36
  def new_regular
32
37
  self.cards = []
33
38
  num_decks.times do
34
- (0..3).each do |suite_value|
39
+ (0..3).each do |suit_value|
35
40
  (0..12).each do |value|
36
- cards << Card.new(value, suite_value)
41
+ cards << Card.new(value, suit_value)
37
42
  end
38
43
  end
39
44
  end
@@ -43,8 +48,8 @@ class Shoe
43
48
  def new_aces
44
49
  self.cards = []
45
50
  (num_decks * 10).times do
46
- (0..3).each do |suite_value|
47
- cards << Card.new(0, suite_value)
51
+ (0..3).each do |suit_value|
52
+ cards << Card.new(0, suit_value)
48
53
  end
49
54
  end
50
55
  shuffle
@@ -53,8 +58,8 @@ class Shoe
53
58
  def new_jacks
54
59
  self.cards = []
55
60
  (num_decks * 10).times do
56
- (0..3).each do |suite_value|
57
- cards << Card.new(10, suite_value)
61
+ (0..3).each do |suit_value|
62
+ cards << Card.new(10, suit_value)
58
63
  end
59
64
  end
60
65
  shuffle
@@ -63,9 +68,9 @@ class Shoe
63
68
  def new_aces_jacks
64
69
  self.cards = []
65
70
  (num_decks * 10).times do
66
- (0..3).each do |suite_value|
67
- cards << Card.new(0, suite_value)
68
- cards << Card.new(10, suite_value)
71
+ (0..3).each do |suit_value|
72
+ cards << Card.new(0, suit_value)
73
+ cards << Card.new(10, suit_value)
69
74
  end
70
75
  end
71
76
  shuffle
@@ -74,8 +79,8 @@ class Shoe
74
79
  def new_sevens
75
80
  self.cards = []
76
81
  (num_decks * 10).times do
77
- (0..3).each do |suite_value|
78
- cards << Card.new(6, suite_value)
82
+ (0..3).each do |suit_value|
83
+ cards << Card.new(6, suit_value)
79
84
  end
80
85
  end
81
86
  shuffle
@@ -84,8 +89,8 @@ class Shoe
84
89
  def new_eights
85
90
  self.cards = []
86
91
  (num_decks * 10).times do
87
- (0..3).each do |suite_value|
88
- cards << Card.new(7, suite_value)
92
+ (0..3).each do |suit_value|
93
+ cards << Card.new(7, suit_value)
89
94
  end
90
95
  end
91
96
  shuffle
@@ -96,13 +101,6 @@ class Shoe
96
101
  end
97
102
 
98
103
  def self.shuffle_specs
99
- [[95, 8],
100
- [92, 7],
101
- [89, 6],
102
- [86, 5],
103
- [84, 4],
104
- [82, 3],
105
- [81, 2],
106
- [80, 1]]
104
+ [80, 81, 82, 84, 86, 89, 92, 95]
107
105
  end
108
106
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SplitHand
4
+ def split_current_hand
5
+ if current_player_hand.can_split?
6
+ expand_split_hands
7
+ this_hand = split_hand
8
+
9
+ if this_hand.done?
10
+ this_hand.process
11
+ else
12
+ draw_hands_current_hand_action
13
+ end
14
+ else
15
+ draw_hands_current_hand_action
16
+ end
17
+ end
18
+
19
+ def split_hand
20
+ this_hand = player_hands[current_hand]
21
+ split_hand = player_hands[current_hand + 1]
22
+
23
+ split_hand.cards = []
24
+ split_hand.cards << this_hand.cards.last
25
+ this_hand.cards.pop
26
+
27
+ this_hand.cards << shoe.next_card
28
+ this_hand
29
+ end
30
+
31
+ def expand_split_hands
32
+ player_hands << PlayerHand.new(self, current_bet)
33
+
34
+ x = player_hands.size - 1
35
+ while x > current_hand
36
+ player_hands[x] = player_hands[x - 1].clone
37
+ x -= 1
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Utils
4
+ def save_game
5
+ File.open(SAVE_FILE, 'w') do |file|
6
+ file.puts "#{num_decks}|#{money}|#{current_bet}"
7
+ end
8
+ end
9
+
10
+ def load_game
11
+ return unless File.readable?(SAVE_FILE)
12
+
13
+ a = File.read(SAVE_FILE).split('|')
14
+ self.num_decks = a[0].to_i
15
+ self.money = a[1].to_i
16
+ self.current_bet = a[2].to_i
17
+ end
18
+
19
+ def clear_draw_hands
20
+ clear
21
+ draw_hands
22
+ end
23
+
24
+ def clear_draw_hands_new_num_decks
25
+ clear_draw_hands
26
+ new_num_decks
27
+ end
28
+
29
+ def clear_draw_hands_new_deck_type
30
+ clear_draw_hands
31
+ new_deck_type
32
+ end
33
+
34
+ def clear_draw_hands_ask_insurance
35
+ clear_draw_hands
36
+ ask_insurance
37
+ end
38
+
39
+ def clear_draw_hands_bet_options
40
+ clear_draw_hands
41
+ draw_bet_options
42
+ end
43
+
44
+ def clear_draw_hands_game_options
45
+ clear_draw_hands
46
+ draw_game_options
47
+ end
48
+
49
+ def draw_hands_current_hand_action
50
+ draw_hands
51
+ current_player_hand.action?
52
+ end
53
+
54
+ def play_dealer_hand
55
+ dealer_hand.play
56
+ draw_hands
57
+ draw_bet_options
58
+ end
59
+ end
@@ -3,7 +3,7 @@
3
3
  FactoryBot.define do
4
4
  factory :card do
5
5
  value { 0 }
6
- suite { 0 }
6
+ suit { 0 }
7
7
 
8
8
  trait :ace do
9
9
  value { 0 }
@@ -37,6 +37,6 @@ FactoryBot.define do
37
37
  value { 9 }
38
38
  end
39
39
 
40
- initialize_with { new(value, suite) }
40
+ initialize_with { new(value, suit) }
41
41
  end
42
42
  end
@@ -1,3 +1,4 @@
1
+ # coding: utf-8
1
2
  # frozen_string_literal: true
2
3
 
3
4
  RSpec.describe Card do
@@ -12,8 +13,8 @@ RSpec.describe Card do
12
13
  expect(card.value).to eq(0)
13
14
  end
14
15
 
15
- it 'has a suite' do
16
- expect(card.suite).to eq(0)
16
+ it 'has a suit' do
17
+ expect(card.suit).to eq(0)
17
18
  end
18
19
  end
19
20
 
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Format do
4
+ describe '#money' do
5
+ it 'returns a formatted money string' do
6
+ str = described_class.money(1)
7
+ expect(str).to eq('1.00')
8
+ end
9
+ end
10
+ end
@@ -17,9 +17,9 @@ RSpec.describe Hand do
17
17
 
18
18
  describe '#deal_card' do
19
19
  it 'adds a card to the hand' do
20
- expect {
20
+ expect do
21
21
  hand.deal_card
22
- }.to change { hand.cards.size }.by(1)
22
+ end.to change { hand.cards.size }.by(1)
23
23
  end
24
24
  end
25
25
 
@@ -1,4 +1,3 @@
1
- # coding: utf-8
2
1
  # frozen_string_literal: true
3
2
 
4
3
  RSpec.describe PlayerHand do
@@ -35,6 +34,35 @@ RSpec.describe PlayerHand do
35
34
  end
36
35
  end
37
36
 
37
+ describe '#draw' do
38
+ it 'draws the hand' do
39
+ player_hand.cards << ace << ten
40
+ expected = " 🂡 🂪 ⇒ 21 $5.00 \n\n"
41
+ expect(player_hand.draw(1)).to eq(expected)
42
+ end
43
+
44
+ it 'draws a lost hand' do
45
+ player_hand.cards << ace << ace
46
+ player_hand.status = LOST
47
+ expected = " 🂡 🂡 ⇒ 12 -$5.00 Lose!\n\n"
48
+ expect(player_hand.draw(1)).to eq(expected)
49
+ end
50
+
51
+ it 'draws a won hand' do
52
+ player_hand.cards << ace << ace
53
+ player_hand.status = WON
54
+ expected = " 🂡 🂡 ⇒ 12 +$5.00 Won!\n\n"
55
+ expect(player_hand.draw(1)).to eq(expected)
56
+ end
57
+
58
+ it 'draws a push hand' do
59
+ player_hand.cards << ace << ace
60
+ player_hand.status = PUSH
61
+ expected = " 🂡 🂡 ⇒ 12 $5.00 Push\n\n"
62
+ expect(player_hand.draw(1)).to eq(expected)
63
+ end
64
+ end
65
+
38
66
  describe '#busted?' do
39
67
  it 'returns false' do
40
68
  expect(player_hand).to_not be_busted
@@ -273,35 +301,6 @@ RSpec.describe PlayerHand do
273
301
  end
274
302
  end
275
303
 
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
304
  describe '#process' do
306
305
  context 'with more hands to play' do
307
306
  before do
@@ -157,8 +157,8 @@ RSpec.describe Shoe do
157
157
  end
158
158
 
159
159
  describe '.shuffle_specs' do
160
- it 'returns when to shuffle' do
161
- expect(described_class.shuffle_specs[0]).to eq([95, 8])
160
+ it 'returns spec for when to shuffle' do
161
+ expect(described_class.shuffle_specs).to eq([80, 81, 82, 84, 86, 89, 92, 95])
162
162
  end
163
163
  end
164
164
  end
@@ -36,13 +36,6 @@ RSpec.describe Blackjack do
36
36
  end
37
37
  end
38
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
39
  describe '.all_bets?' do
47
40
  it 'returns 10' do
48
41
  blackjack.player_hands << player_hand << player_hand
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.0
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Donald
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-09 00:00:00.000000000 Z
11
+ date: 2020-05-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Blackjack for your console, full version.
14
14
  email: gdonald@gmail.com
@@ -29,9 +29,15 @@ files:
29
29
  - lib/blackjack.rb
30
30
  - lib/blackjack/card.rb
31
31
  - lib/blackjack/dealer_hand.rb
32
+ - lib/blackjack/format.rb
32
33
  - lib/blackjack/hand.rb
34
+ - lib/blackjack/menus.rb
33
35
  - lib/blackjack/player_hand.rb
36
+ - lib/blackjack/player_hand_actions.rb
37
+ - lib/blackjack/player_hand_draw.rb
34
38
  - lib/blackjack/shoe.rb
39
+ - lib/blackjack/split_hand.rb
40
+ - lib/blackjack/utils.rb
35
41
  - spec/factories/blackjack_factory.rb
36
42
  - spec/factories/card_factory.rb
37
43
  - spec/factories/dealer_hand_factory.rb
@@ -40,6 +46,7 @@ files:
40
46
  - spec/factories/shoe_factory.rb
41
47
  - spec/lib/blackjack/card_spec.rb
42
48
  - spec/lib/blackjack/dealer_hand_spec.rb
49
+ - spec/lib/blackjack/format_spec.rb
43
50
  - spec/lib/blackjack/hand_spec.rb
44
51
  - spec/lib/blackjack/player_hand_spec.rb
45
52
  - spec/lib/blackjack/shoe_spec.rb
@@ -73,3 +80,4 @@ signing_key:
73
80
  specification_version: 4
74
81
  summary: Console Blackjack
75
82
  test_files: []
83
+ ...