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
15
16
17
18
# File 'lib/rora/model/starting_hand.rb', line 8

def initialize cards
  @cards = Array.new, @id = 1, @key = 1

  @cards = cards.kind_of?(Array) ? cards : Card.to_cards(cards)
  raise ArgumentError, "Exactly 2 cards are required to create a starting hand, #{@cards.size} provided" if @cards.size != 2
  raise ArgumentError, "The hand contains duplicate cards" if @cards.uniq.length != @cards.length

  @cards.each {|card| @id *= card.id}
  @cards.each {|card| @key *= card.rank.id}
  @key = suited? ? @key * 67 : @key
end

Instance Attribute Details

#cardsObject (readonly)

Returns all cards contained in the starting hand.



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

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.



65
66
67
# File 'lib/rora/model/starting_hand.rb', line 65

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!



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

def self.distinct_starting_hands
  StartingHandRepository.instance.distinct_starting_hands
end

Instance Method Details

#pocket_pair?Boolean

Determines if the starting hand is a pocket pair.

Returns:

  • (Boolean)


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

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’



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

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)


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

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

#valueObject

Returns the starting hand value.



26
27
28
# File 'lib/rora/model/starting_hand.rb', line 26

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