class TicTacToe::Board

Attributes

lines[R]
squares[R]
winner[R]

Public Class Methods

new(saved=nil) click to toggle source
# File lib/tic_tac_toe_mchliakh/board/board.rb, line 5
def initialize(saved=nil)
  setup do
    if saved
      saved.map.with_index do |s, n|
        squares << Square.new(self, n + 1, s)
      end
    else
      9.times {|n| squares << Square.new(self, n + 1) }
    end
  end
end

Public Instance Methods

game_over?() click to toggle source
# File lib/tic_tac_toe_mchliakh/board/board.rb, line 25
def game_over?
  !!@game_over
end
serialize() click to toggle source
# File lib/tic_tac_toe_mchliakh/board/board.rb, line 38
def serialize
  squares.map(&:serialize)
end
square(number) click to toggle source
# File lib/tic_tac_toe_mchliakh/board/board.rb, line 17
def square(number)
  unless 1.upto(9).include?(number)
    raise RangeError, 'Choose a number between 1 and 9'
  end

  squares[number - 1]
end
square_taken() click to toggle source
# File lib/tic_tac_toe_mchliakh/board/board.rb, line 29
def square_taken
  game_over if squares.all_taken?
end
three_in_a_row(player) click to toggle source
# File lib/tic_tac_toe_mchliakh/board/board.rb, line 33
def three_in_a_row(player)
  @winner = player
  game_over
end

Private Instance Methods

build_lines() click to toggle source
# File lib/tic_tac_toe_mchliakh/board/board.rb, line 50
def build_lines
  @lines = Lines.new

  lines_of_three.each do |lot|
    lines << Line.new(self,
      Squares.new(lot.map {|s| squares[s] })
    )
  end
end
game_over() click to toggle source
# File lib/tic_tac_toe_mchliakh/board/board.rb, line 73
def game_over
  @game_over = true
end
lines_of_three() click to toggle source
# File lib/tic_tac_toe_mchliakh/board/board.rb, line 60
def lines_of_three
  [
    [0, 1, 2],
    [2, 5, 8],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [3, 4, 5],
    [0, 4, 8],
    [2, 4, 6]
  ]
end
setup() { || ... } click to toggle source
# File lib/tic_tac_toe_mchliakh/board/board.rb, line 44
def setup
  @squares = Squares.new
  yield
  build_lines
end