class Ventiuna::Game

Public Class Methods

new(players, shoe_size: 8, min_bet: 10, max_bet: 500, max_rounds: 1000) click to toggle source
# File lib/ventiuna/game.rb, line 3
def initialize(players, shoe_size: 8, min_bet: 10, max_bet: 500, max_rounds: 1000)
        @dealer = Ventiuna::Dealer.new("Dealer")
        @shoe_size = shoe_size
        @shoe = Ventiuna::Shoe.new(@shoe_size).shuffle
        @players = Array(players)
        @losers = []
        @min_bet = min_bet
        @max_bet = max_bet
        @max_rounds = max_rounds
        @rounds_count = 0
end

Public Instance Methods

play() click to toggle source
# File lib/ventiuna/game.rb, line 15
def play
        remove_dead_beats
        while @rounds_count < @max_rounds
                #puts @rounds_count
                puts "********************** NEW DEAL ******************************"
                start_round

                @rounds_count += 1
                remove_dead_beats
                break if @players.empty?
        end
        print_stats
end

Private Instance Methods

build_hands() click to toggle source
# File lib/ventiuna/game.rb, line 87
def build_hands
        @players.each do |player|
                while player.hand
                        player.hand << @shoe.pop if player.hand.cards.size == 1

                        #puts ""
                        puts "Hit/Stand/Double/SPlit:"
                        puts player.hand.to_s

                        if player.hand.bust?
                                player.hand.stand
                                next
                        end

                        case player.decision(@dealer.hand.cards.first)
                        when "h"
                                puts "HIT"
                                player.hand << @shoe.pop
                                build_hands
                        when "s"
                                puts "STAND"
                                player.hand.stand
                        when "d"
                                if player.hand.cards.size == 2
                                        puts "DOUBLE"
                                        player.hand << @shoe.pop
                                        puts player.hand.to_s
                                        player.hand.stand
                                else
                                        puts "Can't double down on this hand!"
                                        build_hands
                                end
                        when "sp"
                                if player.hand.pair? && player.hands.count == 1
                                        puts "SPLIT"
                                        #$stdin.gets
                                        if player.hand.cards.first.ace? && player.hand.cards.last.ace?
                                                player.hands = player.hand.split
                                                player.hands.each do |h|
                                                        h << @shoe.pop
                                                        puts h.to_s
                                                        h.stand
                                                end
                                        else
                                                player.hands = player.hand.split
                                        end
                                else
                                        puts "Can't split this hand!"
                                end
                                build_hands
                        end
                end
        end
end
deal() click to toggle source
# File lib/ventiuna/game.rb, line 63
def deal
        2.times do
                (@players + [@dealer]).each do |player|
                        player.hand << @shoe.pop
                end
        end
end
dealer_draw() click to toggle source
# File lib/ventiuna/game.rb, line 142
def dealer_draw
        return false if @players.collect{ |p| p.hands }.flatten.all?{ |h| h.bust? }

        while @dealer.hand && !@dealer.hand.bust?
                case @dealer.decision
                when "h"
                        @dealer.hand << @shoe.pop
                        dealer_draw
                when "s"
                        @dealer.hand.stand
                end
        end
end
determine_outcome(player_hand) click to toggle source
# File lib/ventiuna/game.rb, line 173
def determine_outcome(player_hand)
        if player_hand.value == @dealer.hands.first.value
                "tie"
        elsif player_hand.blackjack?
                "blackjack"
        elsif player_hand.bust?
                "bust"
        elsif @dealer.hands.first.bust? && !player_hand.bust?
                "win"
        elsif player_hand.value > @dealer.hands.first.value
                "win"
        elsif player_hand.value < @dealer.hands.first.value
                "lose"
        end
end
end_round() click to toggle source
# File lib/ventiuna/game.rb, line 167
def end_round
        print_hands(print_dealers_full_hand: true)
        payout
        record_moves!
end
payout() click to toggle source
# File lib/ventiuna/game.rb, line 245
def payout
        @players.each do |player|
                player.hands.each do |hand|
                        outcome = determine_outcome(hand)
                        puts outcome.upcase
                        #record_moves(player, hand, outcome)
                        case outcome
                        when "blackjack"
                                player.balance += (player.bet * 2.5)
                                player.wins    += 1
                                @dealer.loses  += 1
                        when "win"
                                player.balance += (player.bet * 2)
                                player.wins    += 1
                                @dealer.loses  += 1
                        when "lose", "bust"
                                player.bet      = 0
                                player.loses   += 1
                                @dealer.wins   += 1
                        when "tie"
                                player.balance += player.bet
                                player.ties    += 1
                                @dealer.ties   += 1
                        end
                end
        end
end
place_bets() click to toggle source
# File lib/ventiuna/game.rb, line 57
def place_bets()
        @players.each do |player|
                player.place_bet(@min_bet, @max_bet)
        end
end
print_hands(print_dealers_full_hand: false) click to toggle source
print_stats() click to toggle source
record_moves(player, hand, outcome) click to toggle source
# File lib/ventiuna/game.rb, line 225
def record_moves(player, hand, outcome)
        last_move = player.decisions[hand].last
        return false unless last_move
        value = 1
        value = 2 if last_move.decision == "d"

        puts outcome.upcase
        case outcome
        when "win", "blackjack"
                last_move.win += value
        when "bust"
                last_move.bust += value
        when "lose"
                last_move.lose += value
        when "tie"
                last_move.tie += 1
        end
        last_move.save
end
record_moves!() click to toggle source
# File lib/ventiuna/game.rb, line 193
def record_moves!()
        #puts "record moves!!!!"
        @players.each do |player|
                #puts player
                #puts player.decisions
                player.decisions.each do |hand, moves|
                        #puts hand
                        #puts moves
                        last_move = moves.pop
                        if last_move.decision == "sp"
                                puts "split"
                                puts hand
                                puts moves.inspect
                                puts player.decisions.keys
                                #$stdin.gets
                                # record split
                                next
                        end
                        #puts last_move.decision
                        $stdin.gets if last_move.decision == "sp"
                        outcome = determine_outcome(hand)

                        last_move.update_counts(outcome)

                        moves.each do |move|
                                next if move.decision == "sp"
                                move.update_counts("progress")
                        end
                end
        end
end
record_split(player, hand) click to toggle source
# File lib/ventiuna/game.rb, line 189
def record_split(player, hand)

end
remove_dead_beats() click to toggle source
# File lib/ventiuna/game.rb, line 43
def remove_dead_beats
        @players.each_with_index do |player, i|
                @losers << @players.delete_at(i) if player.balance < @min_bet
        end
end
setup() click to toggle source
# File lib/ventiuna/game.rb, line 49
def setup
        @shoe = Ventiuna::Shoe.new(@shoe_size).shuffle if @shoe.needs_shuffling?
        (@players + [@dealer]).each do |player|
                player.hands = [Ventiuna::Hand.new]
                player.reset_decisions
        end
end
start_round() click to toggle source
# File lib/ventiuna/game.rb, line 156
def start_round
        setup
        place_bets
        deal
        print_hands
        return end_round if @dealer.hand.blackjack?
        build_hands
        dealer_draw
        end_round
end