class Game

Attributes

current_player[R]
status[R]

Public Class Methods

new(board, player1, player2) click to toggle source
# File lib/tic_tac_toe_vj/game.rb, line 8
def initialize(board, player1, player2)
  @board = board
  @player1 = player1
  @player2 = player2
  @current_player = player1
  @status = GameStatus::NEXT_MOVE
end

Public Instance Methods

play_move(location) click to toggle source
# File lib/tic_tac_toe_vj/game.rb, line 22
def play_move(location)
  if @status == GameStatus::NEXT_MOVE
    @board.mark_tile(location, @current_player.symbol)
    @status = check_game_status
    @current_player = change_player
  end
  return status
end
restart() click to toggle source
# File lib/tic_tac_toe_vj/game.rb, line 16
def restart
  @current_player = @player1
  @status = GameStatus::NEXT_MOVE
  @board.restart
end

Private Instance Methods

change_player() click to toggle source
# File lib/tic_tac_toe_vj/game.rb, line 54
def change_player
  return @current_player == @player1 ? @player2 : @player1
end
check_game_status() click to toggle source
# File lib/tic_tac_toe_vj/game.rb, line 42
def check_game_status
  number_of_terns = @board.number_of_terns
  if number_of_terns < 5
    return GameStatus::NEXT_MOVE
  elsif is_win?
    return winner
  elsif number_of_terns == 9
    return GameStatus::DRAW
  end
  return GameStatus::NEXT_MOVE
end
is_win?() click to toggle source
# File lib/tic_tac_toe_vj/game.rb, line 33
def is_win?
  @board.is_game_finish?(@current_player.symbol)
end
winner() click to toggle source
# File lib/tic_tac_toe_vj/game.rb, line 37
def winner
  @current_player = change_player
  return @current_player == @player1 ? GameStatus::PLAYER_1_WINS : GameStatus::PLAYER_2_WINS
end