class Blackjack::Game

Constants

MINIMUM_BET_SIZE

Attributes

dealer_hand[R]
player_hands[R]

Public Class Methods

new(player, bet, shoe = Shoe.new) click to toggle source
# File lib/blackjack/game.rb, line 11
def initialize(player, bet, shoe = Shoe.new)
  @player = player
  @shoe = shoe

  @player.withdraw(bet)

  @player_hands = [ Hand::PlayerHand.new(player, bet, [ @shoe.take, @shoe.take ]) ]
  @dealer_hand = Hand::DealerHand.new([ @shoe.take, @shoe.take(open: false) ])

  @state = :initial

  check_on_blackjack
end

Public Instance Methods

act(action, hand_index) click to toggle source
# File lib/blackjack/game.rb, line 29
def act(action, hand_index)
  raise Errors::UnableToAction.new('Game is over') if over?
  raise Errors::UnableToAction.new('Invalid action') unless valid_action?(action)

  hand = @player_hands[hand_index]

  raise Errors::UnableToAction.new('Unknown hand') if hand.nil?

  self.send(action, hand)

  if ready_to_finish?
    @state = :over

    unless all_hands_busted?
      @dealer_hand.open_last_card

      fill_dealer_hand_until_17

      @player_hands.each do |hand|
        if hand > @dealer_hand
          @player.reward(hand.bet * 2)
        else hand == @dealer_hand
          @player.reward(hand.bet)
        end
      end
    end
  end
end
over?() click to toggle source
# File lib/blackjack/game.rb, line 25
def over?
  @state == :over
end

Private Instance Methods

all_hands_busted?() click to toggle source
# File lib/blackjack/game.rb, line 60
def all_hands_busted?
  @player_hands
    .map(&:busted?)
    .reduce { |product, is_busted| product && is_busted }
end
check_on_blackjack() click to toggle source
# File lib/blackjack/game.rb, line 106
def check_on_blackjack
  first_hand = @player_hands.first

  if first_hand.blackjack?
    @state = :over
    @player.reward(first_hand.bet * 1.5)
  end
end
double(hand) click to toggle source
# File lib/blackjack/game.rb, line 90
def double(hand)
  fail unless hand.can_double?

  @player.withdraw(hand.bet)
  hand.double_bet
  hand.append(@shoe.take)
  hand.stand unless hand.busted?
end
fill_dealer_hand_until_17() click to toggle source
# File lib/blackjack/game.rb, line 66
def fill_dealer_hand_until_17
  until @dealer_hand.value >= 17
    @dealer_hand << @shoe.take
  end
end
hit(hand) click to toggle source
# File lib/blackjack/game.rb, line 84
def hit(hand)
  fail unless hand.can_hit?

  hand.append(@shoe.take)
end
ready_to_finish?() click to toggle source
# File lib/blackjack/game.rb, line 72
def ready_to_finish?
  !@player_hands
    .map(&:playable?)
    .reduce { |product, is_playable| product || is_playable }
end
split(hand) click to toggle source
# File lib/blackjack/game.rb, line 99
def split(hand)
  fail unless hand.can_split?

  @player_hands << hand.split(@shoe.take, @shoe.take)
  @player.withdraw(hand.bet)
end
stand(hand) click to toggle source
# File lib/blackjack/game.rb, line 78
def stand(hand)
  fail unless hand.can_stand?

  hand.stand
end
valid_action?(action) click to toggle source
# File lib/blackjack/game.rb, line 115
def valid_action?(action)
  [ :hit, :stand, :double, :split ].include?(action)
end