Class: TicTacToe::Game
- Inherits:
-
Object
- Object
- TicTacToe::Game
- Defined in:
- lib/tic_tac_toe/game.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
Returns the value of attribute board.
-
#player_1 ⇒ Object
Returns the value of attribute player_1.
-
#player_2 ⇒ Object
Returns the value of attribute player_2.
-
#winner ⇒ Object
Returns the value of attribute winner.
Instance Method Summary collapse
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #play ⇒ Object
- #reset ⇒ Object
Constructor Details
Instance Attribute Details
#board ⇒ Object
Returns the value of attribute board.
4 5 6 |
# File 'lib/tic_tac_toe/game.rb', line 4 def board @board end |
#player_1 ⇒ Object
Returns the value of attribute player_1.
4 5 6 |
# File 'lib/tic_tac_toe/game.rb', line 4 def player_1 @player_1 end |
#player_2 ⇒ Object
Returns the value of attribute player_2.
4 5 6 |
# File 'lib/tic_tac_toe/game.rb', line 4 def player_2 @player_2 end |
#winner ⇒ Object
Returns the value of attribute winner.
4 5 6 |
# File 'lib/tic_tac_toe/game.rb', line 4 def winner @winner end |
Instance Method Details
#play ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/tic_tac_toe/game.rb', line 10 def play current_player = [@player_1, @player_2].sample until @winner || @board.draw? do begin @board.place_symbol(symbol: current_player.symbol, **current_player.get_move(@board)) @winner = @board.win? ? current_player : nil View.render(self) current_player = current_player == @player_1 ? @player_2 : @player_1 rescue InvalidInputException => e puts e. retry end end self end |