Class: Mazinator::MazeSolver

Inherits:
Object
  • Object
show all
Defined in:
lib/mazinator/maze_solver.rb

Constant Summary collapse

NORTH =
0
SOUTH =
1
EAST =
2
WEST =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#currentObject

Returns the value of attribute current.



5
6
7
# File 'lib/mazinator/maze_solver.rb', line 5

def current
  @current
end

#directionObject

Returns the value of attribute direction.



5
6
7
# File 'lib/mazinator/maze_solver.rb', line 5

def direction
  @direction
end

#mazeObject

Returns the value of attribute maze.



5
6
7
# File 'lib/mazinator/maze_solver.rb', line 5

def maze
  @maze
end

#solvedObject

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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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_forwardObject



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_visitedObject



128
129
130
# File 'lib/mazinator/maze_solver.rb', line 128

def mark_current_as_visited
  self.current.visited = true
end

#reverseObject



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

#solveObject



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

#turn_leftObject



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/mazinator/maze_solver.rb', line 102

def turn_left
  case self.direction
    when NORTH
      self.direction = WEST
    when SOUTH
      self.direction = EAST
    when EAST
      self.direction = NORTH
    when WEST
      self.direction = SOUTH
  end
end

#turn_rightObject



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mazinator/maze_solver.rb', line 89

def turn_right
  case self.direction
    when NORTH
      self.direction = EAST
    when SOUTH
      self.direction = WEST
    when EAST
      self.direction = SOUTH
    when WEST
      self.direction = NORTH
  end
end