Class: AcpcPokerTypes::GameDefinition

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

Constant Summary collapse

UINT8_MAX =
2**8 - 1
DEFAULT_MAX_NUMBER_OF_WAGERS =
UINT8_MAX
INT32_MAX =
2**31 - 1
DEFAULT_CHIP_STACK =
INT32_MAX
MIN_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
}
LIMIT =
'limit'
NO_LIMIT =
'nolimit'
BETTING_TYPES =
{
  :limit => LIMIT,
  :nolimit => NO_LIMIT
}
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'
}
ALL_PLAYER_ALL_ROUND_DEFS =
[
  DEFINITIONS[:@number_of_players],
  DEFINITIONS[:@number_of_rounds],
  DEFINITIONS[:@number_of_suits],
  DEFINITIONS[:@number_of_ranks],
  DEFINITIONS[:@number_of_hole_cards]
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definitions) ⇒ GameDefinition



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/acpc_poker_types/game_definition.rb', line 166

def initialize(definitions)
  @raise_sizes = nil
  @array = nil
  @betting_type = BETTING_TYPES[:limit]
  @number_of_players = MIN_VALUES[:@number_of_players]
  @blinds = @number_of_players.times.inject([]) { |blinds, i| blinds << 0 }
  @number_of_rounds = MIN_VALUES[:@number_of_rounds]
  @number_of_board_cards = @number_of_rounds.times.inject([]) { |cards, i| cards << 0 }
  @first_player_positions = self.class.default_first_player_positions @number_of_rounds
  @max_number_of_wagers = self.class.default_max_number_of_wagers @number_of_rounds
  @chip_stacks = self.class.default_chip_stacks @number_of_players
  @number_of_suits = MIN_VALUES[:@number_of_suits]
  @number_of_ranks = MIN_VALUES[:@number_of_ranks]
  @number_of_hole_cards = MIN_VALUES[:@number_of_hole_cards]

  if definitions.is_a?(Hash)
    assign_definitions! definitions
  else
    parse_definitions! definitions
  end

  @chip_stacks = self.class.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)



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

def betting_type
  @betting_type
end

#blindsArray (readonly)



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

def blinds
  @blinds
end

#chip_stacksArray (readonly)



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

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]


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

def first_player_positions
  @first_player_positions
end

#max_number_of_wagersArray (readonly)



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

def max_number_of_wagers
  @max_number_of_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]


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)



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

def number_of_hole_cards
  @number_of_hole_cards
end

#number_of_playersInteger (readonly)



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)



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

def number_of_ranks
  @number_of_ranks
end

#number_of_roundsInteger (readonly)



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)



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

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 frcom a game definition file for a game definition name and returns the given default value unless there is a match.



138
139
140
141
142
143
144
145
146
147
# File 'lib/acpc_poker_types/game_definition.rb', line 138

def self.check_game_def_line_for_definition(line, definition_name)
  if line.match(/^\s*#{definition_name}\s*=\s*([\d\s]+)/i)
    value = $1.chomp.split(/\s+/).map{ |elem| elem.to_i }
    if ALL_PLAYER_ALL_ROUND_DEFS.include? definition_name
      value.shift
    else
      value
    end
  end
end

.default_chip_stacks(number_of_players) ⇒ Array<Integer>



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

def self.default_chip_stacks(number_of_players)
  number_of_players.times.map { DEFAULT_CHIP_STACK }
end

.default_first_player_positions(number_of_rounds) ⇒ Object



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

def self.default_first_player_positions(number_of_rounds)
  number_of_rounds.times.map { 0 }
end

.default_max_number_of_wagers(number_of_rounds) ⇒ Array



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

def self.default_max_number_of_wagers(number_of_rounds)
  number_of_rounds.times.map { DEFAULT_MAX_NUMBER_OF_WAGERS }
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').



155
156
157
# File 'lib/acpc_poker_types/game_definition.rb', line 155

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.



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

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

.parse_file(game_definition_file_name) ⇒ Object



160
161
162
# File 'lib/acpc_poker_types/game_definition.rb', line 160

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



228
229
230
# File 'lib/acpc_poker_types/game_definition.rb', line 228

def ==(other_game_definition)
  to_h == other_game_definition.to_h
end

#min_wagersObject



232
233
234
235
236
237
238
# File 'lib/acpc_poker_types/game_definition.rb', line 232

def min_wagers
  @min_wagers ||= if @raise_sizes
    @raise_sizes
  else
    @number_of_rounds.times.map { |i| @blinds.max }
  end
end

#to_aObject



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/acpc_poker_types/game_definition.rb', line 209

def to_a
  return @array unless @array.nil?

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

#to_hObject



202
203
204
205
206
207
# File 'lib/acpc_poker_types/game_definition.rb', line 202

def to_h
  @hash ||= DEFINITIONS.keys.inject({betting_type: @betting_type}) do |h, instance_variable_symbol|
    h[instance_variable_symbol[1..-1].to_sym] = instance_variable_get(instance_variable_symbol)
    h
  end
end

#to_sObject Also known as: to_str



196
197
198
# File 'lib/acpc_poker_types/game_definition.rb', line 196

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