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)

Raises:

  • (UnableToPayAnte)


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

def initialize(hand, initial_stack, ante)
  raise UnableToPayAnte if ante > initial_stack

  @hand = hand
  @initial_stack = ChipStack.new initial_stack
  @ante = ChipStack.new ante
  @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



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

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)


145
146
147
# File 'lib/acpc_poker_types/hand_player.rb', line 145

def all_in?
  stack <= 0
end

#all_in_allowed?(betting_type, min_wager_by_cost) ⇒ Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/acpc_poker_types/hand_player.rb', line 161

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)


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

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



57
58
59
# File 'lib/acpc_poker_types/hand_player.rb', line 57

def balance
  @winnings - total_contribution
end

#contributionsObject



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

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



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

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

#folded?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/acpc_poker_types/hand_player.rb', line 149

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

#inactive?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/acpc_poker_types/hand_player.rb', line 141

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,



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
125
126
# File 'lib/acpc_poker_types/hand_player.rb', line 90

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



153
154
155
# File 'lib/acpc_poker_types/hand_player.rb', line 153

def round
  @actions.length - 1
end

#stackObject



53
54
55
# File 'lib/acpc_poker_types/hand_player.rb', line 53

def stack
  @initial_stack + balance
end

#total_contributionObject



82
83
84
# File 'lib/acpc_poker_types/hand_player.rb', line 82

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

#wager_allowed_by_stack?(amount_to_call) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/acpc_poker_types/hand_player.rb', line 157

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