Class: MazeCrosser::Maze

Inherits:
Object
  • Object
show all
Includes:
BasicGridValidator
Defined in:
lib/maze_crosser/maze.rb

Overview

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]

Constant Summary

Constants included from BasicGridValidator

BasicGridValidator::ALLOWED_CHARACTERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BasicGridValidator

#valid?

Constructor Details

#initialize(grid) ⇒ Maze

Returns a new instance of Maze.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
# 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

Instance Attribute Details

#goalObject (readonly)

Returns the value of attribute goal.



16
17
18
# File 'lib/maze_crosser/maze.rb', line 16

def goal
  @goal
end

#gridObject (readonly)

Returns the value of attribute grid.



16
17
18
# File 'lib/maze_crosser/maze.rb', line 16

def grid
  @grid
end

#number_of_columnsObject (readonly)

Returns the value of attribute number_of_columns.



16
17
18
# File 'lib/maze_crosser/maze.rb', line 16

def number_of_columns
  @number_of_columns
end

#number_of_rowsObject (readonly)

Returns the value of attribute number_of_rows.



16
17
18
# File 'lib/maze_crosser/maze.rb', line 16

def number_of_rows
  @number_of_rows
end

#startObject (readonly)

Returns the value of attribute start.



16
17
18
# File 'lib/maze_crosser/maze.rb', line 16

def start
  @start
end

Instance Method Details

#blocked_cell?(coordinates) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/maze_crosser/maze.rb', line 36

def blocked_cell?(coordinates)
  @grid[coordinates[0]][coordinates[1]] == ALLOWED_CHARACTERS[:blocked]
end

#inside_grid?(coordinates) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
# 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