Class: AcpcPokerTypes::MatchState

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

Overview

Model to parse and manage information from a given match state string.

Constant Summary collapse

LABEL =

Returns Label for match state strings.

Returns:

  • (String)

    Label for match state strings.

'MATCHSTATE'
COMMUNITY_CARD_SEPARATOR =
'/'
BETTING_SEQUENCE_SEPARATOR =
COMMUNITY_CARD_SEPARATOR
HAND_SEPARATOR =
'|'
FIELD_SEPARATOR =
':'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_match_state, previous_state = nil, game_def = nil) ⇒ MatchState

Returns a new instance of MatchState.

Parameters:

  • raw_match_state (String)

    A raw match state string to be parsed.

Raises:

  • IncompleteMatchState



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/acpc_poker_types/match_state.rb', line 114

def initialize(raw_match_state, previous_state = nil, game_def = nil)
  fields = raw_match_state.split(/(#{FIELD_SEPARATOR}|\s+#\s*)/)[1..-1]
  fields.delete ':'
  @position_relative_to_dealer = fields.shift.to_i
  @hand_number = fields.shift.to_i

  @comment = if fields[-2].count('#') > 0
    comment = fields.pop
    fields.pop
    comment
  end

  @stack_sizes = if (
      fields.length > 2 &&
      fields.first.count(HAND_SEPARATOR) > 0
    ) then fields.shift.split(HAND_SEPARATOR).map(&:to_f) end

  @betting_sequence_string = fields.shift

  card_fields = fields.shift.split(COMMUNITY_CARD_SEPARATOR)
  @hands_string = card_fields.shift
  @community_cards_string = card_fields.join('/')

  @winning_players = nil
  @str = nil
  @all_hands = nil
  @community_cards = nil
  @round = nil
  @hand = nil
  @opponent_hands = nil
  @first_state_of_first_round = nil
  @first_state_of_round = nil
  @last_action = nil
  @number_of_actions_this_round = nil
  @number_of_actions_this_hand = nil
  @round_in_which_last_action_taken = nil
  @number_of_players = nil
  @betting_sequence = nil
  @hand_ended = nil
  @opponents_cards_visible = nil
  @pot = nil
  @legal_actions = nil
  if (
    previous_state.nil? ||
    game_def.nil? ||
    last_action.nil? ||
    previous_state.number_of_actions_this_hand != number_of_actions_this_hand - 1
  )
    @precise_betting_sequence = nil
    @next_to_act = nil
    @players = nil
    @player_acting_sequence = nil
    @min_wager_by = nil
  else # Only process the one new action, bootstrapping from the given last state
    @precise_betting_sequence = previous_state.betting_sequence(game_def).map { |per_round| per_round.dup }
    @next_to_act = previous_state.next_to_act(game_def)
    @players = previous_state.players(game_def).dup
    @players.each_with_index { |player, i| player.hand = all_hands[i] }
    @player_acting_sequence = previous_state.player_acting_sequence(
      game_def
    ).map { |per_round| per_round.dup }
    @min_wager_by = previous_state.min_wager_by(game_def)

    process_action!(last_action, round_in_which_last_action_taken)
    init_new_round!(game_def, round) if round != previous_state.round
    finish_update!(game_def)
  end

  super to_s
end

Instance Attribute Details

#betting_sequence_stringString (readonly)

Returns The ACPC string created by the given betting sequence.

Returns:

  • (String)

    The ACPC string created by the given betting sequence.



54
55
56
# File 'lib/acpc_poker_types/match_state.rb', line 54

def betting_sequence_string
  @betting_sequence_string
end

#commentString or nil (readonly)

Returns The comment appended to the end of the match state string or nil if none was specified.

Returns:

  • (String or nil)

    The comment appended to the end of the match state string or nil if none was specified.



68
69
70
# File 'lib/acpc_poker_types/match_state.rb', line 68

def comment
  @comment
end

#community_cards_stringObject (readonly)

Returns the value of attribute community_cards_string.



58
59
60
# File 'lib/acpc_poker_types/match_state.rb', line 58

def community_cards_string
  @community_cards_string
end

#hand_numberInteger (readonly)

Returns The hand number.

Returns:

  • (Integer)

    The hand number.



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

def hand_number
  @hand_number
end

#hands_stringObject (readonly)

Returns the value of attribute hands_string.



56
57
58
# File 'lib/acpc_poker_types/match_state.rb', line 56

def hands_string
  @hands_string
end

#position_relative_to_dealerInteger (readonly)

Returns The position relative to the dealer of the player that received the match state string, indexed from 0, modulo the number of players.

Examples:

The player immediately to the left of the dealer has

+position_relative_to_dealer+ == 0

The dealer has

+position_relative_to_dealer+ == <number of players> - 1

Returns:

  • (Integer)

    The position relative to the dealer of the player that received the match state string, indexed from 0, modulo the number of players.



48
49
50
# File 'lib/acpc_poker_types/match_state.rb', line 48

def position_relative_to_dealer
  @position_relative_to_dealer
end

#stack_sizesArray<Float> or nil (readonly)

Returns The stack sizes at the beginning of the current hand or nil if none were specified by the match state string.

Returns:

  • (Array<Float> or nil)

    The stack sizes at the beginning of the current hand or nil if none were specified by the match state string.



64
65
66
# File 'lib/acpc_poker_types/match_state.rb', line 64

def stack_sizes
  @stack_sizes
end

#winning_playersObject (readonly)

Returns the value of attribute winning_players.



60
61
62
# File 'lib/acpc_poker_types/match_state.rb', line 60

def winning_players
  @winning_players
end

Class Method Details

.build_match_state_string(position_relative_to_dealer, hand_number, betting_sequence, all_hole_cards, board_cards, stack_sizes = nil) ⇒ String

Builds a match state string from its given component parts.

Parameters:

  • position_relative_to_dealer (#to_s)

    The position relative to the dealer.

  • hand_number (#to_s)

    The hand number.

  • betting_sequence (#to_s)

    The betting sequence.

  • all_hole_cards (#to_s)

    All the hole cards visible.

  • board_cards (#to_s, #empty?)

    All the community cards on the board.

Returns:

  • (String)

    The constructed match state string.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/acpc_poker_types/match_state.rb', line 95

def self.build_match_state_string(
  position_relative_to_dealer,
  hand_number,
  betting_sequence,
  all_hole_cards,
  board_cards,
  stack_sizes = nil
)
  string = "#{LABEL}#{FIELD_SEPARATOR}#{position_relative_to_dealer}#{FIELD_SEPARATOR}#{hand_number}"
  if stack_sizes
    string << "#{FIELD_SEPARATOR}#{stack_sizes.map { |stack| format('%g', stack) }.join('|')}"
  end
  string << "#{FIELD_SEPARATOR}#{betting_sequence}#{FIELD_SEPARATOR}#{all_hole_cards}"
  string << "/#{board_cards.to_s}" if board_cards && !board_cards.empty?
  string
end

.receive(connection) ⇒ MatchState

Receives a match state string from the given connection.

Parameters:

  • connection (#gets)

    The connection from which a match state string should be received.

Returns:

  • (MatchState)

    The match state string that was received from the connection or nil if none could be received.



83
84
85
# File 'lib/acpc_poker_types/match_state.rb', line 83

def self.receive(connection)
  MatchState.parse connection.gets
end

Instance Method Details

#==(another_match_state) ⇒ Boolean

Returns true if this match state string is equivalent to another_match_state, false otherwise.

Parameters:

  • another_match_state (MatchState)

    A match state string to compare against this one.

Returns:

  • (Boolean)

    true if this match state string is equivalent to another_match_state, false otherwise.



202
203
204
# File 'lib/acpc_poker_types/match_state.rb', line 202

def ==(another_match_state)
  another_match_state.to_s == to_s
end

#all_handsArray<Hand>

Returns The list of hole card hands for each player.

Returns:

  • (Array<Hand>)

    The list of hole card hands for each player.



207
208
209
210
211
212
213
214
215
216
217
# File 'lib/acpc_poker_types/match_state.rb', line 207

def all_hands
  return @all_hands unless @all_hands.nil?

  @all_hands = all_string_hands(@hands_string).map do |string_hand|
    Hand.from_acpc string_hand
  end
  while @all_hands.length < number_of_players
    @all_hands.push Hand.new
  end
  @all_hands
end

#betting_sequence(game_def = nil) ⇒ Array<Array<PokerAction>>

and made more precise (bets marked by ‘r’ are converted into ‘b’ and checks marked by ‘c’ are converted to ‘k’).

Parameters:

  • game_def (GameDefinition) (defaults to: nil)

    A game definition by which the actions can be interpreted

Returns:

  • (Array<Array<PokerAction>>)

    The sequence of betting actions.



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/acpc_poker_types/match_state.rb', line 223

def betting_sequence(game_def=nil)
  if game_def
    every_action(game_def) unless @precise_betting_sequence
    return @precise_betting_sequence
  end

  @betting_sequence ||= if @betting_sequence_string.empty?
    [[]]
  else
    sequence = @betting_sequence_string.split(
      BETTING_SEQUENCE_SEPARATOR
    ).map do |betting_string_per_round|
      actions_from_acpc_characters(betting_string_per_round).map do |action|
        PokerAction.new(action)
      end
    end

    # Adjust the number of rounds if the last action was the last action in the round
    while sequence.length <= round
      sequence << []
    end
    sequence
  end
end

#community_cardsBoardCards

Returns All visible community cards on the board.

Returns:

  • (BoardCards)

    All visible community cards on the board.



249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/acpc_poker_types/match_state.rb', line 249

def community_cards
  return @community_cards unless @community_cards.nil?

  @community_cards = BoardCards.new(
    all_sets_of_community_cards(@community_cards_string).map do |cards_per_round|
      Card.cards(cards_per_round)
    end
  )
  if @community_cards.round < @community_cards_string.count(COMMUNITY_CARD_SEPARATOR)
    @community_cards.next_round!
  end
  @community_cards
end

#every_action(game_def) ⇒ HandPlayerGroup

Returns The current state of the players.

Parameters:

Returns:



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/acpc_poker_types/match_state.rb', line 347

def every_action(game_def)
  @players = players_at_hand_start(
    @stack_sizes || game_def.chip_stacks,
    game_def.blinds
  )

  @next_to_act = game_def.first_player_positions.first
  @player_acting_sequence = []
  @precise_betting_sequence = []
  @min_wager_by = game_def.min_wagers.first

  walk_over_betting_sequence! game_def
  compute_winning_players! game_def
  finish_update! game_def

  @players
end

#first_state_of_first_round?Boolean

Returns Reports whether or not it is the first state of the first round.

Returns:

  • (Boolean)

    Reports whether or not it is the first state of the first round.



287
288
289
290
291
# File 'lib/acpc_poker_types/match_state.rb', line 287

def first_state_of_first_round?
  return @first_state_of_first_round unless @first_state_of_first_round.nil?

  @first_state_of_first_round = @betting_sequence_string.empty?
end

#first_state_of_round?Boolean

Returns:

  • (Boolean)


293
294
295
296
297
# File 'lib/acpc_poker_types/match_state.rb', line 293

def first_state_of_round?
  return @first_state_of_round unless @first_state_of_round.nil?

  @first_state_of_round = ((@betting_sequence_string[-1] == BETTING_SEQUENCE_SEPARATOR) || first_state_of_first_round?)
end

#handHand

Returns The user’s hand.

Examples:

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

'Ad4c'

Returns:

  • (Hand)

    The user’s hand.



271
272
273
# File 'lib/acpc_poker_types/match_state.rb', line 271

def hand
  @hand ||= all_hands[@position_relative_to_dealer]
end

#hand_ended?(game_def) ⇒ Boolean

Returns true if the hand has ended, false otherwise.

Returns:

  • (Boolean)

    true if the hand has ended, false otherwise.



387
388
389
390
391
392
393
394
# File 'lib/acpc_poker_types/match_state.rb', line 387

def hand_ended?(game_def)
  return @hand_ended unless @hand_ended.nil?

  @hand_ended = (
    reached_showdown? ||
    players(game_def).count { |player| player.folded? } >= number_of_players - 1
  )
end

#last_actionPokerAction

Returns The last action taken.

Returns:



305
306
307
308
309
310
311
# File 'lib/acpc_poker_types/match_state.rb', line 305

def last_action
  @last_action ||= if betting_sequence.flatten.empty?
    nil
  else
    betting_sequence[round_in_which_last_action_taken].last
  end
end

Returns The legal actions for the next player to act.

Returns:

  • (Array<PokerAction>)

    The legal actions for the next player to act.



418
419
420
421
422
423
424
425
426
427
428
# File 'lib/acpc_poker_types/match_state.rb', line 418

def legal_actions(game_def)
  return [] unless next_to_act(game_def)
  return @legal_actions unless @legal_actions.nil?

  @legal_actions = players(game_def).legal_actions(
    next_to_act(game_def),
    round,
    game_def,
    min_wager_by(game_def)
  )
end

#min_wager_by(game_def) ⇒ ChipStack

Returns Minimum wager by.

Returns:



411
412
413
414
415
# File 'lib/acpc_poker_types/match_state.rb', line 411

def min_wager_by(game_def)
  every_action(game_def) if @min_wager_by.nil?

  @min_wager_by
end

#next_to_act(game_def) ⇒ Object



365
366
367
368
369
370
371
372
373
374
# File 'lib/acpc_poker_types/match_state.rb', line 365

def next_to_act(game_def)
  unless @next_to_act
    if hand_ended?(game_def)
      @next_to_act = nil
    else
      every_action game_def
    end
  end
  @next_to_act
end

#number_of_actions_this_handInteger

Returns The number of actions in the current hand.

Returns:

  • (Integer)

    The number of actions in the current hand.



319
320
321
322
323
# File 'lib/acpc_poker_types/match_state.rb', line 319

def number_of_actions_this_hand
  @number_of_actions_this_hand ||= betting_sequence.inject(0) do |sum, sequence_per_round|
    sum += sequence_per_round.length
  end
end

#number_of_actions_this_roundInteger

Returns The number of actions in the current round.

Returns:

  • (Integer)

    The number of actions in the current round.



314
315
316
# File 'lib/acpc_poker_types/match_state.rb', line 314

def number_of_actions_this_round
  @number_of_actions_this_round ||= betting_sequence[round].length
end

#number_of_playersInteger

Returns The number of players in this match.

Returns:

  • (Integer)

    The number of players in this match.



300
301
302
# File 'lib/acpc_poker_types/match_state.rb', line 300

def number_of_players
  @number_of_players ||= @hands_string.count(HAND_SEPARATOR) + 1
end

#opponent_handsArray

Returns The list of opponent hole card hands.

Examples:

If there are two opponents, one with AhKs and the other with QdJc, then

list_of_opponents_hole_cards == [AhKs:Hand, QdJc:Hand]

Returns:

  • (Array)

    The list of opponent hole card hands.



278
279
280
281
282
283
284
# File 'lib/acpc_poker_types/match_state.rb', line 278

def opponent_hands
  return @opponent_hands unless @opponent_hands.nil?

  @opponent_hands = all_hands.dup
  @opponent_hands.delete_at @position_relative_to_dealer
  @opponent_hands
end

#opponents_cards_visible?Boolean

Returns:

  • (Boolean)


400
401
402
403
404
# File 'lib/acpc_poker_types/match_state.rb', line 400

def opponents_cards_visible?
  return @opponents_cards_visible unless @opponents_cards_visible.nil?

  @opponents_cards_visible = all_hands.count { |h| !h.empty? } > 1 # At least one opponent hand visible
end

#player_acting_sequence(game_def) ⇒ Object



380
381
382
383
384
# File 'lib/acpc_poker_types/match_state.rb', line 380

def player_acting_sequence(game_def)
  every_action(game_def) unless @player_acting_sequence

  @player_acting_sequence
end

#players(game_def) ⇒ Object



376
377
378
# File 'lib/acpc_poker_types/match_state.rb', line 376

def players(game_def)
  @players ||= every_action(game_def)
end

#players_at_hand_start(stacks, blinds) ⇒ HandPlayerGroup

when the hand began.

Parameters:

  • stacks (Array<#to_f>)
  • blinds (Array<#to_f>)

Returns:



341
342
343
# File 'lib/acpc_poker_types/match_state.rb', line 341

def players_at_hand_start(stacks, blinds)
  HandPlayerGroup.new all_hands, stacks, blinds
end

#pot(game_def) ⇒ Object



406
407
408
# File 'lib/acpc_poker_types/match_state.rb', line 406

def pot(game_def)
  @pot ||= players(game_def).map { |player| player.contributions }.flatten.inject(:+)
end

#reached_showdown?Boolean

Returns:

  • (Boolean)


396
397
398
# File 'lib/acpc_poker_types/match_state.rb', line 396

def reached_showdown?
  opponents_cards_visible?
end

#roundInteger

Returns The zero indexed current round number.

Returns:

  • (Integer)

    The zero indexed current round number.



264
265
266
# File 'lib/acpc_poker_types/match_state.rb', line 264

def round
  @round ||= @betting_sequence_string.count BETTING_SEQUENCE_SEPARATOR
end

#round_in_which_last_action_takenObject



325
326
327
328
329
330
331
332
333
334
335
# File 'lib/acpc_poker_types/match_state.rb', line 325

def round_in_which_last_action_taken
  @round_in_which_last_action_taken ||= if first_state_of_first_round?
    nil
  else
    if @betting_sequence_string[-1] == BETTING_SEQUENCE_SEPARATOR
      round - 1
    else
      round
    end
  end
end

#to_strString Also known as: to_s

Returns The MatchState in raw text form.

Returns:

  • (String)

    The MatchState in raw text form.



186
187
188
189
190
191
192
193
194
195
# File 'lib/acpc_poker_types/match_state.rb', line 186

def to_str
  @str ||= MatchState.build_match_state_string(
    @position_relative_to_dealer,
    @hand_number,
    @betting_sequence_string,
    @hands_string,
    @community_cards_string,
    @stack_sizes
  )
end