class ConnectNGame::Game
The Game class of the connect_n_game.
Responsibility
-
This class is responsible for the overall operation of the game. It holds the playing “rack”, and the players. It also keeps a log of moves, determines if the game has been won or if a stalemate has occurred.
Notes
-
The Game class does NOT interact with the outside world. That is the job of the
UI
object.
Attributes
The current player number.
The play log.
Whose playing?
The game playing surface
The last played turn number.
Public Class Methods
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
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
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
What was the last move?
# File lib/connect_n_game/game.rb, line 87 def last_move log[-1] end
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
# File lib/connect_n_game/game.rb, line 52 def previous_player players[(@current % 2) + 1] end