Class: AcpcPokerTypes::HandPlayer
- Inherits:
-
Object
- Object
- AcpcPokerTypes::HandPlayer
- 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
Instance Attribute Summary collapse
-
#actions ⇒ Array<PokerAction>
readonly
The actions this player has taken.
-
#ante ⇒ AcpcPokerTypes::ChipStack
readonly
the hand.
-
#hand ⇒ Hand
holding cards.
-
#initial_stack ⇒ AcpcPokerTypes::ChipStack
readonly
hand before paying their ante.
-
#winnings ⇒ Object
Returns the value of attribute winnings.
Instance Method Summary collapse
- #==(other_player) ⇒ Object
- #all_in? ⇒ Boolean
- #all_in_allowed?(betting_type, min_wager_by_cost) ⇒ Boolean
- #append_action!(action, round_num = round) ⇒ Object
- #balance ⇒ Object
- #contributions ⇒ Object
- #contributions_before(cur_round = round) ⇒ Object
- #folded? ⇒ Boolean
- #inactive? ⇒ Boolean
-
#initialize(hand, initial_stack, ante) ⇒ HandPlayer
constructor
A new instance of HandPlayer.
-
#legal_actions(in_round: round, amount_to_call: ChipStack.new(0), wager_illegal: false, betting_type: , min_wager_by: ChipStack.new(1)) ⇒ Array<PokerAction>
the smallest and largest possible wagers will be returned in the list.
- #round ⇒ Object
- #stack ⇒ Object
- #total_contribution ⇒ Object
- #wager_allowed_by_stack?(amount_to_call) ⇒ Boolean
Constructor Details
#initialize(hand, initial_stack, ante) ⇒ HandPlayer
Returns a new instance of HandPlayer.
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
#actions ⇒ Array<PokerAction> (readonly)
Returns The actions this player has taken.
28 29 30 |
# File 'lib/acpc_poker_types/hand_player.rb', line 28 def actions @actions end |
#ante ⇒ AcpcPokerTypes::ChipStack (readonly)
the hand.
21 22 23 |
# File 'lib/acpc_poker_types/hand_player.rb', line 21 def ante @ante end |
#hand ⇒ Hand
holding cards.
25 26 27 |
# File 'lib/acpc_poker_types/hand_player.rb', line 25 def hand @hand end |
#initial_stack ⇒ AcpcPokerTypes::ChipStack (readonly)
hand before paying their ante.
17 18 19 |
# File 'lib/acpc_poker_types/hand_player.rb', line 17 def initial_stack @initial_stack end |
#winnings ⇒ Object
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
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
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
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 |
#balance ⇒ Object
55 56 57 |
# File 'lib/acpc_poker_types/hand_player.rb', line 55 def balance @winnings - total_contribution end |
#contributions ⇒ Object
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
147 148 149 |
# File 'lib/acpc_poker_types/hand_player.rb', line 147 def folded? @actions.flatten.last == PokerAction::FOLD end |
#inactive? ⇒ Boolean
139 140 141 |
# File 'lib/acpc_poker_types/hand_player.rb', line 139 def inactive? folded? || all_in? end |
#legal_actions(in_round: round, amount_to_call: ChipStack.new(0), wager_illegal: false, betting_type: , min_wager_by: ChipStack.new(1)) ⇒ Array<PokerAction>
the smallest and largest possible wagers will be returned in the list.
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 |
#round ⇒ Object
151 152 153 |
# File 'lib/acpc_poker_types/hand_player.rb', line 151 def round @actions.length - 1 end |
#stack ⇒ Object
51 52 53 |
# File 'lib/acpc_poker_types/hand_player.rb', line 51 def stack @initial_stack + balance end |
#total_contribution ⇒ Object
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
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 |