Class: TicTacToe::Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



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

def initialize
	setup
	View.render(self)			
end

Instance Attribute Details

#boardObject

Returns the value of attribute board.



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

def board
  @board
end

#player_1Object

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_2Object

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

#winnerObject

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

#playObject



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.message
			retry
		end
	end
	self
end

#resetObject



26
27
28
29
30
# File 'lib/tic_tac_toe/game.rb', line 26

def reset
	@board = Board.new(size: @board.board_array.size)
	@winner = nil
	self
end