Class: TicTacToe::Game
- Inherits:
-
Object
- Object
- TicTacToe::Game
- Defined in:
- lib/tic_tac_toe/game.rb
Overview
The main director of the program Directs its gametype when to retrieve information from the user
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#player_moves ⇒ Object
readonly
Returns the value of attribute player_moves.
Instance Method Summary collapse
- #cats? ⇒ Boolean
- #decorated_grid ⇒ Object
- #grid ⇒ Object
-
#initialize(board = nil, player_1 = nil, player_2 = nil) ⇒ Game
constructor
A new instance of Game.
- #solved? ⇒ Boolean
- #start ⇒ Object
- #winner ⇒ Object
Constructor Details
#initialize(board = nil, player_1 = nil, player_2 = nil) ⇒ Game
Returns a new instance of Game.
21 22 23 24 25 26 27 |
# File 'lib/tic_tac_toe/game.rb', line 21 def initialize(board=nil, player_1=nil, player_2=nil) @board = Board.new @board.grid = board || [[nil,nil,nil],[nil,nil,nil],[nil,nil,nil]] @player_1 = player_1 @player_2 = player_2 @current_player = @player_1 && @player_1.has_next_move? ? @player_1 : @player_2 end |
Instance Attribute Details
#board ⇒ Object (readonly)
Returns the value of attribute board.
17 18 19 |
# File 'lib/tic_tac_toe/game.rb', line 17 def board @board end |
#player_moves ⇒ Object (readonly)
Returns the value of attribute player_moves.
19 20 21 |
# File 'lib/tic_tac_toe/game.rb', line 19 def player_moves @player_moves end |
Instance Method Details
#cats? ⇒ Boolean
56 57 58 |
# File 'lib/tic_tac_toe/game.rb', line 56 def cats? @board.full? && !solved? end |
#decorated_grid ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/tic_tac_toe/game.rb', line 44 def decorated_grid grid.each_with_index.map do |row, i| row.each_with_index.map do |cell, j| cell || ((i*3)+(j+1)).to_s end end end |
#grid ⇒ Object
40 41 42 |
# File 'lib/tic_tac_toe/game.rb', line 40 def grid @board.grid end |
#solved? ⇒ Boolean
52 53 54 |
# File 'lib/tic_tac_toe/game.rb', line 52 def solved? @board.solved? end |
#start ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/tic_tac_toe/game.rb', line 29 def start while @current_player && (move = @current_player.get_move(@board)) move = TicTacToe::number_to_cords(move, @board.size) unless move.is_a?(Array) @board.play_at(move[0], move[1], @current_player.letter) break if over? switch_player end end |
#winner ⇒ Object
60 61 62 |
# File 'lib/tic_tac_toe/game.rb', line 60 def winner @board.winner end |