class TicTacToe::Board

Attributes

board[R]

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/tic_tac_toe/board.rb, line 6
def initialize
  @board = empty_board
  super()
end
valid_position_string?(selection) click to toggle source
# File lib/tic_tac_toe/board.rb, line 25
def self.valid_position_string?(selection)
  selection.downcase.gsub(" ","").match(/^[abc],[123]$/)
end

Public Instance Methods

fill_board_space(position, player) click to toggle source
# File lib/tic_tac_toe/board.rb, line 11
def fill_board_space(position, player)
  cell = cell_from_position(position)
  cell.owner = player
end
get_empty_positions() click to toggle source
# File lib/tic_tac_toe/board.rb, line 21
def get_empty_positions
  @board.each.reduce([]) { |positions, row| positions + row.select(&:empty?) }
end
position_empty?(position) click to toggle source
# File lib/tic_tac_toe/board.rb, line 16
def position_empty?(position)
  cell = cell_from_position(position)
  cell.empty?
end

Private Instance Methods

cell_from_position(position) click to toggle source
# File lib/tic_tac_toe/board.rb, line 30
def cell_from_position(position)
  @board.flatten.find { |cell| cell.position == position.downcase }
end
empty_board() click to toggle source
# File lib/tic_tac_toe/board.rb, line 34
def empty_board
  [1,2,3].each.reduce([]) do |board, number| 
    board << ['a','b','c'].each.reduce([]) do |row, letter|
      row << Cell.new("#{letter},#{number}")
    end
  end
end