Class: Board

Inherits:
Object
  • Object
show all
Defined in:
lib/tic-tac-toe/board.rb

Instance Method Summary collapse

Constructor Details

#initializeBoard

Board Positions: 0 | 1 | 2


3 | 4 | 5


6 | 7 | 8



9
10
11
# File 'lib/tic-tac-toe/board.rb', line 9

def initialize
  @board = Array.new()
end

Instance Method Details

#game_over?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/tic-tac-toe/board.rb', line 43

def game_over?
  (0..8).inject(true) { |val, i| val && @board[i] } || get_winner
end

#get_winnerObject



34
35
36
37
38
39
40
41
# File 'lib/tic-tac-toe/board.rb', line 34

def get_winner
  return @board[0] if(@board[0] == @board[1] && @board[1] == @board[2]) # Top Row
  return @board[3] if(@board[3] == @board[4] && @board[4] == @board[5]) # Middle Row
  return @board[6] if(@board[6] == @board[7] && @board[7] == @board[8]) # Bottom Row
  return @board[0] if(@board[0] == @board[4] && @board[4] == @board[8]) # Diagonal from Top Right
  return @board[2] if(@board[2] == @board[4] && @board[4] == @board[6]) # Diagonal from Top Left
  return nil                                                             # No one has won
end

#place_marker(marker, location) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tic-tac-toe/board.rb', line 22

def place_marker(marker, location)
  if location < 9 && location >= 0
    unless @board[location].nil?
      raise 'Location already taken'
    else
      @board[location] = marker
    end
  else
    raise 'Location does not exist'
  end
end

#to_sObject



13
14
15
16
17
18
19
20
# File 'lib/tic-tac-toe/board.rb', line 13

def to_s
  str = "-----------\n"
  (1..3).to_a.each do |i|
    str << " #{@board[(i*3)-3] || (i*3)-2} | #{@board[(i*3)-2] || (i*3)-1} | #{@board[(i*3)-1] || i*3} \n"
    str << "-----------\n"
  end
  str
end