Class: GameDefinition

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

Overview

Class that parses and manages game definition information from a game definition file.

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 => Suit::DOMAIN.length,
  :@number_of_ranks => 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',
  :@min_wagers => '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.

Raises:

  • GameDefinitionParseError



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

def initialize(definitions)
  initialize_members!
  begin
    parse_definitions! definitions
  rescue => unable_to_read_or_open_file_error
    raise GameDefinitionParseError, unable_to_read_or_open_file_error.message
  end

  @chip_stacks = GameDefinition.default_chip_stacks(@number_of_players) if @chip_stacks.empty?
  @min_wagers = default_min_wagers(@number_of_rounds) if @min_wagers.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.



15
16
17
# File 'lib/acpc_poker_types/game_definition.rb', line 15

def betting_type
  @betting_type
end

#blindsArray (readonly)

Returns The list of blind sizes.

Returns:

  • (Array)

    The list of blind sizes.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

def number_of_ranks
  @number_of_ranks
end

#number_of_roundsInteger (readonly)

Returns The number of rounds.

Returns:

  • (Integer)

    The number of rounds.



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

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.



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

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.



142
143
144
145
146
147
148
149
150
# File 'lib/acpc_poker_types/game_definition.rb', line 142

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
    GameDefinition.flatten_if_single_element_array values
  end
end

.default_chip_stacks(number_of_players) ⇒ Array<ChipStack>

Returns The default list of initial stacks for every player.

Parameters:

  • number_of_players (Integer)

    The number of players that require stacks.

Returns:

  • (Array<ChipStack>)

    The default list of initial stacks for every player.



114
115
116
117
118
# File 'lib/acpc_poker_types/game_definition.rb', line 114

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

.default_first_player_positions(number_of_rounds) ⇒ Object



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

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.



106
107
108
109
110
# File 'lib/acpc_poker_types/game_definition.rb', line 106

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.



122
123
124
# File 'lib/acpc_poker_types/game_definition.rb', line 122

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.



158
159
160
# File 'lib/acpc_poker_types/game_definition.rb', line 158

def self.game_def_line_not_informative?(line)
  GameDefinition.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.



130
131
132
# File 'lib/acpc_poker_types/game_definition.rb', line 130

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.

Raises:

  • GameDefinitionParseError



164
165
166
# File 'lib/acpc_poker_types/game_definition.rb', line 164

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

Instance Method Details

#==(other_game_definition) ⇒ Object



212
213
214
# File 'lib/acpc_poker_types/game_definition.rb', line 212

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

#to_aObject



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

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



189
190
191
# File 'lib/acpc_poker_types/game_definition.rb', line 189

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