class RubyTictactoe::Board

Attributes

all_cells[RW]
num_of_rows[RW]
winning_lines[RW]

Public Class Methods

new(num_of_rows) click to toggle source
# File lib/board.rb, line 8
def initialize(num_of_rows)
  @num_of_rows = num_of_rows
  @all_cells = create_board_hash
  @winning_lines = get_winning_lines
end

Public Instance Methods

add_test_marker(marker, cell) click to toggle source
# File lib/board.rb, line 51
def add_test_marker(marker, cell)
  all_cells[cell] = marker
end
all_rows() click to toggle source
# File lib/board.rb, line 38
def all_rows
  rows = []
  cellIDs = all_cells.keys
  beg = 0
  ending  = num_of_rows - 1
  until rows.length == num_of_rows
    rows << cellIDs[beg..ending]
    beg += num_of_rows
    ending += num_of_rows
  end
  rows
end
available_cell?(cell) click to toggle source
# File lib/board.rb, line 55
def available_cell?(cell)
  valid_cell?(cell) && all_cells[cell].nil?
end
create_board_hash() click to toggle source
# File lib/board.rb, line 14
def create_board_hash
  new_board = Hash.new
  alpha = 'A'
  numeric = 1
  num_of_rows.times do
    num_of_rows.times do
      cellID = numeric.to_s + alpha
      numeric += 1
      new_board[cellID] = nil
    end
    alpha = alpha.next
    numeric = 1
  end
  new_board
end
empty?() click to toggle source
# File lib/board.rb, line 87
def empty?
  open_cells.length == (num_of_rows * num_of_rows)
end
game_over?() click to toggle source
# File lib/board.rb, line 79
def game_over?
  !moves_remaining? || winner?(RubyTictactoe::TictactoeConstants::MARKER_X) || winner?(RubyTictactoe::TictactoeConstants::MARKER_O)
end
get_winning_lines() click to toggle source
# File lib/board.rb, line 30
def get_winning_lines
  lines = []
  all_rows.each { |row| lines << row }
  all_cols.each { |col| lines << col }
  diagonals.each { |diagonal| lines << diagonal }
  lines
end
moves_remaining?() click to toggle source
# File lib/board.rb, line 67
def moves_remaining?
  all_cells.has_value?(nil)
end
open_cells() click to toggle source
# File lib/board.rb, line 83
def open_cells
  all_cells.select { |k,v| v.nil? }
end
random_cell() click to toggle source
# File lib/board.rb, line 91
def random_cell
  cells = open_cells.keys
  cells_count = cells.length - 1
  cells[rand(cells_count)]
end
remove_marker(cell) click to toggle source
# File lib/board.rb, line 63
def remove_marker(cell)
  all_cells[cell] = nil
end
valid_cell?(cell) click to toggle source
# File lib/board.rb, line 59
def valid_cell?(cell)
  all_cells.has_key?(cell)
end
winner?(marker) click to toggle source
# File lib/board.rb, line 71
def winner?(marker)
  board_markers = all_cells.select { |cell, value| value == marker }.keys
  winning_lines.each do |line|
    return true if (line & board_markers).length == num_of_rows
  end
  false
end

Private Instance Methods

all_cols() click to toggle source
# File lib/board.rb, line 98
def all_cols
  cols = []
  index = 0
  num_of_rows.times do
    cols << get_column(index)
    index += 1
  end
  cols
end
diagonal_one() click to toggle source
# File lib/board.rb, line 124
def diagonal_one
  diagonal = []
  alpha = 'A'
  numeric = 1
  num_of_rows.times do
    diagonal << numeric.to_s + alpha
    alpha = alpha.next
    numeric += 1
  end
  diagonal
end
diagonal_two() click to toggle source
# File lib/board.rb, line 136
def diagonal_two
  diagonal = []
  alpha = 'A'
  numeric = num_of_rows
  num_of_rows.times do
    diagonal << numeric.to_s + alpha
    alpha = alpha.next
    numeric -= 1
  end
  diagonal
end
diagonals() click to toggle source
# File lib/board.rb, line 118
def diagonals
  diagonals = []
  diagonals << diagonal_one
  diagonals << diagonal_two
end
get_column(index) click to toggle source
# File lib/board.rb, line 108
def get_column(index)
  column = []
  cellIDs = all_cells.keys
  num_of_rows.times do
    column << cellIDs[index]
    index += num_of_rows
  end
  column
end