Class: TictactoeJ8th::Game
- Inherits:
-
Object
- Object
- TictactoeJ8th::Game
- Defined in:
- lib/tictactoe_j8th/game.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
Instance Method Summary collapse
- #game_over? ⇒ Boolean
-
#initialize(board, player1, player2) ⇒ Game
constructor
A new instance of Game.
- #play ⇒ Object
- #player_up ⇒ Object
- #turn(spot = nil) ⇒ Object
- #winner ⇒ Object
Constructor Details
#initialize(board, player1, player2) ⇒ Game
Returns a new instance of Game.
6 7 8 9 10 11 12 |
# File 'lib/tictactoe_j8th/game.rb', line 6 def initialize(board, player1, player2) @board = board @player1 = player1 @player2 = player2 @playerup = @player1 end |
Instance Attribute Details
#board ⇒ Object (readonly)
Returns the value of attribute board.
4 5 6 |
# File 'lib/tictactoe_j8th/game.rb', line 4 def board @board end |
Instance Method Details
#game_over? ⇒ Boolean
14 15 16 17 |
# File 'lib/tictactoe_j8th/game.rb', line 14 def game_over? return true if @board.full? or not winner.nil? false end |
#play ⇒ Object
37 38 39 40 |
# File 'lib/tictactoe_j8th/game.rb', line 37 def play turn play unless game_over? end |
#player_up ⇒ Object
42 43 44 |
# File 'lib/tictactoe_j8th/game.rb', line 42 def player_up end |
#turn(spot = nil) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/tictactoe_j8th/game.rb', line 27 def turn(spot = nil) if spot.nil? spot = @playerup.move(@board) else @board.place(@playerup.token, spot) end @playerup = @playerup == @player1 ? @player2 : @player1 spot end |
#winner ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/tictactoe_j8th/game.rb', line 19 def winner @board.lines.each do |line| return @player1.token if line.values.all? { |v| v == @player1.token } return @player2.token if line.values.all? { |v| v == @player2.token } end nil end |