Class: C4::Model::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/c4/model/game.rb

Constant Summary collapse

P1 =
'o'
P2 =
'x'
ROWS =
6
COLUMNS =
7
WINNING_LENGTH =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



20
21
22
23
24
# File 'lib/c4/model/game.rb', line 20

def initialize
  @board = Board.new(ROWS, COLUMNS)
  @players = [P1, P2]
  @current_player_index = 0
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



12
13
14
# File 'lib/c4/model/game.rb', line 12

def board
  @board
end

#current_player_indexObject (readonly)

Returns the value of attribute current_player_index.



12
13
14
# File 'lib/c4/model/game.rb', line 12

def current_player_index
  @current_player_index
end

#playersObject (readonly)

Returns the value of attribute players.



12
13
14
# File 'lib/c4/model/game.rb', line 12

def players
  @players
end

Instance Method Details

#current_playerObject



31
32
33
# File 'lib/c4/model/game.rb', line 31

def current_player
  players[current_player_index]
end

#impasse?Boolean

Returns:

  • (Boolean)


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

def impasse?
  board.full?
end

#play!(column) ⇒ Object



26
27
28
29
# File 'lib/c4/model/game.rb', line 26

def play!(column)
  board.put_stone!(column, current_player)
  toggle_player
end

#winnerObject



39
40
41
42
43
# File 'lib/c4/model/game.rb', line 39

def winner
  players.find do |player|
    player if valid_streak_exists?(player)
  end
end