class TictactoeTracypholmes::Game

Constants

WIN_COMBINATIONS

Attributes

board[RW]
player_1[RW]
player_2[RW]

Public Class Methods

new(player_1 = Players::Human.new('X'), player_2 = Players::Human.new('O'), board = Board.new) click to toggle source

need to add name somewhere in the initialization?

# File lib/tictactoe_tracypholmes/game.rb, line 26
def initialize(player_1 = Players::Human.new('X'), player_2 = Players::Human.new('O'), board = Board.new)
  @player_1 = player_1
  @player_2 = player_2
  @board = board
end

Public Instance Methods

current_player() click to toggle source

define current_player here

# File lib/tictactoe_tracypholmes/game.rb, line 33
def current_player
  @board.turn_count.even? ? player_1 : player_2
end
draw?() click to toggle source

define draw? here

# File lib/tictactoe_tracypholmes/game.rb, line 55
def draw?
  !won? && board.full?
end
over?() click to toggle source

over? here - won, is a draw, or full

# File lib/tictactoe_tracypholmes/game.rb, line 38
def over?
  won? || draw?
end
play() click to toggle source

define play here

# File lib/tictactoe_tracypholmes/game.rb, line 77
def play
  until over? # until the game is over
    @board.display
    turn # take turns
  end
  if won?
    @board.display
    puts "Congratulations #{winner}!".green
  elsif draw?
    @board.display
    puts "Cat's Game!".yellow
  end
end
turn() click to toggle source

define turn method here

# File lib/tictactoe_tracypholmes/game.rb, line 67
def turn # work on this dadgum method
  move = current_player.move(@board)
  unless board.valid_move?(move)
    puts 'NOT a valid move. Play again, please!'.red
    turn
  end
  @board.update(move, current_player)
end
winner() click to toggle source

define winner here

# File lib/tictactoe_tracypholmes/game.rb, line 60
def winner
  if win_combo = won?
    @board.cells[win_combo.first]
  end
end
won?() click to toggle source
# File lib/tictactoe_tracypholmes/game.rb, line 42
def won?
  WIN_COMBINATIONS.detect do |win_combo|
    if @board.cells[win_combo[0]] == @board.cells[win_combo[1]] &&
       @board.cells[win_combo[1]] == @board.cells[win_combo[2]] &&
      (@board.cells[win_combo[0]] == 'X' || @board.cells[win_combo[0]] == 'O')
      return win_combo
    else
      false
    end
  end
end