Class: Blackjack

Inherits:
Object
  • Object
show all
Includes:
Menus, SplitHand, Utils
Defined in:
lib/blackjack.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#clear_draw_hands, #clear_draw_hands_ask_insurance, #clear_draw_hands_bet_options, #clear_draw_hands_game_options, #clear_draw_hands_new_deck_type, #clear_draw_hands_new_face_type, #clear_draw_hands_new_num_decks, #draw_hands_current_hand_action, #load_game, #play_dealer_hand, #save_game

Methods included from SplitHand

#expand_split_hands, #split_current_hand, #split_hand

Methods included from Menus

#ask_insurance, #draw_bet_options, #draw_game_options, #new_deck_type, #new_face_type

Constructor Details

#initializeBlackjack

Returns a new instance of Blackjack.



23
24
25
26
27
28
29
# File 'lib/blackjack.rb', line 23

def initialize
  @num_decks = 1
  @face_type = 1
  @deck_type = 1
  @money = 10_000
  @current_bet = 500
end

Instance Attribute Details

#current_betObject

Returns the value of attribute current_bet.



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

def current_bet
  @current_bet
end

#current_handObject

Returns the value of attribute current_hand.



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

def current_hand
  @current_hand
end

#dealer_handObject

Returns the value of attribute dealer_hand.



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

def dealer_hand
  @dealer_hand
end

#deck_typeObject

Returns the value of attribute deck_type.



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

def deck_type
  @deck_type
end

#face_typeObject

Returns the value of attribute face_type.



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

def face_type
  @face_type
end

#moneyObject

Returns the value of attribute money.



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

def money
  @money
end

#num_decksObject

Returns the value of attribute num_decks.



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

def num_decks
  @num_decks
end

#player_handsObject

Returns the value of attribute player_hands.



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

def player_hands
  @player_hands
end

#shoeObject

Returns the value of attribute shoe.



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

def shoe
  @shoe
end

Class Method Details

.getc(input) ⇒ Object



207
208
209
210
211
212
213
214
215
# File 'lib/blackjack.rb', line 207

def self.getc(input)
  begin
    system('stty raw -echo')
    c = input.getc
  ensure
    system('stty -raw echo')
  end
  c.chr
end

Instance Method Details

#all_betsObject



193
194
195
# File 'lib/blackjack.rb', line 193

def all_bets
  player_hands.inject(0) { |sum, player_hand| sum + player_hand.bet }
end

#build_new_handObject



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

def build_new_hand
  self.player_hands = []
  player_hand = PlayerHand.new(self, current_bet)
  player_hands << player_hand
  self.current_hand = 0

  self.dealer_hand = DealerHand.new(self)

  2.times do
    player_hand.deal_card
    dealer_hand.deal_card
  end
  player_hand
end

#clearObject



109
110
111
112
113
# File 'lib/blackjack.rb', line 109

def clear
  return if ENV['CLEAR_TERM'] == '0'

  system('export TERM=linux; clear')
end

#current_player_handObject



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

def current_player_hand
  player_hands[current_hand]
end

#deal_new_handObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/blackjack.rb', line 56

def deal_new_hand
  shoe.send("new_#{SHOES[deck_type]}") if shoe.needs_to_shuffle?
  player_hand = build_new_hand

  if dealer_hand.upcard_is_ace? && !player_hand.blackjack?
    draw_hands
    ask_insurance
  elsif player_hand.done?
    dealer_hand.hide_first_card = false
    pay_hands
    draw_hands
    draw_bet_options
  else
    draw_hands
    player_hand.action?
    save_game
  end
end

#draw_handsObject



115
116
117
118
119
120
121
122
123
124
# File 'lib/blackjack.rb', line 115

def draw_hands
  clear
  out = String.new
  out << "\n Dealer:\n#{dealer_hand.draw}\n"
  out << "\n Player $"
  out << Format.money(money / 100.0)
  out << ":\n"
  out << draw_player_hands
  puts out
end

#draw_player_handsObject



126
127
128
129
130
131
132
# File 'lib/blackjack.rb', line 126

def draw_player_hands
  out = String.new('')
  player_hands.each_with_index do |player_hand, index|
    out << player_hand.draw(index)
  end
  out
end

#insure_handObject



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/blackjack.rb', line 161

def insure_hand
  player_hand = current_player_hand
  player_hand.bet /= 2
  player_hand.played = true
  player_hand.paid = true
  player_hand.status = :lost

  self.money -= player_hand.bet

  draw_hands
  draw_bet_options
end

#more_hands_to_play?Boolean

Returns:

  • (Boolean)


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

def more_hands_to_play?
  current_hand < player_hands.size - 1
end

#need_to_play_dealer_hand?Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
# File 'lib/blackjack.rb', line 90

def need_to_play_dealer_hand?
  player_hands.each do |player_hand|
    return true unless player_hand.busted? || player_hand.blackjack?
  end
  false
end

#new_bet(input) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/blackjack.rb', line 134

def new_bet(input)
  clear
  draw_hands

  puts " Current Bet: $#{Format.money(current_bet / 100)}\n"
  print ' Enter New Bet: $'

  self.current_bet = input.gets.to_i * 100

  normalize_current_bet
  deal_new_hand
end

#new_num_decks(input) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/blackjack.rb', line 147

def new_num_decks(input)
  puts " Number Of Decks: #{num_decks}"
  print ' New Number Of Decks (1-8): '
  self.num_decks = input.gets.to_i

  normalize_num_decks
  clear_draw_hands_game_options
end

#no_insuranceObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/blackjack.rb', line 174

def no_insurance
  if dealer_hand.blackjack?
    dealer_hand.hide_first_card = false

    pay_hands
    draw_hands
    draw_bet_options
  else
    player_hand = current_player_hand

    if player_hand.done?
      play_dealer_hand
    else
      draw_hands
      player_hand.action?
    end
  end
end

#normalize_current_betObject



197
198
199
200
201
202
203
204
205
# File 'lib/blackjack.rb', line 197

def normalize_current_bet
  if current_bet < MIN_BET
    self.current_bet = MIN_BET
  elsif current_bet > MAX_BET
    self.current_bet = MAX_BET
  end

  self.current_bet = money if current_bet > money
end

#normalize_num_decksObject



156
157
158
159
# File 'lib/blackjack.rb', line 156

def normalize_num_decks
  self.num_decks = 1 if num_decks < 1
  self.num_decks = 8 if num_decks > 8
end

#pay_handsObject



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/blackjack.rb', line 97

def pay_hands
  dealer_hand_value = dealer_hand.value(:soft)
  dealer_busted = dealer_hand.busted?

  player_hands.each do |player_hand|
    player_hand.pay(dealer_hand_value, dealer_busted)
  end

  normalize_current_bet
  save_game
end

#play_more_handsObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/blackjack.rb', line 79

def play_more_hands
  self.current_hand += 1
  current_player_hand.deal_card

  if current_player_hand.done?
    current_player_hand.process
  else
    draw_hands_current_hand_action
  end
end

#runObject



31
32
33
34
35
# File 'lib/blackjack.rb', line 31

def run
  load_game
  @shoe = Shoe.new(self, num_decks)
  deal_new_hand
end