Class: Player

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

Overview

Class to model a player.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, seat, chip_stack) ⇒ Player

TODO:

These comments don’t work as expected

Returns a new instance of Player.

Parameters:

  • name (String)

    This players name.

  • seat (Integer)

    (see #seat)

  • chip_stack (#to_i)

    (see #chip_stack)



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

def initialize(name, seat, chip_stack)
  @name = name
  @seat = seat
  @chip_balance = 0
  @chip_stack = chip_stack
  @chip_contributions = [0]

  @actions_taken_this_hand = [[]]
end

Instance Attribute Details

#actions_taken_this_handArray<Array<PokerAction>> (readonly)

Returns The list of actions this player has taken in the current hand, separated by round.

Returns:

  • (Array<Array<PokerAction>>)

    The list of actions this player has taken in the current hand, separated by round.



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

def actions_taken_this_hand
  @actions_taken_this_hand
end

#chip_balanceInteger (readonly)

Returns The amount this player has won or lost in the current match. During a hand, this is a projected amount assuming that this player loses. Positive amounts are winnings, negative amounts are losses.

Returns:

  • (Integer)

    The amount this player has won or lost in the current match. During a hand, this is a projected amount assuming that this player loses. Positive amounts are winnings, negative amounts are losses.



44
45
46
# File 'lib/acpc_poker_types/player.rb', line 44

def chip_balance
  @chip_balance
end

#chip_contributionsArray<ChipStack> (readonly)

Returns This player’s contribution to the pot in the current hand, organized by round.

Returns:

  • (Array<ChipStack>)

    This player’s contribution to the pot in the current hand, organized by round.



39
40
41
# File 'lib/acpc_poker_types/player.rb', line 39

def chip_contributions
  @chip_contributions
end

#chip_stackChipStack (readonly)

Returns This player’s chip stack.

Returns:

  • (ChipStack)

    This player’s chip stack.



35
36
37
# File 'lib/acpc_poker_types/player.rb', line 35

def chip_stack
  @chip_stack
end

#hole_cardsHand (readonly)

Returns This player’s hole cards or nil if this player is not holding cards.

Examples:

An ace of diamonds and a 4 of clubs is represented as

'Ad4c'

Returns:

  • (Hand)

    This player’s hole cards or nil if this player is not holding cards.



49
50
51
# File 'lib/acpc_poker_types/player.rb', line 49

def hole_cards
  @hole_cards
end

#nameString (readonly)

Returns The name of this player.

Returns:

  • (String)

    The name of this player.



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

def name
  @name
end

#seatInteger (readonly)

Returns This player’s seat. This is a 0 indexed number that represents the order that this player joined the dealer.

Returns:

  • (Integer)

    This player’s seat. This is a 0 indexed number that represents the order that this player joined the dealer.



32
33
34
# File 'lib/acpc_poker_types/player.rb', line 32

def seat
  @seat
end

Class Method Details

.create_players(player_names, game_def) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/acpc_poker_types/player.rb', line 11

def self.create_players(player_names, game_def)
  if game_def.number_of_players != player_names.length
    raise(
      IncorrectNumberOfPlayerNames,
      "#{player_names.length} names for #{game_def.number_of_players} players"
    )
  end

  game_def.number_of_players.times.inject([]) do |players, seat|
    players << Player.join_match(player_names[seat],
                                 seat,
                                 ChipStack.new(game_def.chip_stacks[seat])
                                 )
  end
end

Instance Method Details

#==(other) ⇒ Object



71
72
73
74
# File 'lib/acpc_poker_types/player.rb', line 71

def ==(other)
  equal_on_public_info?(other) &&
  @hole_cards == other.hole_cards
end

#active?Boolean

Returns Whether or not this player is active (has not folded or gone all-in). true if this player is active, false otherwise.

Returns:

  • (Boolean)

    Whether or not this player is active (has not folded or gone all-in). true if this player is active, false otherwise.



129
130
131
# File 'lib/acpc_poker_types/player.rb', line 129

def active?
  !(folded? || all_in?)
end

#all_in?Boolean

Returns Reports whether or not this player is all-in.

Returns:

  • (Boolean)

    Reports whether or not this player is all-in.



123
124
125
# File 'lib/acpc_poker_types/player.rb', line 123

def all_in?
  0 == @chip_stack
end

#assign_cards!(hole_cards) ⇒ Object



146
147
148
149
150
# File 'lib/acpc_poker_types/player.rb', line 146

def assign_cards!(hole_cards)
  @hole_cards = hole_cards

  self
end

#equal_on_public_info?(other) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
# File 'lib/acpc_poker_types/player.rb', line 76

def equal_on_public_info?(other)
  @name == other.name && 
  @seat == other.seat && 
  @chip_stack == other.chip_stack &&
  @chip_contributions == other.chip_contributions &&
  @chip_balance == other.chip_balance &&
  @actions_taken_this_hand == other.actions_taken_this_hand
end

#folded?Boolean

Returns Reports whether or not this player has folded.

Returns:

  • (Boolean)

    Reports whether or not this player has folded.



114
115
116
117
118
119
120
# File 'lib/acpc_poker_types/player.rb', line 114

def folded?
  if @actions_taken_this_hand.last.empty?
    false
  else
    :fold == @actions_taken_this_hand.last.last.to_sym
  end
end

#roundInteger

Returns The current round, zero indexed.

Returns:

  • (Integer)

    The current round, zero indexed.



134
135
136
# File 'lib/acpc_poker_types/player.rb', line 134

def round
  @actions_taken_this_hand.length - 1
end

#start_new_hand!(blind = ChipStack.new(0), chip_stack = @chip_stack, hole_cards = Hand.new) ⇒ Object

Parameters:

  • blind_amount (#to_i)

    The blind amount for this player to pay.

  • chip_stack (#to_i) (defaults to: @chip_stack)

    (see #chip_stack)

  • hole_cards (Hand) (defaults to: Hand.new)

    (see #hole_cards)



88
89
90
91
92
93
94
95
96
97
# File 'lib/acpc_poker_types/player.rb', line 88

def start_new_hand!(blind=ChipStack.new(0), chip_stack=@chip_stack, hole_cards=Hand.new)
  @chip_stack = chip_stack
  @hole_cards = hole_cards
  @actions_taken_this_hand = []
  @chip_contributions = []

  start_new_round!

  pay_blind! blind
end

#start_new_round!Object



99
100
101
102
103
104
# File 'lib/acpc_poker_types/player.rb', line 99

def start_new_round!
  @actions_taken_this_hand << []
  @chip_contributions << 0

  self
end

#take_action!(action) ⇒ Object

Parameters:



107
108
109
110
111
# File 'lib/acpc_poker_types/player.rb', line 107

def take_action!(action)
  @actions_taken_this_hand.last << action

  take_from_chip_stack! action.amount_to_put_in_pot
end

#take_winnings!(chips) ⇒ Object

Adjusts this player’s state when it takes chips from the pot.

Parameters:

  • chips (Integer)

    The number of chips this player has won from the pot.



140
141
142
143
144
# File 'lib/acpc_poker_types/player.rb', line 140

def take_winnings!(chips)
  @chip_contributions << 0

  add_to_stack! chips
end