Class: AcpcPokerTypes::GameDefinition

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

Constant Summary collapse

UINT8_MAX =

Returns The maximum value of an eight bit unsigned integer (for consistency with the ACPC Dealer).

Returns:

  • (Integer)

    The maximum value of an eight bit unsigned integer (for consistency with the ACPC Dealer).

2**8 - 1
DEFAULT_MAX_NUMBER_OF_WAGERS =
UINT8_MAX
INT32_MAX =

Returns The maximum value of a 32 bit signed integer (for consistency with the ACPC Dealer).

Returns:

  • (Integer)

    The maximum value of a 32 bit signed integer (for consistency with the ACPC Dealer).

2**31 - 1
DEFAULT_CHIP_STACK =
INT32_MAX
MIN_VALUES =

Returns Maximum game parameter values.

Returns:

  • (Hash)

    Maximum game parameter values.

{
  :@number_of_rounds => 1,
  :@number_of_players => 2,
  :@number_of_hole_cards => 1,
  :@number_of_suits => 1,
  :@number_of_ranks => 2
}
MAX_VALUES =
{
  :@number_of_suits => AcpcPokerTypes::Suit::DOMAIN.length,
  :@number_of_ranks => AcpcPokerTypes::Rank::DOMAIN.length
}
BETTING_TYPES =

Returns Betting types understood by this class.

Returns:

  • (Hash)

    Betting types understood by this class.

{
  :limit => 'limit',
  :nolimit => 'nolimit'
}
DEFINITIONS =
{
  :@chip_stacks => 'stack',
  :@number_of_players => 'numPlayers',
  :@blinds => 'blind',
  :@raise_sizes => 'raiseSize',
  :@number_of_rounds => 'numRounds',
  :@first_player_positions => 'firstPlayer',
  :@max_number_of_wagers => 'maxRaises',
  :@number_of_suits => 'numSuits',
  :@number_of_ranks => 'numRanks',
  :@number_of_hole_cards => 'numHoleCards',
  :@number_of_board_cards => 'numBoardCards'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definitions) ⇒ GameDefinition

Returns a new instance of GameDefinition.



173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/acpc_poker_types/game_definition.rb', line 173

def initialize(definitions)
  initialize_members!
  parse_definitions! definitions

  @chip_stacks = AcpcPokerTypes::GameDefinition.default_chip_stacks(@number_of_players) if @chip_stacks.empty?

  unless @first_player_positions.any? { |pos| pos <= 0 }
    @first_player_positions.map! { |position| position - 1 }
  end

  sanity_check_game_definitions!
end

Instance Attribute Details

#betting_typeString (readonly)

Returns The string designating the betting type.

Returns:

  • (String)

    The string designating the betting type.



19
20
21
# File 'lib/acpc_poker_types/game_definition.rb', line 19

def betting_type
  @betting_type
end

#blindsArray (readonly)

Returns The list of blind sizes.

Returns:

  • (Array)

    The list of blind sizes.



57
58
59
# File 'lib/acpc_poker_types/game_definition.rb', line 57

def blinds
  @blinds
end

#chip_stacksArray (readonly)

Returns The list containing the initial stack size for every player.

Returns:

  • (Array)

    The list containing the initial stack size for every player.



45
46
47
# File 'lib/acpc_poker_types/game_definition.rb', line 45

def chip_stacks
  @chip_stacks
end

#first_player_positionsArray (readonly)

Returns The position relative to the dealer that is first to act in each round, indexed from 0.

Examples:

The usual Texas hold’em sequence would look like this:

first_player_positions == [1, 0, 0, 0]

Returns:

  • (Array)

    The position relative to the dealer that is first to act in each round, indexed from 0.



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

def first_player_positions
  @first_player_positions
end

#max_number_of_wagersArray (readonly)

Returns The maximum number of wagers in each round.

Returns:

  • (Array)

    The maximum number of wagers in each round.



42
43
44
# File 'lib/acpc_poker_types/game_definition.rb', line 42

def max_number_of_wagers
  @max_number_of_wagers
end

#min_wagersArray (readonly)

Returns The minimum wager in each round.

Returns:

  • (Array)

    The minimum wager in each round.



33
34
35
# File 'lib/acpc_poker_types/game_definition.rb', line 33

def min_wagers
  @min_wagers
end

#number_of_board_cardsArray (readonly)

Returns The number of board cards in each round.

Examples:

The usual Texas hold’em sequence would look like this:

number_of_board_cards == [0, 3, 1, 1]

Returns:

  • (Array)

    The number of board cards in each round.



30
31
32
# File 'lib/acpc_poker_types/game_definition.rb', line 30

def number_of_board_cards
  @number_of_board_cards
end

#number_of_hole_cardsInteger (readonly)

Returns The number of hole cards that each player is dealt.

Returns:

  • (Integer)

    The number of hole cards that each player is dealt.



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

def number_of_hole_cards
  @number_of_hole_cards
end

#number_of_playersInteger (readonly)

Returns The number of players.

Returns:

  • (Integer)

    The number of players.



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

def number_of_players
  @number_of_players
end

#number_of_ranksInteger (readonly)

Returns The number of ranks in the deck.

Returns:

  • (Integer)

    The number of ranks in the deck.



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

def number_of_ranks
  @number_of_ranks
end

#number_of_roundsInteger (readonly)

Returns The number of rounds.

Returns:

  • (Integer)

    The number of rounds.



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

def number_of_rounds
  @number_of_rounds
end

#number_of_suitsInteger (readonly)

Returns The number of suits in the deck.

