Class: PokerOdds::Hand

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

Instance Method Summary collapse

Instance Method Details

#deckObject



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/poker_odds/hand.rb', line 152

def deck
  cards = PokerTrump::Cards.new_deck
  cards.delete_cards!(expose_cards) unless expose_cards.size == 0
  player_hands.each do |player_hand|
    cards.delete_cards!(player_hand)
  end
  cards.delete_cards!(flop_cards) unless flop_cards.size == 0
  cards.delete_card!(turn_card) if !!turn_card
  cards.delete_card!(river_card) if !!river_card

  cards
end

#equitiesObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/poker_odds/hand.rb', line 34

def equities
  z = deck.each_with_object({}) do |card, a|
    scores = player_hands.each_with_object({}) do |player_hand, h|
      cards = player_hand
      cards = cards.add_cards(flop_cards)
      cards = cards.add_card(turn_card)
      cards = cards.add_card(card)

      h[cards.score] ||= []
      h[cards.score] << player_hand
    end

    a[card] = {}
    if scores.keys.size == 1
      a[card][:tie_hands] = player_hands
    else
      mk = scores.keys.max
      a[card][:win_hands] = scores[mk]
      a[card][:lose_hands] = scores.each_with_object([]) do |(k,v), a|
        next if k == mk

        a.concat(v)
      end
    end
  end

  s = z.size.to_f

  player_hands.each_with_object({}) do |player_hand, h|
    win_hands = z.select { |a,b| b[:win_hands]&.include?(player_hand) }
    win_rate = win_hands.size / s
    h[player_hand] = {}
    h[player_hand][:win_rate] = win_rate
    h[player_hand][:lose_rate] = z.count { |a,b| b[:lose_hands]&.include?(player_hand) } / s
    h[player_hand][:tie_rate] = z.count { |a,b| b[:tie_hands]&.include?(player_hand) } / s
    h[player_hand][:outs] = win_hands.keys if win_rate < 0.5
  end
end

#flop=(str) ⇒ Object



19
20
21
22
# File 'lib/poker_odds/hand.rb', line 19

def flop=(str)
  self.flop_cards = PokerTrump::Cards.from_string(str)
  self
end

#flop_equitiesObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/poker_odds/hand.rb', line 73

def flop_equities
  z = deck.combination(2).each_with_object({}) do |cs, a|
    scores = player_hands.each_with_object({}) do |player_hand, h|
      cards = player_hand
      cards = cards.add_cards(flop_cards)

      cs.each { |c| cards = cards.add_card(c) }

      h[cards.score] ||= []
      h[cards.score] << player_hand
    end

    a[cs] = {}
    if scores.keys.size == 1
      a[cs][:tie_hands] = player_hands
    else
      mk = scores.keys.max
      a[cs][:win_hands] = scores[mk]
      a[cs][:lose_hands] = scores.each_with_object([]) do |(k,v), a|
        next if k == mk

        a.concat(v)
      end
    end
  end

  s = z.size.to_f

  player_hands.each_with_object({}) do |player_hand, h|
    win_hands = z.select { |a,b| b[:win_hands]&.include?(player_hand) }
    win_rate = win_hands.size / s
    h[player_hand] = {}
    h[player_hand][:win_rate] = win_rate
    h[player_hand][:lose_rate] = z.count { |a,b| b[:lose_hands]&.include?(player_hand) } / s
    h[player_hand][:tie_rate] = z.count { |a,b| b[:tie_hands]&.include?(player_hand) } / s
    h[player_hand][:outs] = win_hands.keys if win_rate < 0.5
  end
end

#player=(str) ⇒ Object



12
13
14
15
16
17
# File 'lib/poker_odds/hand.rb', line 12

def player=(str)
  self.player_hands = str.split(',').map do |s|
    PokerTrump::Cards.from_string(s.lstrip)
  end
  self
end

#preflop_equitiesObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/poker_odds/hand.rb', line 112

def preflop_equities
  ran = deck.combination(5).to_a
  z = 10000.times.with_object({}) do |i, a|
    cs = ran.sample
    scores = player_hands.each_with_object({}) do |player_hand, h|
      cards = player_hand

      cs.each { |c| cards = cards.add_card(c) }

      h[cards.score] ||= []
      h[cards.score] << player_hand
    end

    a[cs] = {}
    if scores.keys.size == 1
      a[cs][:tie_hands] = player_hands
    else
      mk = scores.keys.max
      a[cs][:win_hands] = scores[mk]
      a[cs][:lose_hands] = scores.each_with_object([]) do |(k,v), a|
        next if k == mk

        a.concat(v)
      end
    end
  end

  s = z.size.to_f

  player_hands.each_with_object({}) do |player_hand, h|
    win_hands = z.select { |a,b| b[:win_hands]&.include?(player_hand) }
    win_rate = win_hands.size / s
    h[player_hand] = {}
    h[player_hand][:win_rate] = win_rate
    h[player_hand][:lose_rate] = z.count { |a,b| b[:lose_hands]&.include?(player_hand) } / s
    h[player_hand][:tie_rate] = z.count { |a,b| b[:tie_hands]&.include?(player_hand) } / s
    h[player_hand][:outs] = win_hands.keys if win_rate < 0.5
  end
end

#river=(s) ⇒ Object



29
30
31
32
# File 'lib/poker_odds/hand.rb', line 29

def river=(s)
  self.river_cards = PokerTrump::Card.from_string(str)
  self
end

#turn=(str) ⇒ Object



24
25
26
27
# File 'lib/poker_odds/hand.rb', line 24

def turn=(str)
  self.turn_card = PokerTrump::Card.from_string(str)
  self
end