Class: StartingHand

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

Overview

Two cards, also known as hole cards or pocket cards, which belong solely to one player and remain hidden from the other players.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cards) ⇒ StartingHand

Returns a new instance of StartingHand.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
# File 'lib/rora/model/starting_hand.rb', line 8

def initialize cards
  @cards = (cards.kind_of?(Array) ? cards : Card.to_cards(cards)).sort
  raise ArgumentError, "Exactly 2 cards are required to create a starting hand, #{@cards.size} provided" if @cards.size != 2
  raise ArgumentError, "The starting hand contains duplicate cards" if @cards.uniq.length != @cards.length
  @key = @cards.inject('') { |string, card| string << card.key }
  @id = @cards.inject(1) { |product, card| product * card.uid }
end

Instance Attribute Details

#cardsObject (readonly)

Returns all cards contained in the starting hand.



17
18
19
# File 'lib/rora/model/starting_hand.rb', line 17

def cards
  @cards
end

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/rora/model/starting_hand.rb', line 6

def id
  @id
end

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/rora/model/starting_hand.rb', line 6

def key
  @key
end

Class Method Details

.all_starting_handsObject

Returns all possible starting hands.

There are exaclty 1,326 (52c2) starting hands. This method returns a list containing every possible starting hand.



57
58
59
# File 'lib/rora/model/starting_hand.rb', line 57

def self.all_starting_hands
  StartingHandRepository.instance.all_starting_hands
end

.distinct_starting_handsObject

Returns all distinct starting hands.

While there are 1,324 starting hands, many of these starting hands have the same value in poker. To elaborate on this a bit, consider the number of hands a player could have containing a Jack and a Seven:

J♣ 7♦ J♠ 7♦ J♥ 7♦ J♦ 7♦ J♣ 7♠ J♠ 7♠ J♥ 7♠ J♦ 7♠ J♣ 4♥ J♠ 4♥

This list goes on a bit further further. You might be surprised to know that there are 16 starting hand combinations that contain exactly one Jack and one Seven. Out of this list of 16, only two card values have any relevance in a poker game - Jack-Seven suited and Jack-Seven unsuited.

This exercise demonstrates that while there are 1,324 starting hands to contend with, the number of distinct starting hands is dramatically lower. This is because card suits don’t tend to affect the score of the hand.

Once all 1,324 poker hands are collapsed into distinct values, we end up with just 169 starting hands!



90
91
92
# File 'lib/rora/model/starting_hand.rb', line 90

def self.distinct_starting_hands
  StartingHandRepository.instance.distinct_starting_hands
end

Instance Method Details

#==(starting_hand) ⇒ Object



98
99
100
# File 'lib/rora/model/starting_hand.rb', line 98

def == starting_hand
  self.id == starting_hand.id
end

#eql?(starting_hand) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/rora/model/starting_hand.rb', line 94

def eql? starting_hand
  self == starting_hand
end

#hashObject



102
103
104
# File 'lib/rora/model/starting_hand.rb', line 102

def hash
  return self.id
end

#pocket_pair?Boolean

Determines if the starting hand is a pocket pair.

Returns:

  • (Boolean)


44
45
46
# File 'lib/rora/model/starting_hand.rb', line 44

def pocket_pair?
  cards[0].rank == cards[1].rank
end

#short_valueObject

Returns the shorthand notation for the starting hand.

It is often desirable to have a short hand notation for starting hands, ignoring card suits and simply describing whether the starting hand is suited or not. The shorthand notation removes suit characters, and appends an ‘o’ (offsuit) or ‘s’ (suited) to the card ranks.

Starting hand consisting of the Ace of Clubs and Jack of Clubs: card.value == ‘AC,JC’ card.short_value == ‘JCs’

Starting hand consisting of the Ten of Hearts and Eight of Clubs: card.value == ‘TH,8C’, card.short_value == ‘T8o’



39
40
41
# File 'lib/rora/model/starting_hand.rb', line 39

def short_value
  @cards[0].rank.key + @cards[1].rank.key + (suited? ? "s" : "o")
end

#suited?Boolean

Determines if the starting hand is suited.

Returns:

  • (Boolean)


49
50
51
# File 'lib/rora/model/starting_hand.rb', line 49

def suited?
  @cards[0].suit == @cards[1].suit
end

#to_sObject



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

def to_s
  value
end

#valueObject



21
22
23
# File 'lib/rora/model/starting_hand.rb', line 21

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