# Copyright 2006 Michael Lewis # Distributed under the terms of the GNU General Public License v2 class Hand < Array def dirtythis @dirty = true end def initialize @dirty = true end def copy( o ) @dirty = true @cached_value = nil @cached_soft = nil replace o end def add_card( newCard ) @dirty = true @cached_value = nil @cached_soft = nil push newCard true end def hand_value_i if @dirty total = 0 aces = 0 each do |card| total += card aces += 1 if card == 11 end while total > 21 and aces > 0 total -= 10 aces -= 1 end #return a negative value if it is soft @cached_value = total @cached_soft = aces > 0 @dirty = false end return @cached_value end def blackjack? return ( hand_value == 21 and length == 2 ) end def soft? hand_value_i @cached_soft end def hand_value hand_value_i.abs end def bust? hand_value > 21 end def to_s hand_value.to_s + " ( " + join( ", " ) + " )" end end