Class: Game
Overview
Класс Game: Управляет игровым процессом
Constant Summary
Constants inherited from Engine
Constants included from Emoji
Emoji::DASH, Emoji::O, Emoji::X
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#current_player ⇒ Object
Returns the value of attribute current_player.
-
#first_player ⇒ Object
Returns the value of attribute first_player.
-
#judge ⇒ Object
Returns the value of attribute judge.
-
#second_player ⇒ Object
Returns the value of attribute second_player.
Attributes inherited from Engine
Class Method Summary collapse
Instance Method Summary collapse
- #draw? ⇒ Boolean
- #get_user_token ⇒ Object
- #hotseat_mode ⇒ Object
- #increase_counter ⇒ Object
-
#initialize(difficulty = MediumAI) ⇒ Game
constructor
A new instance of Game.
- #observer_mode ⇒ Object
- #over? ⇒ Boolean
- #over_message ⇒ Object
-
#print_winner ⇒ Object
Метод для объявления победителя.
-
#set_players_tokens(token) ⇒ Object
Метод который устанавливает символы игрокам По умолчанию игрок - X, компьютер - O.
- #singleplayer_mode ⇒ Object
- #start_new_game? ⇒ Boolean
-
#switch_player ⇒ Object
Метод для переключения на следующего игрока.
- #update_players! ⇒ Object
-
#who_goes_first ⇒ Object
Метод для выбора игрока, который будет ходить первым.
- #won?(player = @current_player) ⇒ Boolean
Methods inherited from Engine
difficulty_level, #draws, #game_mode, game_mode, is_singleplayer?, #losses, reset_counters, set_difficulty, set_game_mode, #wins
Constructor Details
#initialize(difficulty = MediumAI) ⇒ Game
Returns a new instance of Game.
24 25 26 27 28 29 30 31 |
# 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 |
Instance Attribute Details
#board ⇒ Object (readonly)
Returns the value of attribute board.
7 8 9 |
# File 'lib/ttt-cli/game.rb', line 7 def board @board end |
#current_player ⇒ Object
Returns the value of attribute current_player.
5 6 7 |
# File 'lib/ttt-cli/game.rb', line 5 def current_player @current_player end |
#first_player ⇒ Object
Returns the value of attribute first_player.
5 6 7 |
# File 'lib/ttt-cli/game.rb', line 5 def first_player @first_player end |
#judge ⇒ Object
Returns the value of attribute judge.
5 6 7 |
# File 'lib/ttt-cli/game.rb', line 5 def judge @judge end |
#second_player ⇒ Object
Returns the value of attribute second_player.
5 6 7 |
# File 'lib/ttt-cli/game.rb', line 5 def second_player @second_player end |
Class Method Details
.setup_game(game) ⇒ Object
16 17 18 19 20 21 22 |
# 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 ⇒ Object
9 10 11 12 13 14 |
# File 'lib/ttt-cli/game.rb', line 9 def self.start CommandLine::Display. Game.set_game_mode(Game.game_mode) new_game = new(Game.set_difficulty) Game.setup_game(new_game) end |
Instance Method Details
#draw? ⇒ Boolean
103 104 105 |
# File 'lib/ttt-cli/game.rb', line 103 def draw? @board.full? && !won? end |
#get_user_token ⇒ Object
63 64 65 66 67 |
# File 'lib/ttt-cli/game.rb', line 63 def get_user_token if @@game_mode == :singleplayer CommandLine::Display.user_token end end |
#hotseat_mode ⇒ Object
51 52 53 54 55 |
# 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 ⇒ Object
128 129 130 131 132 133 134 135 136 |
# 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 ⇒ Object
57 58 59 60 61 |
# 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? ⇒ Boolean
99 100 101 |
# File 'lib/ttt-cli/game.rb', line 99 def over? draw? || won? end |
#over_message ⇒ Object
111 112 113 114 115 |
# File 'lib/ttt-cli/game.rb', line 111 def increase_counter CommandLine::Display.print_board(@board) print_winner end |
#print_winner ⇒ Object
Метод для объявления победителя
118 119 120 121 122 123 124 125 126 |
# File 'lib/ttt-cli/game.rb', line 118 def print_winner if draw? CommandLine::Display.draw elsif won?(@first_player) CommandLine::Display.winner(self) elsif won?(@second_player) CommandLine::Display.loser(self) end end |
#set_players_tokens(token) ⇒ Object
Метод который устанавливает символы игрокам По умолчанию игрок - X, компьютер - O
71 72 73 74 75 76 77 78 |
# 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 ⇒ Object
47 48 49 |
# File 'lib/ttt-cli/game.rb', line 47 def singleplayer_mode @second_player = Players::Computer.new(token: O, game: self) end |
#start_new_game? ⇒ Boolean
33 34 35 |
# File 'lib/ttt-cli/game.rb', line 33 def start_new_game? CommandLine::Display.play_again end |
#switch_player ⇒ Object
Метод для переключения на следующего игрока
90 91 92 93 94 95 96 97 |
# 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! ⇒ Object
37 38 39 40 41 42 43 44 45 |
# 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 ⇒ Object
Метод для выбора игрока, который будет ходить первым. По умолчанию первым ходит игрок.
82 83 84 85 86 87 |
# 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) ⇒ Boolean
107 108 109 |
# File 'lib/ttt-cli/game.rb', line 107 def won?(player = @current_player) @judge.is_combo?(player) end |