Class: Board

Inherits:
Object
  • Object
show all
Includes:
Color
Defined in:
lib/capim_tictactoe/board.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Color

#line, #o_marker, #pipe, #x_marker

Constructor Details

#initializeBoard

Returns a new instance of Board.



11
12
13
14
# File 'lib/capim_tictactoe/board.rb', line 11

def initialize
  @spots = %w[0 1 2 3 4 5 6 7 8]
  @grid = Grid.new
end

Instance Attribute Details

#gridObject (readonly)

Returns the value of attribute grid.



9
10
11
# File 'lib/capim_tictactoe/board.rb', line 9

def grid
  @grid
end

#spotsObject (readonly)

Returns the value of attribute spots.



9
10
11
# File 'lib/capim_tictactoe/board.rb', line 9

def spots
  @spots
end

Instance Method Details

#available_spotsObject



31
32
33
# File 'lib/capim_tictactoe/board.rb', line 31

def available_spots
  @grid.spots.select { |spot| %(0 1 2 3 4 5 6 7 8).include?(spot) }
end

#center_marked?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/capim_tictactoe/board.rb', line 23

def center_marked?
  @grid.get_at(4) != '4'
end

#game_over?(spots = @grid.spots) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/capim_tictactoe/board.rb', line 35

def game_over?(spots = @grid.spots)
  valid_line?(@grid.rows(spots)) || valid_line?(@grid.columns(spots)) || valid_line?(@grid.diagonals(spots))
end

#mark_center(marker) ⇒ Object



27
28
29
# File 'lib/capim_tictactoe/board.rb', line 27

def mark_center(marker)
  @grid.spot_marker(4, marker)
end

#tie?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/capim_tictactoe/board.rb', line 39

def tie?
  @grid.spots.all? { |spot| [x_marker, o_marker].include?(spot) }
end

#valid_line?(set) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
# File 'lib/capim_tictactoe/board.rb', line 16

def valid_line?(set)
  set.each do |line|
    return true if line.uniq.length == 1
  end
  false
end