Class: TictactoeJ8th::Board

Inherits:
Object
  • Object
show all
Defined in:
lib/tictactoe_j8th/board.rb

Constant Summary collapse

BOARD_SIZE =
9
BOARD_LINES =
[
  [0, 1, 2],
  [3, 4, 5],
  [6, 7, 8],

  [0, 3, 6],
  [1, 4, 7],
  [2, 5, 8],

  [0, 4, 8],
  [2, 4, 6]
]

Instance Method Summary collapse

Constructor Details

#initializeBoard

Returns a new instance of Board.



17
18
19
# File 'lib/tictactoe_j8th/board.rb', line 17

def initialize
  @board = Array.new(BOARD_SIZE)
end

Instance Method Details

#[](spot) ⇒ Object



34
35
36
# File 'lib/tictactoe_j8th/board.rb', line 34

def [](spot)
  board[spot]
end

#create_copyObject



54
55
56
57
58
# File 'lib/tictactoe_j8th/board.rb', line 54

def create_copy
  copy = Board.new
  copy.set_board(@board.dup)
  copy
end

#empty?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/tictactoe_j8th/board.rb', line 21

def empty?
  board.all? { |value| value.nil? }
end

#full?Boolean

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/tictactoe_j8th/board.rb', line 29

def full?
  return false if board.include? nil
  true
end

#linesObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tictactoe_j8th/board.rb', line 38

def lines
  array = []
  BOARD_LINES.each do |line|
    hash = {}
    line.each do |i|
      hash[i] = board[i]
    end
    array.push(hash)
  end
  array
end

#place(item, position) ⇒ Object



25
26
27
# File 'lib/tictactoe_j8th/board.rb', line 25

def place(item, position)
  board[position] = item if board[position].nil?
end

#set_board(array) ⇒ Object



50
51
52
# File 'lib/tictactoe_j8th/board.rb', line 50

def set_board(array)
  @board = array
end