class Blackjack

Attributes

current_bet[RW]
current_hand[RW]
dealer_hand[RW]
money[RW]
num_decks[RW]
player_hands[RW]
shoe[RW]

Public Class Methods

getc() click to toggle source
# File lib/blackjack.rb, line 204
def self.getc
  begin
    system('stty raw -echo')
    c = $stdin.getc
  ensure
    system('stty -raw echo')
  end
  c.chr
end
new() click to toggle source
# File lib/blackjack.rb, line 22
def initialize
  @num_decks = 1
  @money = 10_000
  @current_bet = 500
end

Public Instance Methods

all_bets() click to toggle source
# File lib/blackjack.rb, line 190
def all_bets
  player_hands.inject(0) { |sum, player_hand| sum + player_hand.bet }
end
build_new_hand() click to toggle source
# File lib/blackjack.rb, line 38
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
clear() click to toggle source
# File lib/blackjack.rb, line 106
def clear
  return if ENV['CLEAR_TERM'] == '0'

  system('export TERM=linux; clear')
end
current_player_hand() click to toggle source
# File lib/blackjack.rb, line 34
def current_player_hand
  player_hands[current_hand]
end
deal_new_hand() click to toggle source
# File lib/blackjack.rb, line 53
def deal_new_hand
  shoe.new_regular 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_down_card = false
    pay_hands
    draw_hands
    draw_bet_options
  else
    draw_hands
    player_hand.action?
    save_game
  end
end
draw_hands() click to toggle source
# File lib/blackjack.rb, line 112
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_hands() click to toggle source
# File lib/blackjack.rb, line 123
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_hand() click to toggle source
# File lib/blackjack.rb, line 158
def insure_hand
  player_hand = current_player_hand
  player_hand.bet /= 2
  player_hand.played = true
  player_hand.payed = true
  player_hand.status = LOST

  self.money -= player_hand.bet

  draw_hands
  draw_bet_options
end
more_hands_to_play?() click to toggle source
# File lib/blackjack.rb, line 72
def more_hands_to_play?
  current_hand < player_hands.size - 1
end
need_to_play_dealer_hand?() click to toggle source
# File lib/blackjack.rb, line 87
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() click to toggle source
# File lib/blackjack.rb, line 131
def new_bet
  clear
  draw_hands

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

  self.current_bet = $stdin.gets.to_i * 100

  normalize_current_bet
  deal_new_hand
end
new_num_decks() click to toggle source
# File lib/blackjack.rb, line 144
def new_num_decks
  puts " Number Of Decks: #{num_decks}"
  print ' New Number Of Decks (1-8): '
  self.num_decks = $stdin.gets.to_i

  normalize_num_decks
  clear_draw_hands_game_options
end
no_insurance() click to toggle source
# File lib/blackjack.rb, line 171
def no_insurance
  if dealer_hand.blackjack?
    dealer_hand.hide_down_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_bet() click to toggle source
# File lib/blackjack.rb, line 194
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_decks() click to toggle source
# File lib/blackjack.rb, line 153
def normalize_num_decks
  self.num_decks = 1 if num_decks < 1
  self.num_decks = 8 if num_decks > 8
end
pay_hands() click to toggle source
# File lib/blackjack.rb, line 94
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_hands() click to toggle source
# File lib/blackjack.rb, line 76
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
run() click to toggle source
# File lib/blackjack.rb, line 28
def run
  load_game
  @shoe = Shoe.new(num_decks)
  deal_new_hand
end