Class: Amaze::Grid

Inherits:
Object
  • Object
show all
Defined in:
lib/amaze/grid.rb

Direct Known Subclasses

Delta, Ortho, Polar, Sigma, Upsilon

Defined Under Namespace

Classes: Delta, Ortho, Polar, Sigma, Upsilon

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rows, columns) ⇒ Grid

Returns a new instance of Grid.



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

def initialize rows, columns
  @rows = rows
  @columns = columns
  
  prepare_grid
  configure_cell
end

Instance Attribute Details

#columnsObject (readonly)

The dimension of the amaze



10
11
12
# File 'lib/amaze/grid.rb', line 10

def columns
  @columns
end

#gridObject (readonly)

The grid



13
14
15
# File 'lib/amaze/grid.rb', line 13

def grid
  @grid
end

#rowsObject (readonly)

The dimension of the amaze



10
11
12
# File 'lib/amaze/grid.rb', line 10

def rows
  @rows
end

Instance Method Details

#[](row, column) ⇒ Object



31
32
33
34
35
# File 'lib/amaze/grid.rb', line 31

def [](row, column)
  return nil unless row.between?(0, rows-1)
  return nil unless column.between?(0, columns-1)
  grid[row][column]
end

#configure_cellObject

Raises:

  • (NotImplementedError)


27
28
29
# File 'lib/amaze/grid.rb', line 27

def configure_cell
  raise NotImplementedError
end

#deadendsObject



55
56
57
58
59
60
61
# File 'lib/amaze/grid.rb', line 55

def deadends
  list = []
  each_cell do |cell|
    list << cell if cell.links.size == 1
  end
  list
end

#each_cellObject



43
44
45
46
47
48
49
# File 'lib/amaze/grid.rb', line 43

def each_cell
  each_row do |row|
    row.each do |cell|
      yield cell if cell
    end
  end
end

#each_rowObject



37
38
39
40
41
# File 'lib/amaze/grid.rb', line 37

def each_row
  grid.each do |row|
    yield row
  end
end

#prepare_gridObject

Raises:

  • (NotImplementedError)


23
24
25
# File 'lib/amaze/grid.rb', line 23

def prepare_grid
  raise NotImplementedError
end

#random_cellObject



51
52
53
# File 'lib/amaze/grid.rb', line 51

def random_cell
  self[rand(rows), rand(columns)]
end

#sizeObject



63
64
65
# File 'lib/amaze/grid.rb', line 63

def size
  rows * columns
end