subparry_labyrinth_solver 1.0.4 → 1.1.0
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/CHANGELOG.md +5 -0
- data/README.md +3 -5
- data/lib/subparry_labyrinth_solver.rb +3 -1
- data/lib/subparry_labyrinth_solver/solver.rb +29 -2
- data/lib/subparry_labyrinth_solver/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e6b29e16241f2cc49228dade89632f4abae2e88aaa65817f75efeffddbef424b
|
4
|
+
data.tar.gz: 6660e17d4047fe5aae28e899cff81ad7dc512247271641006ed1e8edf938dba8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c657486a382ba18ddbf507bbce29a8a76ab6ca22e00795876b2014238d44fab0f746b345dad1f633e86ee1fc6bce29a7cf78fbe2d89c4c57f8469f8447fab751
|
7
|
+
data.tar.gz: 6dd3f0a61db28f164baed8b3bdd79eb91b09f8e9197c1ec3bec34f484c73df523e60b2ab9b9897cdb6b89b2328c9b6a252c176b9d96c2715f9a4413fd46a4e6b
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -10,9 +10,7 @@ Fun little project about labyrinth solving inspired in mice that find cheese in
|
|
10
10
|
|
11
11
|
It exposes some classes to create a labyrinth and then find the path to the cheese.
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
That being said, I plan to support that kind of labyrinths soon.
|
13
|
+
Note that although the paths found are mostly efficient, the algorithm does not focus on finding **the best** path to the cheese.
|
16
14
|
|
17
15
|
## Usage
|
18
16
|
|
@@ -40,7 +38,7 @@ data = [
|
|
40
38
|
]
|
41
39
|
]
|
42
40
|
|
43
|
-
#
|
41
|
+
# The above data represents this maze:
|
44
42
|
# ___ ___
|
45
43
|
# | | 🧀|
|
46
44
|
# | | |
|
@@ -72,6 +70,6 @@ solver.position # => <struct x: 1, y: 0>
|
|
72
70
|
|
73
71
|
## Pending
|
74
72
|
|
75
|
-
- Support labyrinths with circular paths
|
76
73
|
- Create a labyrinth maker class
|
77
74
|
- Visual representation of labyrinths
|
75
|
+
- Document Node and Labyrinth classes
|
@@ -3,6 +3,8 @@ require 'subparry_labyrinth_solver/labyrinth'
|
|
3
3
|
require 'subparry_labyrinth_solver/solver'
|
4
4
|
module LabyrinthSolver
|
5
5
|
class MissingPathsError < ArgumentError; end
|
6
|
+
|
6
7
|
class InvalidMoveError < RuntimeError; end
|
8
|
+
|
7
9
|
class MissingCheeseError < ArgumentError; end
|
8
|
-
end
|
10
|
+
end
|
@@ -23,6 +23,8 @@ module LabyrinthSolver
|
|
23
23
|
next_dir = OPPOSITE_DIRECTIONS.find { |dir, opp| @labyrinth.open?(dir) && opp != @path.last }
|
24
24
|
return dead_end unless next_dir
|
25
25
|
|
26
|
+
return dead_end if circled?
|
27
|
+
|
26
28
|
go(next_dir.first)
|
27
29
|
@path.push(next_dir.first)
|
28
30
|
end
|
@@ -32,11 +34,36 @@ module LabyrinthSolver
|
|
32
34
|
end
|
33
35
|
|
34
36
|
private
|
35
|
-
|
37
|
+
|
36
38
|
def dead_end
|
37
39
|
to_close = path.pop
|
38
40
|
go(OPPOSITE_DIRECTIONS[to_close])
|
39
41
|
close(to_close)
|
40
42
|
end
|
43
|
+
|
44
|
+
def circled?
|
45
|
+
return false if path.size < 4
|
46
|
+
|
47
|
+
idx = path.size - 1
|
48
|
+
control_point = Point.new(position.x, position.y)
|
49
|
+
|
50
|
+
loop do
|
51
|
+
case path[idx]
|
52
|
+
when :up
|
53
|
+
control_point.y += 1
|
54
|
+
when :down
|
55
|
+
control_point.y -= 1
|
56
|
+
when :left
|
57
|
+
control_point.x += 1
|
58
|
+
when :right
|
59
|
+
control_point.x -= 1
|
60
|
+
end
|
61
|
+
return true if control_point == position
|
62
|
+
|
63
|
+
return false if idx.zero?
|
64
|
+
|
65
|
+
idx -= 1
|
66
|
+
end
|
67
|
+
end
|
41
68
|
end
|
42
|
-
end
|
69
|
+
end
|