class Ventiuna::Player

Attributes

balance[RW]
bet[RW]
decisions[R]
hands[RW]
loses[RW]
name[RW]
ties[RW]
wins[RW]

Public Class Methods

new(name, strategy: "Learning", balance: 1000000.0) click to toggle source
# File lib/ventiuna/player.rb, line 6
def initialize(name, strategy: "Learning", balance: 1000000.0)
        @name = name
        @strategy = Ventiuna::Strategy.find_or_create_by_name(strategy)
        @balance = balance
        @hands = [Ventiuna::Hand.new]
        @bet = 0
        @wins = 0
        @loses = 0
        @ties = 0
        @decisions = Hash.new([])
end

Public Instance Methods

active_hand() click to toggle source
# File lib/ventiuna/player.rb, line 46
def active_hand
        hands.select{ |h| h.active? }.first
end
Also aliased as: hand
decision(dealers_card, strategy: "interactive") click to toggle source
# File lib/ventiuna/player.rb, line 56
def decision(dealers_card, strategy: "interactive")
        define_posible_moves(dealers_card)
        can_split = hands.size == 1
        #d = @strategy.decision(dealers_card, hand, can_split)
        d = nil
        
        unless d
                d = "s"
                case strategy
                when "random"
                        d = random_strategy
                when "interactive"
                        d = interactive
                end
        end
        move = Ventiuna::Move.find_by_strategy_id_and_dealers_card_value_and_hand_and_decision(@strategy.id, dealers_card.value, hand.to_db, d)
        #puts move.inspect
        @decisions[hand] = decisions[hand] << move
        #puts @decisions
        d
end
hand()
Alias for: active_hand
loses_percent() click to toggle source
# File lib/ventiuna/player.rb, line 27
def loses_percent
        return 0 if total_rounds == 0
        (self.loses.to_f/self.total_rounds) * 100
end
place_bet(min_bet, max_bet) click to toggle source
# File lib/ventiuna/player.rb, line 41
def place_bet(min_bet, max_bet)
        @bet = min_bet
        @balance -= @bet
end
reset_decisions() click to toggle source
# File lib/ventiuna/player.rb, line 78
def reset_decisions
        @decisions = Hash.new([])
end
reset_hand()
Alias for: reset_hands
reset_hands() click to toggle source
# File lib/ventiuna/player.rb, line 51
def reset_hands
        @hands = [Ventiuna::Hand.new]
end
Also aliased as: reset_hand
stats() click to toggle source
# File lib/ventiuna/player.rb, line 18
def stats
        "Wins: #{self.wins} (#{self.wins_percent.round}%),    Loses: #{self.loses} (#{self.loses_percent.round}%),    Ties: #{self.ties} (#{self.ties_percent.round}%)"
end
ties_percent() click to toggle source
# File lib/ventiuna/player.rb, line 32
def ties_percent
        return 0 if total_rounds == 0
        (self.ties.to_f/self.total_rounds) * 100
end
total_rounds() click to toggle source
# File lib/ventiuna/player.rb, line 37
def total_rounds
        @wins+@loses+@ties
end
wins_percent() click to toggle source
# File lib/ventiuna/player.rb, line 22
def wins_percent
        return 0 if total_rounds == 0
        (self.wins.to_f/self.total_rounds) * 100
end

Private Instance Methods

define_posible_moves(dealers_card) click to toggle source
# File lib/ventiuna/player.rb, line 94
def define_posible_moves(dealers_card)
        posible_moves(dealers_card).each do |m|
                Ventiuna::Move.find_or_create_by_strategy_id_and_dealers_card_value_and_hand_and_decision(@strategy.id, dealers_card.value, hand.to_db, m).save
        end
end
interactive() click to toggle source
# File lib/ventiuna/player.rb, line 108
def interactive
        choice = $stdin.gets.to_s.strip

        case choice
        when /^hit$|^h$/i
                "h"
        when /^stand$|^s$/i
                "s"
        when /^double$|^d$/i
                "d"
        when /^split$|^sp$/i
                "sp"
        else
                puts "invalid choice!"
                interactive()
        end
                
end
posible_moves(dealers_card) click to toggle source
# File lib/ventiuna/player.rb, line 83
def posible_moves(dealers_card)
        moves_list = ["s"]
        return moves_list if active_hand.bust?

        moves_list.unshift("h") if active_hand.soft? || active_hand.value < 21
        moves_list.unshift("sp") if active_hand.pair?
        moves_list.unshift("d") if active_hand.cards.size == 2
        
        moves_list
end
random_strategy() click to toggle source
# File lib/ventiuna/player.rb, line 100
def random_strategy
        return "s" if self.active_hand.bust?
        return "s" if self.active_hand.value == 21 || self.active_hand.blackjack?
        return ["s", "h", "d", "sp"].shuffle.pop if self.active_hand.pair?
        return ["s", "h", "d"].shuffle.pop if self.active_hand.cards.size == 2
        ["s", "h"].shuffle.pop
end