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
# 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)).sort
  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.



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

def cards
  @cards
end

Instance Method Details

#contains(cards) ⇒ Object

Determines if this hand contains the given cards.



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

def contains cards
  @cards.contains cards
end

#flush?Boolean

Determines if this hand is a flush.

Returns:

  • (Boolean)


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

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)


61
62
63
# File 'lib/rora/model/hand.rb', line 61

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)


66
67
68
# File 'lib/rora/model/hand.rb', line 66

def full_house?
  type == HandType::FULL_HOUSE
end

#high_card?Boolean

Determines if this hand is a high card.

Returns:

  • (Boolean)


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

def high_card?
  type == HandType::HIGH_CARD
end

#keyObject

Returns the hand key.



23
24
25
# File 'lib/rora/model/hand.rb', line 23

def key
  @cards.map { |card| "#{card.key}" }.join
end

#nameObject

Returns the hand name.



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

def name
  resolve_hand_attribute(2)
end

#one_pair?Boolean

Determines if this hand has one pair.

Returns:

  • (Boolean)


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

def one_pair?
  type == HandType::ONE_PAIR
end

#scoreObject

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



28
29
30
# File 'lib/rora/model/hand.rb', line 28

def score
  resolve_hand_attribute(0)
end

#straight?Boolean

Determines if this hand is a straight.

Returns:

  • (Boolean)


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

def straight?
  type == HandType::STRAIGHT
end

#straight_flush?Boolean

Determines if this hand is a straight flush.

Returns:

  • (Boolean)


56
57
58
# File 'lib/rora/model/hand.rb', line 56

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)


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

def three_of_a_kind?
  type == HandType::THREE_OF_A_KIND
end

#to_sObject



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

def to_s
  "#{@cards.inject('') { |string, card| string << card.key }} (#{name})"
end

#two_pair?Boolean

Determines if this hand is two pair.

Returns:

  • (Boolean)


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

def two_pair?
  type == HandType::TWO_PAIR
end

#typeObject

Returns the hand type.



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

def type
  HandType.get resolve_hand_attribute(1)
end