mazinator 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/mazinator/maze_solver.rb +133 -0
- data/lib/mazinator/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1a01ee450778c10020a2e285b4269a60025264dd
|
4
|
+
data.tar.gz: 7c1b3d14168a05902bbd87a65cd106675d4b4758
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9c9d857df38663af8815fbb339fb309474308f129cba3746f3317f5990277f5bf186cb2e015e280d4f869532d2206a5f5d4b7bd5084bec341dc7095b30c841fe
|
7
|
+
data.tar.gz: 5d807c18604485179feafed78a283e5650b960725b5e9fd43a69f9410d4a779b369971cc1f03c0866ce9650fb618289a3d2bc6a8b8fd19f3b9a6722edf835eab
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require "mazinator/maze"
|
2
|
+
|
3
|
+
module Mazinator
|
4
|
+
class MazeSolver
|
5
|
+
attr_accessor :direction, :solved, :maze, :current
|
6
|
+
|
7
|
+
NORTH = 0
|
8
|
+
SOUTH = 1
|
9
|
+
EAST = 2
|
10
|
+
WEST = 3
|
11
|
+
|
12
|
+
def initialize(maze, direction=EAST)
|
13
|
+
@direction = direction
|
14
|
+
@solved = false
|
15
|
+
@maze = maze
|
16
|
+
@current = maze.start
|
17
|
+
end
|
18
|
+
|
19
|
+
def solve
|
20
|
+
while self.current != self.maze.exit do
|
21
|
+
self.mark_current_as_visited
|
22
|
+
if self.can_go_right?
|
23
|
+
self.turn_right and self.go_forward
|
24
|
+
elsif self.can_go_forward?
|
25
|
+
self.go_forward
|
26
|
+
elsif self.can_go_left?
|
27
|
+
self.turn_left and self.go_forward
|
28
|
+
else
|
29
|
+
self.reverse and self.go_forward
|
30
|
+
end
|
31
|
+
end
|
32
|
+
# mark the last cell as
|
33
|
+
self.mark_current_as_visited
|
34
|
+
end
|
35
|
+
|
36
|
+
def can_go_right?
|
37
|
+
case self.direction
|
38
|
+
when NORTH
|
39
|
+
!self.current.walls[:right]
|
40
|
+
when SOUTH
|
41
|
+
!self.current.walls[:left]
|
42
|
+
when EAST
|
43
|
+
!self.current.walls[:down]
|
44
|
+
when WEST
|
45
|
+
!self.current.walls[:up]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def can_go_forward?
|
50
|
+
case self.direction
|
51
|
+
when NORTH
|
52
|
+
!self.current.walls[:up]
|
53
|
+
when SOUTH
|
54
|
+
!self.current.walls[:down]
|
55
|
+
when EAST
|
56
|
+
!self.current.walls[:right]
|
57
|
+
when WEST
|
58
|
+
!self.current.walls[:left]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
def can_go_left?
|
64
|
+
case self.direction
|
65
|
+
when NORTH
|
66
|
+
!self.current.walls[:left]
|
67
|
+
when SOUTH
|
68
|
+
!self.current.walls[:right]
|
69
|
+
when EAST
|
70
|
+
!self.current.walls[:up]
|
71
|
+
when WEST
|
72
|
+
!self.current.walls[:down]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def go_forward
|
77
|
+
case self.direction
|
78
|
+
when NORTH
|
79
|
+
self.current = self.maze.maze[self.current.row-1, self.current.col]
|
80
|
+
when SOUTH
|
81
|
+
self.current = self.maze.maze[self.current.row+1, self.current.col]
|
82
|
+
when EAST
|
83
|
+
self.current = self.maze.maze[self.current.row, self.current.col+1]
|
84
|
+
when WEST
|
85
|
+
self.current = self.maze.maze[self.current.row, self.current.col-1]
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def turn_right
|
90
|
+
case self.direction
|
91
|
+
when NORTH
|
92
|
+
self.direction = EAST
|
93
|
+
when SOUTH
|
94
|
+
self.direction = WEST
|
95
|
+
when EAST
|
96
|
+
self.direction = SOUTH
|
97
|
+
when WEST
|
98
|
+
self.direction = NORTH
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def turn_left
|
103
|
+
case self.direction
|
104
|
+
when NORTH
|
105
|
+
self.direction = WEST
|
106
|
+
when SOUTH
|
107
|
+
self.direction = EAST
|
108
|
+
when EAST
|
109
|
+
self.direction = NORTH
|
110
|
+
when WEST
|
111
|
+
self.direction = SOUTH
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def reverse
|
116
|
+
case self.direction
|
117
|
+
when NORTH
|
118
|
+
self.direction = SOUTH
|
119
|
+
when SOUTH
|
120
|
+
self.direction = NORTH
|
121
|
+
when EAST
|
122
|
+
self.direction = WEST
|
123
|
+
when WEST
|
124
|
+
self.direction = EAST
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def mark_current_as_visited
|
129
|
+
self.current.visited = true
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
end
|
data/lib/mazinator/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mazinator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kamil Pluta
|
@@ -143,6 +143,7 @@ files:
|
|
143
143
|
- lib/mazinator/cell.rb
|
144
144
|
- lib/mazinator/maze.rb
|
145
145
|
- lib/mazinator/maze_generator.rb
|
146
|
+
- lib/mazinator/maze_solver.rb
|
146
147
|
- lib/mazinator/version.rb
|
147
148
|
- mazinator.gemspec
|
148
149
|
homepage: https://github.com/detfis/mazinator
|