Class: AcpcPokerTypes::HandPlayer

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

Overview

Class to model a player during a hand from information in a MatchState

Direct Known Subclasses

NilHandPlayer, Player

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hand, initial_stack, ante) ⇒ HandPlayer

Returns a new instance of HandPlayer.

Parameters:

  • hand (Hand)
  • initial_chip_stack (#to_i)
  • ante (#to_i)


35
36
37
38
39
40
41
# File 'lib/acpc_poker_types/hand_player.rb', line 35

def initialize(hand, initial_stack, ante)
  @hand = hand
  @initial_stack = ChipStack.new initial_stack
  @ante = ChipStack.new [ante, initial_stack].min
  @actions = [[]]
  @winnings = ChipStack.new 0
end

Instance Attribute Details

#actionsArray<PokerAction> (readonly)

Returns The actions this player has taken.

Returns:

  • (Array<PokerAction>)

    The actions this player has taken



28
29
30
# File 'lib/acpc_poker_types/hand_player.rb', line 28

def actions
  @actions
end

#anteAcpcPokerTypes::ChipStack (readonly)

the hand.

Returns:



21
22
23
# File 'lib/acpc_poker_types/hand_player.rb', line 21

def ante
  @ante
end

#handHand

holding cards.

Returns:

  • (Hand)

    This player's hole cards or nil if this player is not



25
26
27
# File 'lib/acpc_poker_types/hand_player.rb', line 25

def hand
  @hand
end

#initial_stackAcpcPokerTypes::ChipStack (readonly)

hand before paying their ante.

Returns:



17
18
19
# File 'lib/acpc_poker_types/hand_player.rb', line 17

def initial_stack
  @initial_stack
end

#winningsObject

Returns the value of attribute winnings.



30
31
32
# File 'lib/acpc_poker_types/hand_player.rb', line 30

def winnings
  @winnings
end

Instance Method Details

#==(other_player) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/acpc_poker_types/hand_player.rb', line 43

def ==(other_player)
  @hand == other_player.hand &&
  @initial_stack == other_player.initial_stack &&
  @ante == other_player.ante &&
  @actions == other_player.actions &&
  @winnings == other_player.winnings
end

#all_in?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/acpc_poker_types/hand_player.rb', line 143

def all_in?
  stack <= 0
end

#all_in_allowed?(betting_type, min_wager_by_cost) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/acpc_poker_types/hand_player.rb', line 159

def all_in_allowed?(betting_type, min_wager_by_cost)
  betting_type != GameDefinition::BETTING_TYPES[:limit] && min_wager_by_cost < stack
end

#append_action!(action, round_num = round) ⇒ Object

Raises:

  • (Inactive)


126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/acpc_poker_types/hand_player.rb', line 126

def append_action!(action, round_num = round)
  raise Inactive if inactive?

  while @actions.length <= round_num
    @actions << []
  end

  @actions[round_num] ||= []
  @actions[round_num] << action

  self
end

#balanceObject



55
56
57
# File 'lib/acpc_poker_types/hand_player.rb', line 55

def balance
  @winnings - total_contribution
end

#contributionsObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/acpc_poker_types/hand_player.rb', line 67

def contributions
  contribution_list = @actions.map do |actions_per_round|
    actions_per_round.inject(0) { |sum, action| sum += action.cost }
  end
  if contribution_list.empty?
    contribution_list << @ante
  else
    contribution_list[0] += @ante
  end

  contribution_list
end

#contributions_before(cur_round = round) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/acpc_poker_types/hand_player.rb', line 59

def contributions_before(cur_round = round)
  if cur_round > 0
    contributions[0..cur_round-1].inject(:+)
  else
    0
  end
end

#folded?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/acpc_poker_types/hand_player.rb', line 147

def folded?
  @actions.flatten.last == PokerAction::FOLD
end

#inactive?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/acpc_poker_types/hand_player.rb', line 139

def inactive?
  folded? || all_in?
end

the smallest and largest possible wagers will be returned in the list.

Parameters:

  • amount_to_call (#to_r) (defaults to: ChipStack.new(0))

    The amount to call for this player

  • wager_illegal (Boolean) (defaults to: false)

Returns:

  • (Array<PokerAction>)

    The list of legal actions for this player. If a wager is legal,



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/acpc_poker_types/hand_player.rb', line 88

def legal_actions(
  in_round: round,
  amount_to_call: ChipStack.new(0),
  wager_illegal: false,
  betting_type: GameDefinition::BETTING_TYPES[:limit],
  min_wager_by: ChipStack.new(1)
)
  l_actions = []
  return l_actions if inactive?

  if amount_to_call.to_r > 0
    l_actions << PokerAction.new(PokerAction::CALL, cost: amount_to_call)
    l_actions << PokerAction.new(PokerAction::FOLD)
  else
    l_actions << PokerAction.new(PokerAction::CHECK)
  end
  if !wager_illegal && wager_allowed_by_stack?(amount_to_call)
    min_wager_by_cost = [min_wager_by + amount_to_call.to_r, stack].min

    add_wager_actions = ->(wager_character) do
      l_actions << PokerAction.new(wager_character, cost: min_wager_by_cost)
      if all_in_allowed?(betting_type, min_wager_by_cost)
        l_actions << PokerAction.new(wager_character, cost: stack)
      end
    end

    if (
      amount_to_call.to_r > 0 || contributions[in_round].to_i > 0
    )
      add_wager_actions.call(PokerAction::RAISE)
    else
      add_wager_actions.call(PokerAction::BET)
    end
  end

  l_actions
end

#roundObject



151
152
153
# File 'lib/acpc_poker_types/hand_player.rb', line 151

def round
  @actions.length - 1
end

#stackObject



51
52
53
# File 'lib/acpc_poker_types/hand_player.rb', line 51

def stack
  @initial_stack + balance
end

#total_contributionObject



80
81
82
# File 'lib/acpc_poker_types/hand_player.rb', line 80

def total_contribution
  @ante + @actions.flatten.inject(0) { |sum, action| sum += action.cost }
end

#wager_allowed_by_stack?(amount_to_call) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/acpc_poker_types/hand_player.rb', line 155

def wager_allowed_by_stack?(amount_to_call)
  stack > amount_to_call.to_r
end