Class: Ventiuna::Hand

Inherits:
Object
  • Object
show all
Defined in:
lib/ventiuna/hand.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(card = [], *other_cards) ⇒ Hand

Returns a new instance of Hand.



5
6
7
8
9
# File 'lib/ventiuna/hand.rb', line 5

def initialize(card=[], *other_cards)
	@cards = Array(card) + other_cards
	@soft = false
	@active = true
end

Instance Attribute Details

#cardsObject

Returns the value of attribute cards.



3
4
5
# File 'lib/ventiuna/hand.rb', line 3

def cards
  @cards
end

Instance Method Details

#<<(another_card) ⇒ Object Also known as: hit



11
12
13
# File 'lib/ventiuna/hand.rb', line 11

def << (another_card)
	self.cards << another_card
end

#active?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/ventiuna/hand.rb', line 36

def active?
	@active
end

#blackjack?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/ventiuna/hand.rb', line 40

def blackjack?
	self.value == 21 && self.cards.size == 2
end

#bust?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/ventiuna/hand.rb', line 44

def bust?
	self.value > 21 #&& !self.soft?
end

#pair?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/ventiuna/hand.rb', line 48

def pair?
	self.cards.size == 2 && (self.cards.first.value == self.cards.last.value)
end

#soft?Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/ventiuna/hand.rb', line 52

def soft?
	self.value
	@soft
end

#splitObject



20
21
22
23
# 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

#standObject



16
17
18
# File 'lib/ventiuna/hand.rb', line 16

def stand
	@active = false
end

#to_dbObject



61
62
63
64
65
66
67
# 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_sObject



57
58
59
# File 'lib/ventiuna/hand.rb', line 57

def to_s
	"#{self.cards.collect{|c| c.to_s}} = #{self.value}"
end

#valueObject



25
26
27
28
29
30
31
32
33
34
# 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