class Ventiuna::Hand

Attributes

cards[RW]

Public Class Methods

new(card=[], *other_cards) click to toggle source
# File lib/ventiuna/hand.rb, line 5
def initialize(card=[], *other_cards)
        @cards = Array(card) + other_cards
        @soft = false
        @active = true
end

Public Instance Methods

<<(another_card) click to toggle source
# File lib/ventiuna/hand.rb, line 11
def << (another_card)
        self.cards << another_card
end
Also aliased as: hit
active?() click to toggle source
# File lib/ventiuna/hand.rb, line 36
def active?
        @active
end
blackjack?() click to toggle source
# File lib/ventiuna/hand.rb, line 40
def blackjack?
        self.value == 21 && self.cards.size == 2
end
bust?() click to toggle source
# File lib/ventiuna/hand.rb, line 44
def bust?
        self.value > 21 #&& !self.soft?
end
hit(another_card)
Alias for: <<
pair?() click to toggle source
# File lib/ventiuna/hand.rb, line 48
def pair?
        self.cards.size == 2 && (self.cards.first.value == self.cards.last.value)
end
soft?() click to toggle source
# File lib/ventiuna/hand.rb, line 52
def soft?
        self.value
        @soft
end
split() click to toggle source
# File lib/ventiuna/hand.rb, line 20
def split
        return false unless self.pair?
        [Ventiuna::Hand.new(self.cards.first), Ventiuna::Hand.new(self.cards.last)]
end
stand() click to toggle source
# File lib/ventiuna/hand.rb, line 16
def stand
        @active = false
end
to_db() click to toggle source
# File lib/ventiuna/hand.rb, line 61
def to_db
        s = "#{value}"
        s += "I" if cards.size == 2
        s += "S" if soft?
        s += "P" if pair?
        s
end
to_s() click to toggle source
# File lib/ventiuna/hand.rb, line 57
def to_s
        "#{self.cards.collect{|c| c.to_s}} = #{self.value}"
end
value() click to toggle source
# File lib/ventiuna/hand.rb, line 25
def value
        sum = 0
        self.cards.sort.each_with_index do |card, i|
                # hit low if haven't reached 11 or there are more cards left
                hit_low = (sum >= 11) || (i+1 != self.cards.size)
                @soft = card.ace? && !hit_low
                sum += card.value(low: hit_low)
        end
        sum
end