Class: GameDefinition
- Inherits:
-
Object
- Object
- GameDefinition
- 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).
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).
2**31 - 1
- DEFAULT_CHIP_STACK =
INT32_MAX
- MIN_VALUES =
Returns 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.
{ :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
-
#betting_type ⇒ String
readonly
The string designating the betting type.
-
#blinds ⇒ Array
readonly
The list of blind sizes.
-
#chip_stacks ⇒ Array
readonly
The list containing the initial stack size for every player.
-
#first_player_positions ⇒ Array
readonly
The position relative to the dealer that is first to act in each round, indexed from 0.
-
#max_number_of_wagers ⇒ Array
readonly
The maximum number of wagers in each round.
-
#min_wagers ⇒ Array
readonly
The minimum wager in each round.
-
#number_of_board_cards ⇒ Array
readonly
The number of board cards in each round.
-
#number_of_hole_cards ⇒ Integer
readonly
The number of hole cards that each player is dealt.
-
#number_of_players ⇒ Integer
readonly
The number of players.
-
#number_of_ranks ⇒ Integer
readonly
The number of ranks in the deck.
-
#number_of_rounds ⇒ Integer
readonly
The number of rounds.
-
#number_of_suits ⇒ Integer
readonly
The number of suits in the deck.
Class Method Summary collapse
-
.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.
-
.default_chip_stacks(number_of_players) ⇒ Array<ChipStack>
The default list of initial stacks for every player.
- .default_first_player_positions(number_of_rounds) ⇒ Object
-
.default_max_number_of_wagers(number_of_rounds) ⇒ Array
The default maximum raise in each round.
-
.flatten_if_single_element_array(array) ⇒ Object
array
ifarray
has more than one element, the single element inarray
otherwise. -
.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’).
-
.line_is_comment_or_empty?(line) ⇒ Boolean
Checks if the given line is a comment beginning with ‘#’ or ‘;’, or empty.
- .parse_file(game_definition_file_name) ⇒ Object
Instance Method Summary collapse
- #==(other_game_definition) ⇒ Object
-
#initialize(definitions) ⇒ GameDefinition
constructor
A new instance of GameDefinition.
- #to_a ⇒ Object
- #to_s ⇒ Object (also: #to_str)
Constructor Details
#initialize(definitions) ⇒ GameDefinition
Returns a new instance of GameDefinition.
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. 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_type ⇒ String (readonly)
Returns 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 |
#blinds ⇒ Array (readonly)
Returns The list of blind sizes.
53 54 55 |
# File 'lib/acpc_poker_types/game_definition.rb', line 53 def blinds @blinds end |
#chip_stacks ⇒ Array (readonly)
Returns 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_positions ⇒ Array (readonly)
Returns 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_wagers ⇒ Array (readonly)
Returns 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_wagers ⇒ Array (readonly)
Returns 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_cards ⇒ Array (readonly)
Returns 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_cards ⇒ Integer (readonly)
Returns 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_players ⇒ Integer (readonly)
Returns 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_ranks ⇒ Integer (readonly)
Returns 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_rounds ⇒ Integer (readonly)
Returns 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_suits ⇒ Integer (readonly)
Returns 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.
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.
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.
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.
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’).
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.
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
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_a ⇒ Object
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_s ⇒ Object 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 |