Class: Baron::Game

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/baron/game.rb

Overview

Base class for all games, this contains the logic for maintaining a standard 18xx game, which can be customized through configuration.

The basic information for the game is defined in yml files in the games directory.

Constant Summary collapse

BANK_SIZE =

The size of the bank.

In many games the bank size involves an end game trigger, however in Baron we calculate that through other means. We just need a pool of money large enough to pay out everything.

100_000_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules, players) ⇒ Game

Construct the game

order in which they are going to take turns. The player starting the game with priority deal should be first in the array.

Examples:

Baron::Game.new(rules, players)

Parameters:


83
84
85
86
87
88
89
90
91
92
93
# File 'lib/baron/game.rb', line 83

def initialize(rules, players)
  @rules = rules
  @players = players.freeze
  @flow = GameRoundFlow.new(self)
  init_bank
  init_certificates
  init_market
  init_initial_offering
  init_starting_cash
  init_trains
end

Instance Attribute Details

#bankBaron::Bank (readonly)

The bank for this game

Examples:

game.bank

Returns:


26
27
28
# File 'lib/baron/game.rb', line 26

def bank
  @bank
end

#initial_offeringBaron::InitialOffering (readonly)

The initial offering

Examples:

game.initial_offering

Returns:


35
36
37
# File 'lib/baron/game.rb', line 35

def initial_offering
  @initial_offering
end

#marketBaron::Market (readonly)

The market

Examples:

game.market

Returns:


44
45
46
# File 'lib/baron/game.rb', line 44

def market
  @market
end

#playersArray<Baron::Player> (readonly)

The players involved in this game

Examples:

game.players

Returns:


53
54
55
# File 'lib/baron/game.rb', line 53

def players
  @players
end

#rulesBaron::Rules (readonly)

The rules of this game

Examples:

game.rules

Returns:


62
63
64
# File 'lib/baron/game.rb', line 62

def rules
  @rules
end

#unavailable_certificates_poolBaron::UnavailableCertificatesPool (readonly)

The unavailable certificates pool

Examples:

game.unavailable_certificates_pool

Returns:


71
72
73
# File 'lib/baron/game.rb', line 71

def unavailable_certificates_pool
  @unavailable_certificates_pool
end

Instance Method Details

#add_next_level_of_trainsvoid

This method returns an undefined value.

Place the next level of trains in the initial offering

This will generally be triggered at the start of the game or after a train has been purchased. The trains will be added if the appopriate conditions have been met.

Examples:

game.add_next_level_of_trains

181
182
183
184
185
186
# File 'lib/baron/game.rb', line 181

def add_next_level_of_trains
  unavailable_certificates_pool.give(
    initial_offering,
    unavailable_certificates_pool.next_trains
  ) if initial_offering.trains.empty?
end

#current_playerBaron::Player

The player whose turn it is to currently act

Examples:

game.current_player

Returns:


129
130
131
# File 'lib/baron/game.rb', line 129

def current_player
  current_turn.player
end

#current_roundBaron::Round

The current round in the game

This will return a stock round, operating round, auction round or some other round.

Examples:

game.current_round

Returns:


105
106
107
# File 'lib/baron/game.rb', line 105

def current_round
  @flow.current_round
end

#current_turnBaron::Turn

The current turn in the game

This is delegated to the current round

Examples:

game.current_turn

Returns:


118
119
120
# File 'lib/baron/game.rb', line 118

def current_turn
  current_round.current_turn
end

#director(company) ⇒ Baron::Player

Get the director of a company

nil if no player holds it

Examples:

game.director(company) #=> the player who is a director

Parameters:

Returns:

  • (Baron::Player)

    The player who holds the directors certificate,


142
143
144
145
146
147
# File 'lib/baron/game.rb', line 142

def director(company)
  players.each do |player|
    return player if player.directorships.include?(company)
  end
  bank
end

#inspectString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a basic string representation of the game

Returns:

  • (String)

153
154
155
# File 'lib/baron/game.rb', line 153

def inspect
  "#<Baron::Game:#{object_id}>"
end

#over?Boolean

Is the game over

TODO: Implement game over logic

Examples:

game.over?

Returns:

  • (Boolean)

    True if the game is over, false otherwise.


166
167
168
# File 'lib/baron/game.rb', line 166

def over?
  false
end

#phaseFixnum

The current phase of the game

The game phase is defined by the trains which have been sold. As an example, when the first 3 train has been sold, the game enters phase 3.

Examples:

game.phase

Returns:

  • (Fixnum)

    The phase of the game


198
199
200
# File 'lib/baron/game.rb', line 198

def phase
  major_companies.map(&:largest_train).compact.max || 2
end