Class: Shoe

Inherits:
Object
  • Object
show all
Defined in:
lib/blackjack/shoe.rb

Constant Summary collapse

CARDS_PER_DECK =
52

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blackjack, num_decks) ⇒ Shoe

Returns a new instance of Shoe.



19
20
21
22
23
# File 'lib/blackjack/shoe.rb', line 19

def initialize(blackjack, num_decks)
  @blackjack = blackjack
  @num_decks = num_decks
  @cards = []
end

Instance Attribute Details

#blackjackObject

Returns the value of attribute blackjack.



17
18
19
# File 'lib/blackjack/shoe.rb', line 17

def blackjack
  @blackjack
end

#cardsObject

Returns the value of attribute cards.



17
18
19
# File 'lib/blackjack/shoe.rb', line 17

def cards
  @cards
end

#num_decksObject

Returns the value of attribute num_decks.



17
18
19
# File 'lib/blackjack/shoe.rb', line 17

def num_decks
  @num_decks
end

Class Method Details

.shuffle_specsObject



84
85
86
# File 'lib/blackjack/shoe.rb', line 84

def self.shuffle_specs
  [80, 81, 82, 84, 86, 89, 92, 95]
end

Instance Method Details

#build_deck(values = []) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/blackjack/shoe.rb', line 42

def build_deck(values = [])
  self.cards = []
  while cards.count < total_cards
    4.times do |suit|
      next if cards.count >= total_cards

      values.each do |value|
        cards << Card.new(blackjack, value, suit)
      end
    end
  end
  shuffle
end

#needs_to_shuffle?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
# File 'lib/blackjack/shoe.rb', line 25

def needs_to_shuffle?
  return true if cards.empty?

  cards_dealt = total_cards - cards.size
  used = cards_dealt / total_cards.to_f * 100.0

  used > Shoe.shuffle_specs[num_decks - 1]
end

#new_acesObject



60
61
62
# File 'lib/blackjack/shoe.rb', line 60

def new_aces
  build_deck([0])
end

#new_aces_jacksObject



68
69
70
# File 'lib/blackjack/shoe.rb', line 68

def new_aces_jacks
  build_deck([0, 10])
end

#new_eightsObject



76
77
78
# File 'lib/blackjack/shoe.rb', line 76

def new_eights
  build_deck([7])
end

#new_jacksObject



64
65
66
# File 'lib/blackjack/shoe.rb', line 64

def new_jacks
  build_deck([10])
end

#new_regularObject



56
57
58
# File 'lib/blackjack/shoe.rb', line 56

def new_regular
  build_deck((0..12).to_a)
end

#new_sevensObject



72
73
74
# File 'lib/blackjack/shoe.rb', line 72

def new_sevens
  build_deck([6])
end

#next_cardObject



80
81
82
# File 'lib/blackjack/shoe.rb', line 80

def next_card
  cards.shift
end

#shuffleObject



34
35
36
# File 'lib/blackjack/shoe.rb', line 34

def shuffle
  7.times { cards.shuffle! }
end

#total_cardsObject



38
39
40
# File 'lib/blackjack/shoe.rb', line 38

def total_cards
  @total_cards ||= num_decks * CARDS_PER_DECK
end