class Game

Класс Game: Управляет игровым процессом

Attributes

board[R]
current_player[RW]
first_player[RW]
judge[RW]
second_player[RW]

Public Class Methods

new(difficulty = MediumAI) click to toggle source
# File lib/ttt-cli/game.rb, line 24
def initialize(difficulty = MediumAI)
  @board = Board.new(self)
  @difficulty = difficulty
  @first_player = Players::Human.new(token: X)
  @second_player = Players::Computer.new(token: O, game: self)
  @current_player = @first_player
  @judge = Judge.new(self)
end
setup_game(game) click to toggle source
# File lib/ttt-cli/game.rb, line 16
def self.setup_game(game)
  game.update_players!
  game.set_players_tokens(game.get_user_token)
  game.who_goes_first
  CommandLine::Display.print_board(game.board)
  game
end
start() click to toggle source
# File lib/ttt-cli/game.rb, line 9
def self.start
  CommandLine::Display.welcome_banner
  Game.set_game_mode(Game.game_mode)
  new_game = new(Game.set_difficulty)
  Game.setup_game(new_game)
end

Public Instance Methods

draw?() click to toggle source
# File lib/ttt-cli/game.rb, line 103
def draw?
  @board.full? && !won?
end
get_user_token() click to toggle source
# File lib/ttt-cli/game.rb, line 63
def get_user_token
  if @@game_mode == :singleplayer
    CommandLine::Display.user_token
  end
end
hotseat_mode() click to toggle source
# File lib/ttt-cli/game.rb, line 51
def hotseat_mode
  @first_player = Players::Human.new(token: X, name: 'Player 1')
  @second_player = Players::Human.new(token: O, name: 'Player 2')
  @current_player = @first_player
end
increase_counter() click to toggle source
# File lib/ttt-cli/game.rb, line 128
def increase_counter
  if draw?
    @@draws += 1
  elsif won?(@first_player)
    @@wins += 1
  elsif won?(@second_player)
    @@losses += 1
  end
end
observer_mode() click to toggle source
# File lib/ttt-cli/game.rb, line 57
def observer_mode
  @first_player = Players::Computer.new(token: X, name: 'Connor', game: self)
  @second_player = Players::Computer.new(token: O, name: 'William', game: self)
  @current_player = @first_player
end
over?() click to toggle source
# File lib/ttt-cli/game.rb, line 99
def over?
  draw? || won?
end
over_message() click to toggle source
# File lib/ttt-cli/game.rb, line 111
def over_message
  increase_counter
  CommandLine::Display.print_board(@board)
  print_winner
end
print_winner() click to toggle source

Метод для объявления победителя

set_players_tokens(token) click to toggle source

Метод который устанавливает символы игрокам По умолчанию игрок - X, компьютер - O

# File lib/ttt-cli/game.rb, line 71
def set_players_tokens(token)
  if @@game_mode == :singleplayer
    if token != 'X'
      @first_player.token = O
      @second_player.token = X
    end
  end
end
singleplayer_mode() click to toggle source
# File lib/ttt-cli/game.rb, line 47
def singleplayer_mode
  @second_player = Players::Computer.new(token: O, game: self)
end
start_new_game?() click to toggle source
# File lib/ttt-cli/game.rb, line 33
def start_new_game?
  CommandLine::Display.play_again
end
switch_player() click to toggle source

Метод для переключения на следующего игрока

# File lib/ttt-cli/game.rb, line 90
def switch_player
  case @current_player
  when @first_player
    @current_player = @second_player
  else
    @current_player = @first_player
  end
end
update_players!() click to toggle source
# File lib/ttt-cli/game.rb, line 37
def update_players!
  if @@game_mode == :singleplayer
    singleplayer_mode
  elsif @@game_mode == :hotseat
    hotseat_mode
  else
    observer_mode
  end
end
who_goes_first() click to toggle source

Метод для выбора игрока, который будет ходить первым. По умолчанию первым ходит игрок.

# File lib/ttt-cli/game.rb, line 82
def who_goes_first
  case @first_player.token
  when O
    @current_player = @second_player
  end
end
won?(player = @current_player) click to toggle source
# File lib/ttt-cli/game.rb, line 107
def won?(player = @current_player)
  @judge.is_combo?(player)
end