Returns:

  • (Integer)

    The number of suits in the deck.



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

def number_of_suits
  @number_of_suits
end

Class Method Details

.check_game_def_line_for_definition(line, definition_name) ⇒ Object

Checks a given line from a game definition file for a game definition name and returns the given default value unless there is a match.

Parameters:

  • line (String, #match)

    A line from a game definition file.

  • definition_name (String)

    The name of the game definition that is being checked for in line.

Returns:

  • The game definition value in line if line contains the game definition referred to by definition_name, nil otherwise.



146
147
148
149
150
151
152
153
154
# File 'lib/acpc_poker_types/game_definition.rb', line 146

def self.check_game_def_line_for_definition(line, definition_name)
  if line.match(/^\s*#{definition_name}\s*=\s*([\d\s]+)/i)
    values = $1.chomp.split(/\s+/)
    (0..values.length-1).each do |i|
      values[i] = values[i].to_i
    end
    flatten_if_single_element_array values
  end
end

.default_chip_stacks(number_of_players) ⇒ Array<AcpcPokerTypes::ChipStack>

Returns The default list of initial stacks for every player.

Parameters:

  • number_of_players (Integer)

    The number of players that require stacks.

Returns:



118
119
120
121
122
# File 'lib/acpc_poker_types/game_definition.rb', line 118

def self.default_chip_stacks(number_of_players)
  number_of_players.to_i.times.inject([]) do |list, i|
    list << AcpcPokerTypes::ChipStack.new(DEFAULT_CHIP_STACK)
  end
end

.default_first_player_positions(number_of_rounds) ⇒ Object



103
104
105
106
107
# File 'lib/acpc_poker_types/game_definition.rb', line 103

def self.default_first_player_positions(number_of_rounds)
  number_of_rounds.to_i.times.inject([]) do |list, i|
    list << 0
  end
end

.default_max_number_of_wagers(number_of_rounds) ⇒ Array

Returns The default maximum raise in each round.

Returns:

  • (Array)

    The default maximum raise in each round.



110
111
112
113
114
# File 'lib/acpc_poker_types/game_definition.rb', line 110

def self.default_max_number_of_wagers(number_of_rounds)
  number_of_rounds.to_i.times.inject([]) do |list, i|
    list << DEFAULT_MAX_NUMBER_OF_WAGERS
  end
end

.flatten_if_single_element_array(array) ⇒ Object

Returns array if array has more than one element, the single element in array otherwise.

Parameters:

  • array (Array)

    The array to flatten into a single element.

Returns:

  • array if array has more than one element, the single element in array otherwise.



126
127
128
# File 'lib/acpc_poker_types/game_definition.rb', line 126

def self.flatten_if_single_element_array(array)
  if 1 == array.length then array[0] else array end
end

.game_def_line_not_informative?(line) ⇒ String, Boolean

Checks if the given line from a game definition file is informative or not (in which case the line is either: a comment beginning with ‘#’, empty, or contains ‘gamedef’).

Returns:

  • (String)

    line The line to check.

  • (Boolean)

    true if the line is not informative, false otherwise.



162
163
164
# File 'lib/acpc_poker_types/game_definition.rb', line 162

def self.game_def_line_not_informative?(line)
  line_is_comment_or_empty?(line) || line.match(/\s*gamedef\s*/i)
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.



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

def self.line_is_comment_or_empty?(line)
  line.nil? || line.match(/^\s*[#;]/) || line.empty?
end

.parse_file(game_definition_file_name) ⇒ Object

Parameters:

  • game_definition_file_name (String)

    The name of the game definition file that this instance should parse.



167
168
169
# File 'lib/acpc_poker_types/game_definition.rb', line 167

def self.parse_file(game_definition_file_name)
  File.open(game_definition_file_name, 'r') { |file|  parse file }
end

Instance Method Details

#==(other_game_definition) ⇒ Object



209
210
211
# File 'lib/acpc_poker_types/game_definition.rb', line 209

def ==(other_game_definition)
  Set.new(to_a) == Set.new(other_game_definition.to_a)
end

#to_aObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/acpc_poker_types/game_definition.rb', line 192

def to_a
  list_of_lines = []
  list_of_lines << BETTING_TYPES[@betting_type] if @betting_type
  list_of_lines << "stack = #{@chip_stacks.join(' ')}" unless @chip_stacks.empty?
  list_of_lines << "numPlayers = #{@number_of_players}" if @number_of_players
  list_of_lines << "blind = #{@blinds.join(' ')}" unless @blinds.empty?
  list_of_lines << "raiseSize = #{min_wagers.join(' ')}" unless min_wagers.empty?
  list_of_lines << "numRounds = #{@number_of_rounds}" if @number_of_rounds
  list_of_lines << "firstPlayer = #{(@first_player_positions.map{|p| p + 1}).join(' ')}" unless @first_player_positions.empty?
  list_of_lines << "maxRaises = #{@max_number_of_wagers.join(' ')}" unless @max_number_of_wagers.empty?
  list_of_lines << "numSuits = #{@number_of_suits}" if @number_of_suits
  list_of_lines << "numRanks = #{@number_of_ranks}" if @number_of_ranks
  list_of_lines << "numHoleCards = #{@number_of_hole_cards}" if @number_of_hole_cards
  list_of_lines << "numBoardCards = #{@number_of_board_cards.join(' ')}" unless @number_of_board_cards.empty?
  list_of_lines
end

#to_sObject Also known as: to_str



186
187
188
# File 'lib/acpc_poker_types/game_definition.rb', line 186

def to_s
  to_a.join("\n")
end