Class: Mazinator::MazeSolver
- Inherits:
-
Object
- Object
- Mazinator::MazeSolver
- Defined in:
- lib/mazinator/maze_solver.rb
Constant Summary collapse
- NORTH =
0
- SOUTH =
1
- EAST =
2
- WEST =
3
Instance Attribute Summary collapse
-
#current ⇒ Object
Returns the value of attribute current.
-
#direction ⇒ Object
Returns the value of attribute direction.
-
#maze ⇒ Object
Returns the value of attribute maze.
-
#solved ⇒ Object
Returns the value of attribute solved.
Instance Method Summary collapse
- #can_go_forward? ⇒ Boolean
- #can_go_left? ⇒ Boolean
- #can_go_right? ⇒ Boolean
- #go_forward ⇒ Object
-
#initialize(maze, direction = EAST) ⇒ MazeSolver
constructor
A new instance of MazeSolver.
- #mark_current_as_visited ⇒ Object
- #reverse ⇒ Object
- #solve ⇒ Object
- #turn_left ⇒ Object
- #turn_right ⇒ Object
Constructor Details
#initialize(maze, direction = EAST) ⇒ MazeSolver
Returns a new instance of MazeSolver.
12 13 14 15 16 17 |
# File 'lib/mazinator/maze_solver.rb', line 12 def initialize(maze, direction=EAST) @direction = direction @solved = false @maze = maze @current = maze.start end |
Instance Attribute Details
#current ⇒ Object
Returns the value of attribute current.
5 6 7 |
# File 'lib/mazinator/maze_solver.rb', line 5 def current @current end |
#direction ⇒ Object
Returns the value of attribute direction.
5 6 7 |
# File 'lib/mazinator/maze_solver.rb', line 5 def direction @direction end |
#maze ⇒ Object
Returns the value of attribute maze.
5 6 7 |
# File 'lib/mazinator/maze_solver.rb', line 5 def maze @maze end |
#solved ⇒ Object
Returns the value of attribute solved.
5 6 7 |
# File 'lib/mazinator/maze_solver.rb', line 5 def solved @solved end |
Instance Method Details
#can_go_forward? ⇒ Boolean
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mazinator/maze_solver.rb', line 49 def can_go_forward? case self.direction when NORTH !self.current.walls[:up] when SOUTH !self.current.walls[:down] when EAST !self.current.walls[:right] when WEST !self.current.walls[:left] end end |
#can_go_left? ⇒ Boolean
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/mazinator/maze_solver.rb', line 63 def can_go_left? case self.direction when NORTH !self.current.walls[:left] when SOUTH !self.current.walls[:right] when EAST !self.current.walls[:up] when WEST !self.current.walls[:down] end end |
#can_go_right? ⇒ Boolean
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/mazinator/maze_solver.rb', line 36 def can_go_right? case self.direction when NORTH !self.current.walls[:right] when SOUTH !self.current.walls[:left] when EAST !self.current.walls[:down] when WEST !self.current.walls[:up] end end |
#go_forward ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/mazinator/maze_solver.rb', line 76 def go_forward case self.direction when NORTH self.current = self.maze.maze[self.current.row-1, self.current.col] when SOUTH self.current = self.maze.maze[self.current.row+1, self.current.col] when EAST self.current = self.maze.maze[self.current.row, self.current.col+1] when WEST self.current = self.maze.maze[self.current.row, self.current.col-1] end end |
#mark_current_as_visited ⇒ Object
128 129 130 |
# File 'lib/mazinator/maze_solver.rb', line 128 def mark_current_as_visited self.current.visited = true end |
#reverse ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/mazinator/maze_solver.rb', line 115 def reverse case self.direction when NORTH self.direction = SOUTH when SOUTH self.direction = NORTH when EAST self.direction = WEST when WEST self.direction = EAST end end |
#solve ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/mazinator/maze_solver.rb', line 19 def solve while self.current != self.maze.exit do self.mark_current_as_visited if self.can_go_right? self.turn_right and self.go_forward elsif self.can_go_forward? self.go_forward elsif self.can_go_left? self.turn_left and self.go_forward else self.reverse and self.go_forward end end # mark the last cell as self.mark_current_as_visited end |