class TictactoeJ8th::Game

Attributes

board[R]

Public Class Methods

new(board, player1, player2) click to toggle source
# File lib/tictactoe_j8th/game.rb, line 6
def initialize(board, player1, player2)
  @board = board
  @player1 = player1
  @player2 = player2

  @playerup = @player1
end

Public Instance Methods

game_over?() click to toggle source
# File lib/tictactoe_j8th/game.rb, line 14
def game_over?
  return true if @board.full? or not winner.nil?
  false
end
play() click to toggle source
# File lib/tictactoe_j8th/game.rb, line 37
def play
  turn
  play unless game_over?
end
player_up() click to toggle source
# File lib/tictactoe_j8th/game.rb, line 42
def player_up
  
end
turn(spot = nil) click to toggle source
# 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() click to toggle source
# 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