Class: Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board, player_x, player_o) ⇒ Game

Returns a new instance of Game.



5
6
7
8
9
10
# File 'lib/game.rb', line 5

def initialize(board, player_x, player_o)
  @board = board
  @player_x = player_x
  @player_o = player_o
  @current_player = player_x
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



3
4
5
# File 'lib/game.rb', line 3

def board
  @board
end

#current_playerObject (readonly)

Returns the value of attribute current_player.



3
4
5
# File 'lib/game.rb', line 3

def current_player
  @current_player
end

#player_oObject (readonly)

Returns the value of attribute player_o.



3
4
5
# File 'lib/game.rb', line 3

def player_o
  @player_o
end

#player_xObject (readonly)

Returns the value of attribute player_x.



3
4
5
# File 'lib/game.rb', line 3

def player_x
  @player_x
end

Instance Method Details

#change_turnsObject



17
18
19
# File 'lib/game.rb', line 17

def change_turns
  @current_player = @board.current_player_marker == "X" ? @player_x : @player_o
end

#game_over?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/game.rb', line 21

def game_over?
  @board.game_over?
end

#take_turnObject



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

def take_turn
  @board = @board.place_marker(@current_player.choose_space(@board))
  change_turns
end