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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d0bf1b3d99443815ae5ff546b0abbd55512351f75ddb9d7de6299a2166c6f796
4
- data.tar.gz: 4c7f159471b6f6e9c24178cd8d165f60e8a593f6f89160e92720e6c826e9a747
3
+ metadata.gz: e6b29e16241f2cc49228dade89632f4abae2e88aaa65817f75efeffddbef424b
4
+ data.tar.gz: 6660e17d4047fe5aae28e899cff81ad7dc512247271641006ed1e8edf938dba8
5
5
  SHA512:
6
- metadata.gz: 2efa961d309f5b2c729adb7f8c2c4b8a576195566fd27605401e2c9882bdedfdcf15a7562b47bf0a65e96ef5ffc0788ed358c29353abab9169440f33787278dd
7
- data.tar.gz: 566eb78c672a2bec211480ac09651dbdf458f7674983dbae0b4079691d553410ca71dfa9f4f7753a91a9cae1d59e13b5e55b7a18bdc37ab707690ad56a0d232f
6
+ metadata.gz: c657486a382ba18ddbf507bbce29a8a76ab6ca22e00795876b2014238d44fab0f746b345dad1f633e86ee1fc6bce29a7cf78fbe2d89c4c57f8469f8447fab751
7
+ data.tar.gz: 6dd3f0a61db28f164baed8b3bdd79eb91b09f8e9197c1ec3bec34f484c73df523e60b2ab9b9897cdb6b89b2328c9b6a252c176b9d96c2715f9a4413fd46a4e6b
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.1.0 2021-01-22
4
+
5
+ - Added support for labyrinths with circular paths
6
+ - Added support for "empty" labyrinths (only outer walls and cheese)
7
+
3
8
  ## 1.0.4 2021-01-22
4
9
 
5
10
  - First fully functional release
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
- Be advised: **It does not support labyrinths with circular paths and if you try to solve one of these, it will result in an infinite loop**.
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
- # This data represents this maze:
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LabyrinthSolver
4
- VERSION = '1.0.4'
4
+ VERSION = '1.1.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: subparry_labyrinth_solver
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adrian Parry