Class: Hand

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

Overview

A subset of cards held at one time by a player during a game.

A hand consists of exactly five cards, and can be created in one of two ways.

A hand can be created with an array of cards. hand = Hand.new()

A hand can be created with string representation of each card. hand = Hand.new(“ACKCJS9H4H”)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cards) ⇒ Hand

Returns a new instance of Hand.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
# File 'lib/rora/model/hand.rb', line 15

def initialize cards
  @hand_repository = HandRepository.instance
  @cards = cards.kind_of?(Array) ? cards : Card.to_cards(cards)

  raise ArgumentError, "Exactly 5 cards are required to create a hand, #{cards.size} provided" if @cards.size != 5
  raise ArgumentError, "The hand contains duplicate cards" if @cards.uniq.length != @cards.length
end

Instance Attribute Details

#cardsObject (readonly)

Returns all cards contained in the hand.



63
64
65
# File 'lib/rora/model/hand.rb', line 63

def cards
  @cards
end

#hand_repositoryObject (readonly)

Returns the value of attribute hand_repository.



13
14
15
# File 'lib/rora/model/hand.rb', line 13

def hand_repository
  @hand_repository
end

Instance Method Details

#contains(cards) ⇒ Object

Determines if this hand contains the given cards.



116
117
118
# File 'lib/rora/model/hand.rb', line 116

def contains cards
  @cards.contains cards
end

#flush?Boolean

Determines if this hand is a flush.

Returns:

  • (Boolean)


68
69
70
71
72
73
# File 'lib/rora/model/hand.rb', line 68

def flush?
  for i in 0..@cards.size - 2 do
    return false if @cards[i].suit != @cards[i+1].suit
  end
  true
end

#four_of_a_kind?Boolean

Determines if this hand is four of a kind.

Returns:

  • (Boolean)


81
82
83
# File 'lib/rora/model/hand.rb', line 81

def four_of_a_kind?
  type == HandType::FOUR_OF_A_KIND
end

#full_house?Boolean

Determines if this hand is a full house.

Returns:

  • (Boolean)


86
87
88
# File 'lib/rora/model/hand.rb', line 86

def full_house?
  type == HandType::FULL_HOUSE
end

#hash_keyObject

Returns the hand hash key.



31
32
33
34
35
# File 'lib/rora/model/hand.rb', line 31

def hash_key
  j = 1
  @cards.each { |card| j = j * card.rank.id  }
  flush? ? (j * 67) : j
end

#high_card?Boolean

Determines if this hand is a high card.

Returns:

  • (Boolean)


111
112
113
# File 'lib/rora/model/hand.rb', line 111

def high_card?
  type == HandType::HIGH_CARD
end

#idObject

Returns the hand id.



24
25
26
27
28
# File 'lib/rora/model/hand.rb', line 24

def id
  i = 1
  @cards.each { |card| i = i * card.id }
  i
end

#nameObject

Returns the hand name.



53
54
55
# File 'lib/rora/model/hand.rb', line 53

def name
  resolve_hand_attribute(2)
end

#one_pair?Boolean

Determines if this hand has one pair.

Returns:

  • (Boolean)


106
107
108
# File 'lib/rora/model/hand.rb', line 106

def one_pair?
  type == HandType::ONE_PAIR
end

#probabilityObject

Returns the probability of this hand being dealt.



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

def probability
  resolve_hand_attribute(3)
end

#scoreObject

Returns the hand strength, from 1 (strongest) to 7462 (weakest).



43
44
45
# File 'lib/rora/model/hand.rb', line 43

def score
  resolve_hand_attribute(0)
end

#straight?Boolean

Determines if this hand is a straight.

Returns:

  • (Boolean)


91
92
93
# File 'lib/rora/model/hand.rb', line 91

def straight?
  type == HandType::STRAIGHT
end

#straight_flush?Boolean

Determines if this hand is a straight flush.

Returns:

  • (Boolean)


76
77
78
# File 'lib/rora/model/hand.rb', line 76

def straight_flush?
  type == HandType::STRAIGHT_FLUSH
end

#three_of_a_kind?Boolean

Determines if this hand is a three of a kind.

Returns:

  • (Boolean)


96
97
98
# File 'lib/rora/model/hand.rb', line 96

def three_of_a_kind?
  type == HandType::THREE_OF_A_KIND
end

#two_pair?Boolean

Determines if this hand is two pair.

Returns:

  • (Boolean)


101
102
103
# File 'lib/rora/model/hand.rb', line 101

def two_pair?
  type == HandType::TWO_PAIR
end

#typeObject

Returns the hand type.



58
59
60
# File 'lib/rora/model/hand.rb', line 58

def type
  HandType.get resolve_hand_attribute(1)
end

#valueObject

Returns the hand value.



38
39
40
# File 'lib/rora/model/hand.rb', line 38

def value
  @cards.map { |card| "#{card.key}" }.join(",")
end