Class: PokerRanking::Hand

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

Constant Summary collapse

HAND_TYPES =
[HandType::StraightFlush, HandType::FourOfAKind, HandType::FullHouse, HandType::Flush, HandType::Straight, HandType::ThreeOfAKind, HandType::TwoPair, HandType::Pair, HandType::HighCard]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cards) ⇒ Hand

Returns a new instance of Hand.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hand.rb', line 18

def initialize(cards)
  @cards = []
  cards.each do |card|
    if card.is_a? PokerRanking::Card
      @cards << card
    elsif card.is_a? String
      @cards << PokerRanking::Card::by_name(card)
    else
      @cards << PokerRanking::Card.new(card)
    end
  end

  if @cards.length > 0
    @cards.sort_by! { |card| card.value }

    calculate_rank
  end
end

Instance Attribute Details

#cardsObject (readonly)

Returns the value of attribute cards.



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

def cards
  @cards
end

#kickersObject (readonly)

Returns the value of attribute kickers.



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

def kickers
  @kickers
end

#rankObject (readonly)

Returns the value of attribute rank.



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

def rank
  @rank
end

#second_valueObject (readonly)

Returns the value of attribute second_value.



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

def second_value
  @second_value
end

#valueObject (readonly)

Returns the value of attribute value.



14
15
16
# File 'lib/hand.rb', line 14

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



64
65
66
# File 'lib/hand.rb', line 64

def ==(other)
  cards == other.cards
end

#cards_usedObject



60
61
62
# File 'lib/hand.rb', line 60

def cards_used
  hand_type.cards_used.sort_by! { |card| card.value }
end

#dataObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/hand.rb', line 68

def data
  {
      cards: @cards.map { |card| card.data },
      cards_used: cards_used.map { |card| card.data },
      rank: rank,
      value: value,
      second_value: second_value,
      kickers: kickers
  }
end

#defeats?(other_hand) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hand.rb', line 38

def defeats?(other_hand)
  if @cards.length == 0
    return false
  end

  if other_hand.cards.length == 0
    return true
  end

  return self.rank > other_hand.rank unless self.rank == other_hand.rank
  return self.value > other_hand.value unless self.value == other_hand.value
  return self.second_value > other_hand.second_value unless self.second_value == other_hand.second_value
  self.kickers.each_with_index do |kicker,index|
    return kicker > other_hand.kickers[index] unless kicker == other_hand.kickers[index]
  end
  return false
end

#nameObject



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

def name
  hand_type.name
end