Class: Grid

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Color

#line, #o_marker, #pipe, #x_marker

Constructor Details

#initializeGrid

Returns a new instance of Grid.



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

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

Instance Attribute Details

#gridObject (readonly) Also known as: spots

Returns the value of attribute grid.



8
9
10
# File 'lib/capim_tictactoe/grid.rb', line 8

def grid
  @grid
end

Instance Method Details

#columns(spots) ⇒ Object



30
31
32
# File 'lib/capim_tictactoe/grid.rb', line 30

def columns(spots)
  rows(spots).to_a.transpose
end

#diagonals(spots = grid) ⇒ Object



34
35
36
# File 'lib/capim_tictactoe/grid.rb', line 34

def diagonals(spots = grid)
  [[spots[0], spots[4], spots[8]], [spots[2], spots[4], spots[6]]]
end

#displayObject



15
16
17
18
19
20
# File 'lib/capim_tictactoe/grid.rb', line 15

def display
  rows.each do |row|
    print " #{row.join(pipe)} "
    print line
  end
end

#get_at(index) ⇒ Object



22
23
24
# File 'lib/capim_tictactoe/grid.rb', line 22

def get_at(index)
  @grid[index]
end

#marked?(spot) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/capim_tictactoe/grid.rb', line 42

def marked?(spot)
  [x_marker, o_marker].include?(@grid[spot])
end

#rows(spots = grid) ⇒ Object



26
27
28
# File 'lib/capim_tictactoe/grid.rb', line 26

def rows(spots = grid)
  spots.each_slice(3)
end

#spot_marker(spot, marker) ⇒ Object



38
39
40
# File 'lib/capim_tictactoe/grid.rb', line 38

def spot_marker(spot, marker)
  @grid[spot] = marker
end