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 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
#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
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
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
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
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 |
#balance ⇒ Object
57 58 59 |
# File 'lib/acpc_poker_types/hand_player.rb', line 57 def balance @winnings - total_contribution end |
#contributions ⇒ Object
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
149 150 151 |
# File 'lib/acpc_poker_types/hand_player.rb', line 149 def folded? @actions.flatten.last == PokerAction::FOLD end |
#inactive? ⇒ Boolean
141 142 143 |
# File 'lib/acpc_poker_types/hand_player.rb', line 141 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.
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 |
#round ⇒ Object
153 154 155 |
# File 'lib/acpc_poker_types/hand_player.rb', line 153 def round @actions.length - 1 end |
#stack ⇒ Object
53 54 55 |
# File 'lib/acpc_poker_types/hand_player.rb', line 53 def stack @initial_stack + balance end |
#total_contribution ⇒ Object
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
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 |