Class: AcpcPokerTypes::MatchState

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

Constant Summary collapse

LABEL =

Returns Label for match state strings.

Returns:

  • (String)

    Label for match state strings.

'MATCHSTATE'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_match_state) ⇒ MatchState

Returns a new instance of MatchState.

Parameters:

  • raw_match_state (String)

    A raw match state string to be parsed.

Raises:

  • IncompleteMatchState



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/acpc_poker_types/match_state.rb', line 81

def initialize(raw_match_state)
  raise IncompleteMatchState, raw_match_state if AcpcPokerTypes::MatchState.line_is_comment_or_empty? raw_match_state

  if raw_match_state.match(
    /#{LABEL}:(\d+):(\d+):([\d#{AcpcPokerTypes::PokerAction::CONCATONATED_ACTIONS}\/]*):([|#{AcpcPokerTypes::Card::CONCATONATED_CARDS}]+)\/*([\/#{AcpcPokerTypes::Card::CONCATONATED_CARDS}]*)/)
    @position_relative_to_dealer = $1.to_i
    @hand_number = $2.to_i
    @betting_sequence = parse_betting_sequence $3
    @list_of_hole_card_hands = parse_list_of_hole_card_hands $4
    @board_cards = parse_board_cards $5
  end

  raise IncompleteMatchState, raw_match_state if incomplete_match_state?
end

Instance Attribute Details

#betting_sequenceArray<Array<AcpcPokerTypes::PokerAction>> (readonly)

Returns The sequence of betting actions.

Returns:



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

def betting_sequence
  @betting_sequence
end

#board_cardsAcpcPokerTypes::BoardCards (readonly)

Returns All visible community cards on the board.

Returns:



34
35
36
# File 'lib/acpc_poker_types/match_state.rb', line 34

def board_cards
  @board_cards
end

#first_seat_in_each_roundArray<Integer> (readonly)

Returns The list of first seats for each round.

Returns:

  • (Array<Integer>)

    The list of first seats for each round.



37
38
39
# File 'lib/acpc_poker_types/match_state.rb', line 37

def first_seat_in_each_round
  @first_seat_in_each_round
end

#hand_numberInteger (readonly)

Returns The hand number.

Returns:

  • (Integer)

    The hand number.



25
26
27
# File 'lib/acpc_poker_types/match_state.rb', line 25

def hand_number
  @hand_number
end

#list_of_hole_card_handsArray<AcpcPokerTypes::Hand> (readonly)

Returns The list of visible hole card sets for each player.

Returns:



31
32
33
# File 'lib/acpc_poker_types/match_state.rb', line 31

def list_of_hole_card_hands
  @list_of_hole_card_hands
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.



22
23
24
# File 'lib/acpc_poker_types/match_state.rb', line 22

def position_relative_to_dealer
  @position_relative_to_dealer
end

Class Method Details

.build_match_state_string(position_relative_to_dealer, hand_number, betting_sequence, all_hole_cards, board_cards) ⇒ 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_acpc, #empty?)

    All the community cards on the board.

Returns:

  • (String)

    The constructed match state string.



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

def self.build_match_state_string(
  position_relative_to_dealer,
  hand_number,
  betting_sequence,
  all_hole_cards,
  board_cards
)
  string = "#{LABEL}:#{position_relative_to_dealer}:#{hand_number}:#{betting_sequence}:#{all_hole_cards}"
  string << board_cards.to_acpc if board_cards && !board_cards.empty?
  string
end

.line_is_comment_or_empty?(line) ⇒ Boolean

Checks if the given line is a comment beginning with ‘#’ or ‘;’, or empty.

Parameters:

  • line (String)

Returns:

  • (Boolean)

    True if line is a comment or empty, false otherwise.



75
76
77
# File 'lib/acpc_poker_types/match_state.rb', line 75

def self.line_is_comment_or_empty?(line)
  line.nil? || line.match(/^\s*[#;]/) || line.empty?
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.



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

def self.receive(connection)
  AcpcPokerTypes::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:

Returns:

  • (Boolean)

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



111
112
113
# File 'lib/acpc_poker_types/match_state.rb', line 111

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

#betting_sequence_string(betting_sequence = @betting_sequence) ⇒ String

Returns The ACPC string created by the given betting sequence, betting_sequence.

Parameters:

  • betting_sequence=@betting_sequence (Array<Action>)

    The sequence of actions to link into an ACPC string.

Returns:

  • (String)

    The ACPC string created by the given betting sequence, betting_sequence.



166
167
168
169
170
171
172
# File 'lib/acpc_poker_types/match_state.rb', line 166

def betting_sequence_string(betting_sequence=@betting_sequence)
  (round + 1).times.inject('') do |string, i|
    string << (betting_sequence[i].map { |action| action.to_s }).join('')
    string << '/' unless i == round
    string
  end
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.



175
176
177
# File 'lib/acpc_poker_types/match_state.rb', line 175

def first_state_of_first_round?
  (0 == number_of_actions_this_hand)
end

#last_action(betting_sequence = @betting_sequence) ⇒ AcpcPokerTypes::PokerAction

Returns The last action taken.

Parameters:

  • betting_sequence (Array<Array<AcpcPokerTypes::PokerAction>>) (defaults to: @betting_sequence)

    The betting sequence from which the last action should be retrieved.

Returns:

Raises:

  • NoActionsHaveBeenTaken if no actions have been taken.



121
122
123
124
125
126
127
128
129
# File 'lib/acpc_poker_types/match_state.rb', line 121

def last_action(betting_sequence=@betting_sequence)
  if betting_sequence.nil? || betting_sequence.empty?
    nil
  elsif betting_sequence.last.last
    betting_sequence.last.last
  else
    last_action(betting_sequence.reject{ |elem| elem.equal?(betting_sequence.last) })
  end
end

#list_of_opponents_hole_cardsArray

Returns The list of opponent hole cards that are visible.

Examples:

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

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

Returns:

  • (Array)

    The list of opponent hole cards that are visible.



141
142
143
144
145
# File 'lib/acpc_poker_types/match_state.rb', line 141

def list_of_opponents_hole_cards
  local_list_of_hole_card_hands = @list_of_hole_card_hands.dup
  local_list_of_hole_card_hands.delete_at @position_relative_to_dealer
  local_list_of_hole_card_hands
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.



156
157
158
159
160
# File 'lib/acpc_poker_types/match_state.rb', line 156

def 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.



153
# File 'lib/acpc_poker_types/match_state.rb', line 153

def 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.



116
# File 'lib/acpc_poker_types/match_state.rb', line 116

def number_of_players() @list_of_hole_card_hands.length end

#player_position_relative_to_selfObject



179
180
181
# File 'lib/acpc_poker_types/match_state.rb', line 179

def player_position_relative_to_self
  number_of_players - 1
end

#roundInteger

Returns The zero indexed current round number.

Returns:

  • (Integer)

    The zero indexed current round number.



148
149
150
# File 'lib/acpc_poker_types/match_state.rb', line 148

def round
  @betting_sequence.length - 1
end

#round_in_which_last_action_takenObject



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/acpc_poker_types/match_state.rb', line 183

def round_in_which_last_action_taken
  unless number_of_actions_this_hand > 0
    nil
  else
    if number_of_actions_this_round < 1
      round - 1
    else
      round
    end
  end
end

#to_strString Also known as: to_s

Returns The AcpcPokerTypes::MatchState in raw text form.

Returns:

  • (String)

    The AcpcPokerTypes::MatchState in raw text form.



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

def to_str
  AcpcPokerTypes::MatchState.build_match_state_string(
    @position_relative_to_dealer,
    @hand_number, betting_sequence_string,
    hole_card_strings,
    @board_cards
  )
end

#users_hole_cardsAcpcPokerTypes::Hand

Returns The user’s hole cards.

Examples:

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

'Ad4c'

Returns:



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

def users_hole_cards
  @list_of_hole_card_hands[@position_relative_to_dealer]
end