class MazeCrosser::Maze

Class responsible for representing a maze.

maze = Maze.new(grid)

Check if the coordinates are inside the grid maze.inside_grid? [4, 2]

Check if a cell is blocked maze.blocked_cell? [4, 2]

Attributes

goal[R]
grid[R]
number_of_columns[R]
number_of_rows[R]
start[R]

Public Class Methods

new(grid) click to toggle source
# File lib/maze_crosser/maze.rb, line 22
def initialize(grid)
  raise ArgumentError, 'Invalid maze' unless valid? grid

  @grid = grid
  set_dimensions
  set_start
  set_goal
end

Public Instance Methods

blocked_cell?(coordinates) click to toggle source
# File lib/maze_crosser/maze.rb, line 36
def blocked_cell?(coordinates)
  @grid[coordinates[0]][coordinates[1]] == ALLOWED_CHARACTERS[:blocked]
end
inside_grid?(coordinates) click to toggle source
# File lib/maze_crosser/maze.rb, line 31
def inside_grid?(coordinates)
  coordinates[0].between?(0, @number_of_rows - 1) && \
    coordinates[1].between?(0, @number_of_columns - 1)
end

Private Instance Methods

set_dimensions() click to toggle source
# File lib/maze_crosser/maze.rb, line 42
def set_dimensions
  @number_of_rows = @grid.size
  @number_of_columns = @grid[0].size
end
set_goal() click to toggle source
# File lib/maze_crosser/maze.rb, line 54
def set_goal
  @goal = [
    r = @grid.index { |row| row.include? ALLOWED_CHARACTERS[:goal] },
    @grid[r].index(ALLOWED_CHARACTERS[:goal])
  ]
end
set_start() click to toggle source
# File lib/maze_crosser/maze.rb, line 47
def set_start
  @start = [
    r = @grid.index { |row| row.include? ALLOWED_CHARACTERS[:start] },
    @grid[r].index(ALLOWED_CHARACTERS[:start])
  ]
end