class Board

Attributes

grid[RW]

Public Class Methods

new() click to toggle source
# File lib/board.rb, line 4
def initialize
  @grid = [[nil, nil, nil],
           [nil, nil, nil],
           [nil, nil, nil]]
end

Public Instance Methods

add_piece(player_token, location) click to toggle source

adds a piece to the grid indexes start at 0 from the top left corner of the grid

# File lib/board.rb, line 37
def add_piece(player_token, location)
  if validate_move(location)
    @grid[location.first][location.last] = player_token  
    return true
  else
    return false
  end
end
available_spaces() click to toggle source

checks for empty(nil) spaces on the grid

# File lib/board.rb, line 19
def available_spaces
  spaces = []
  @grid.each_with_index do |column, col_index|
    column.each_with_index do |space, row_index|
      spaces << [col_index, row_index] if space == nil
    end
  end
  spaces
end
calculate_win() click to toggle source

determines if there is a winning player and returns that player token

# File lib/board.rb, line 47
def calculate_win
  return diagonal_win if diagonal_win
  return horizontal_win if horizontal_win
  return vertical_win if vertical_win
  return 'nobody' if cat_game?
end
clone() click to toggle source
# File lib/board.rb, line 10
def clone
  board = Board.new
  board.grid = [[@grid[0][0], @grid[0][1], @grid[0][2]],
                [@grid[1][0], @grid[1][1], @grid[1][2]],
                [@grid[2][0], @grid[2][1], @grid[2][2]]]
  return board
end
print() click to toggle source
validate_move(location) click to toggle source
# File lib/board.rb, line 29
def validate_move(location)
  if location.first && location.last
    return @grid[location.first][location.last].nil?
  end
end

Private Instance Methods

cat_game?() click to toggle source
# File lib/board.rb, line 122
def cat_game?
  @grid.each do |row|
    row.each do |space|
      return false if not space
    end
  end
  return true
end
diagonal_win() click to toggle source
# File lib/board.rb, line 76
def diagonal_win
  # top left to bottom right
  if @grid[0][0] == @grid[1][1] && @grid[1][1] == @grid[2][2] && @grid[0][0]
    return @grid[0][0]
  end
  
  # bottom left to top right
  if @grid[0][2] == @grid[1][1] && @grid[1][1] == @grid[2][0] && @grid[0][2]
    return @grid[0][2]
  end
end
horizontal_win() click to toggle source
# File lib/board.rb, line 88
def horizontal_win
  # top row
  if @grid[0][0] == @grid[0][1] && @grid[0][1] == @grid[0][2] && @grid[0][0]
    return @grid[0][0]
  end
  
  # middle row
  if @grid[1][0] == @grid[1][1] && @grid[1][1] == @grid[1][2] && @grid[1][0]
    return @grid[1][0]
  end
  
  # bottom row
  if @grid[2][0] == @grid[2][1] && @grid[2][1] == @grid[2][2] && @grid[2][0]
    return @grid[2][0]
  end
end
vertical_win() click to toggle source
# File lib/board.rb, line 105
def vertical_win
  # top row
  if @grid[0][0] == @grid[1][0] && @grid[1][0] == @grid[2][0] && @grid[0][0]
    return @grid[0][0]
  end
  
  # middle row
  if @grid[0][1] == @grid[1][1] && @grid[1][1] == @grid[2][1] && @grid[0][1]
    return @grid[0][1]
  end

  # bottom row
  if @grid[0][2] == @grid[1][2] && @grid[1][2] == @grid[2][2] && @grid[0][2]
    return @grid[0][2]
  end
end