class GameRules

Attributes

current_player[RW]

Public Instance Methods

full?(board) click to toggle source
# File lib/game_rules.rb, line 7
def full?(board)
  board.count(nil) == 0
end
game_over?(board) click to toggle source
# File lib/game_rules.rb, line 15
def game_over?(board)
  full?(board) || winner(board)
end
tie?(board) click to toggle source
# File lib/game_rules.rb, line 11
def tie?(board)
  full?(board) == true && winner(board) == false
end
valid?(board, move) click to toggle source
# File lib/game_rules.rb, line 3
def valid?(board, move)
  board[move.to_i] == nil && move.to_i < 9
end
winner(board) click to toggle source
# File lib/game_rules.rb, line 19
 def winner(board)
  [first_row(board),
   second_row(board),
   third_row(board),
   left_column(board),
   middle_column(board),
   right_column(board),
   left_diag_winner(board),
   right_diag_winner(board)
  ].each do |set|
    if set.uniq.count == 1 
      if set[0] != nil
        return set[0]
      end
    end
  end
  false
end

Private Instance Methods

first_row(board) click to toggle source
# File lib/game_rules.rb, line 48
def first_row(board)
  board[0..2]
end
left_column(board) click to toggle source
# File lib/game_rules.rb, line 60
def left_column(board)
  [board[0], board[3], board[6]]
end
left_diag_winner(board) click to toggle source
# File lib/game_rules.rb, line 72
def left_diag_winner(board)
  [board[0], board[4], board[8]]
end
middle_column(board) click to toggle source
# File lib/game_rules.rb, line 64
def middle_column(board)
  [board[1], board[4], board[7]]
end
right_column(board) click to toggle source
# File lib/game_rules.rb, line 68
def right_column(board)
  [board[2], board[5], board[8]]
end
right_diag_winner(board) click to toggle source
# File lib/game_rules.rb, line 76
def right_diag_winner(board)
  [board[2], board[4], board[6]]
end
second_row(board) click to toggle source
# File lib/game_rules.rb, line 52
def second_row(board)
  board[3..5]
end
third_row(board) click to toggle source
# File lib/game_rules.rb, line 56
def third_row(board)
  board[6..8]
end