class ConnectNGame::Game

The Game class of the connect_n_game.
Responsibility


Notes

Attributes

current[R]

The current player number.

log[R]

The play log.

players[R]

Whose playing?

rack[R]

The game playing surface

turn[R]

The last played turn number.

Public Class Methods

new(player_ex, player_oh, game_size=4) click to toggle source

Create an instance of a game object.
Parameters

  • player_ex - The player who moves first

  • payer_oh - The player who moves next

  • game_size - The size of the game connection (4..8). Default: 4


Returns

  • An instance of a Game.

# File lib/connect_n_game/game.rb, line 39
def initialize(player_ex, player_oh, game_size=4)
  @game_size = game_size
  #Set up player related data.
  @players = { 1 => player_ex, 2 => player_oh }
end

Public Instance Methods

current_player() click to toggle source

What player moves next?
Returns

  • An instance of a Player.

# File lib/connect_n_game/game.rb, line 48
def current_player
  players[current]
end
game_initialize() click to toggle source

Get ready to start a game

# File lib/connect_n_game/game.rb, line 57
def game_initialize
  #Set up game play data.
  @turn = 0
  @current = 1
  @rack = Rack.new(@game_size)
  @log  = []

  self
end
last_move() click to toggle source

What was the last move?

# File lib/connect_n_game/game.rb, line 87
def last_move
  log[-1]
end
next_move() click to toggle source

Play the next move!
Returns

  • :victory, :stalemate, :continue, or :invalid_move

# File lib/connect_n_game/game.rb, line 70
def next_move
  channel = current_player.make_move(self, current)
  score = rack.play_channel(channel, current)
  @log << channel
  @turn += 1

  if score >= rack.order
    :victory
  elsif rack.rack_full?
    :stalemate
  else
    @current = (@current % 2) + 1
    :continue
  end
end
previous_player() click to toggle source
# File lib/connect_n_game/game.rb, line 52
def previous_player
  players[(@current % 2) + 1]
end