maze-solver 1.0.2 → 1.0.3
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.
- data/.travis.yml +4 -7
- data/Gemfile +7 -0
- data/lib/maze_solver.rb +18 -17
- data/lib/maze_solver/version.rb +1 -1
- data/spec/maze_solver_spec.rb +53 -45
- metadata +10 -7
- checksums.yaml +0 -7
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/lib/maze_solver.rb
CHANGED
@@ -3,47 +3,45 @@
|
|
3
3
|
require 'maze_solver/version'
|
4
4
|
|
5
5
|
module MazeSolver
|
6
|
-
|
7
6
|
# Maze solver: solves a maze
|
8
7
|
class MazeSolver
|
9
|
-
|
10
8
|
def initialize(args = {})
|
11
|
-
if args[:from_grid]
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
9
|
+
@grid = if args[:from_grid]
|
10
|
+
args[:from_grid].split("\n")
|
11
|
+
elsif args[:from_file]
|
12
|
+
from_file(args[:from_file])
|
13
|
+
end
|
16
14
|
pad_short_rows
|
17
15
|
end
|
18
16
|
|
19
17
|
def pad_short_rows
|
20
|
-
w =
|
21
|
-
|
18
|
+
w = width
|
19
|
+
grid.each { |row| row << ' ' * (w - row.length) if w > row.length }
|
22
20
|
end
|
23
21
|
|
24
22
|
def height
|
25
|
-
|
23
|
+
grid.size
|
26
24
|
end
|
27
25
|
|
28
26
|
def width
|
29
|
-
|
27
|
+
grid.map { |row| row.length }.max
|
30
28
|
end
|
31
29
|
|
32
30
|
def visitable?(x, y)
|
33
31
|
row, column = y, x
|
34
32
|
return false unless (0...height).include? row
|
35
33
|
return false unless (0...width).include? column
|
36
|
-
|
34
|
+
grid[row][column].chr == ' '
|
37
35
|
end
|
38
36
|
|
39
37
|
def visit(x, y)
|
40
38
|
row, column = y, x
|
41
|
-
|
39
|
+
grid[row][column] = '.'
|
42
40
|
end
|
43
41
|
|
44
42
|
def unvisit(x, y)
|
45
43
|
row, column = y, x
|
46
|
-
|
44
|
+
grid[row][column] = ' '
|
47
45
|
end
|
48
46
|
|
49
47
|
def solve(start_x, start_y, end_x, end_y)
|
@@ -68,13 +66,16 @@ module MazeSolver
|
|
68
66
|
end
|
69
67
|
|
70
68
|
def to_s
|
71
|
-
|
69
|
+
grid.join("\n") << "\n"
|
72
70
|
end
|
73
71
|
|
74
72
|
def from_file(filename)
|
75
|
-
|
76
|
-
|
73
|
+
grid = File.open(filename, 'r').readlines
|
74
|
+
grid.each { | row | row.strip! }
|
77
75
|
end
|
78
76
|
|
77
|
+
private
|
78
|
+
|
79
|
+
attr_accessor :grid
|
79
80
|
end
|
80
81
|
end
|
data/lib/maze_solver/version.rb
CHANGED
data/spec/maze_solver_spec.rb
CHANGED
@@ -2,71 +2,70 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe MazeSolver do
|
5
|
+
describe MazeSolver::MazeSolver do
|
6
6
|
context 'a simple 8 x 3 maze' do
|
7
|
-
|
8
|
-
@maze = MazeSolver::MazeSolver.new(from_grid: <<-GRID
|
7
|
+
let(:grid) do <<-GRID
|
9
8
|
********
|
10
9
|
|
11
10
|
********
|
12
11
|
GRID
|
13
|
-
)
|
14
12
|
end
|
15
13
|
|
16
|
-
|
17
|
-
specify { @maze.height.should eq 3 }
|
14
|
+
subject(:maze) { described_class.new(from_grid: grid) }
|
18
15
|
|
19
|
-
|
20
|
-
|
16
|
+
its(:width) { should eq 8 }
|
17
|
+
its(:height) { should eq 3 }
|
18
|
+
|
19
|
+
it 'is possible to visit an empty space' do
|
20
|
+
expect(maze).to be_visitable(0, 1)
|
21
21
|
end
|
22
22
|
|
23
|
-
it '
|
24
|
-
|
23
|
+
it 'is not possible to visit a wall' do
|
24
|
+
expect(maze).to_not be_visitable(0, 0)
|
25
25
|
end
|
26
26
|
|
27
|
-
it '
|
28
|
-
|
27
|
+
it 'is not possible to visit outside the maze' do
|
28
|
+
expect(maze).to_not be_visitable(8, 3)
|
29
29
|
end
|
30
30
|
|
31
|
-
it '
|
32
|
-
|
33
|
-
|
31
|
+
it 'is not possible to visit a visited space' do
|
32
|
+
maze.visit(0, 1)
|
33
|
+
expect(maze).to_not be_visitable(0, 1)
|
34
34
|
end
|
35
35
|
|
36
|
-
it '
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
it 'is possible to visit an unvisited space' do
|
37
|
+
maze.visit(0, 1)
|
38
|
+
maze.unvisit(0, 1)
|
39
|
+
expect(maze).to be_visitable(0, 1)
|
40
40
|
end
|
41
41
|
|
42
|
-
it '
|
43
|
-
|
42
|
+
it 'is soluble' do
|
43
|
+
expect(maze.solve(0, 1, 7, 1)).to be_true
|
44
44
|
end
|
45
45
|
|
46
|
-
it '
|
47
|
-
|
46
|
+
it 'is soluble backwards' do
|
47
|
+
expect(maze.solve(7, 1, 0, 1)).to be_true
|
48
48
|
end
|
49
49
|
|
50
|
-
it '
|
51
|
-
|
50
|
+
it 'is printable' do
|
51
|
+
expect(maze.to_s).to eq "********\n" +
|
52
52
|
" \n" +
|
53
53
|
"********\n"
|
54
54
|
end
|
55
55
|
|
56
|
-
it '
|
57
|
-
|
58
|
-
|
56
|
+
it 'is printable when solved' do
|
57
|
+
maze.solve(0, 1, 7, 1)
|
58
|
+
expect(maze.to_s).to eq <<-GRID
|
59
59
|
********
|
60
60
|
........
|
61
61
|
********
|
62
62
|
GRID
|
63
63
|
end
|
64
|
-
|
65
64
|
end
|
66
65
|
|
67
66
|
context 'a bigger maze' do
|
68
|
-
|
69
|
-
|
67
|
+
let(:grid) do
|
68
|
+
<<-GRID
|
70
69
|
********
|
71
70
|
** *
|
72
71
|
* ** * *
|
@@ -75,15 +74,19 @@ GRID
|
|
75
74
|
* **** *
|
76
75
|
* *
|
77
76
|
********
|
78
|
-
GRID
|
79
|
-
|
80
|
-
|
77
|
+
GRID
|
78
|
+
end
|
79
|
+
|
80
|
+
subject(:maze) { described_class.new(from_grid: grid) }
|
81
|
+
|
82
|
+
it 'is soluble' do
|
83
|
+
expect(maze.solve(0, 1, 3, 3)).to be_true
|
81
84
|
end
|
82
85
|
end
|
83
86
|
|
84
87
|
context 'an impossible maze' do
|
85
|
-
|
86
|
-
|
88
|
+
let(:grid) do
|
89
|
+
<<-GRID
|
87
90
|
********
|
88
91
|
** *
|
89
92
|
* **** *
|
@@ -93,14 +96,18 @@ GRID
|
|
93
96
|
* *
|
94
97
|
********
|
95
98
|
GRID
|
96
|
-
|
97
|
-
|
99
|
+
end
|
100
|
+
|
101
|
+
subject(:maze) { described_class.new(from_grid: grid) }
|
102
|
+
|
103
|
+
it 'is insoluble' do
|
104
|
+
expect(maze.solve(0, 1, 3, 3)).to be_false
|
98
105
|
end
|
99
106
|
end
|
100
107
|
|
101
108
|
context 'from file' do
|
102
|
-
|
103
|
-
|
109
|
+
let(:test_maze) do
|
110
|
+
StringIO.new(<<-DOC
|
104
111
|
****************** *
|
105
112
|
* * **** *
|
106
113
|
* ***** ***** ** *
|
@@ -108,14 +115,15 @@ GRID
|
|
108
115
|
* * ** ** *
|
109
116
|
* ********** ** *
|
110
117
|
************ *******
|
111
|
-
DOC
|
112
|
-
|
113
|
-
File.stub(:open).with('test.maze', 'r').and_return(test_maze)
|
118
|
+
DOC
|
119
|
+
)
|
114
120
|
end
|
115
121
|
|
116
|
-
|
122
|
+
before { File.stub(:open).with('test.maze', 'r').and_return(test_maze) }
|
123
|
+
|
124
|
+
it 'is creatable' do
|
117
125
|
maze = MazeSolver::MazeSolver.new(from_file: 'test.maze')
|
118
|
-
maze.solve(18, 0, 12, 6).
|
126
|
+
expect(maze.solve(18, 0, 12, 6)).to be_true
|
119
127
|
end
|
120
128
|
end
|
121
129
|
end
|
metadata
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: maze-solver
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- Pete Johns
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2013-
|
12
|
+
date: 2013-12-23 00:00:00.000000000 Z
|
12
13
|
dependencies: []
|
13
14
|
description: A gem to solve a 2D maze.
|
14
15
|
email:
|
@@ -35,27 +36,29 @@ files:
|
|
35
36
|
- spec/spec_helper.rb
|
36
37
|
homepage: https://github.com/johnsyweb/ruby_maze_solver#readme
|
37
38
|
licenses: []
|
38
|
-
metadata: {}
|
39
39
|
post_install_message:
|
40
40
|
rdoc_options: []
|
41
41
|
require_paths:
|
42
42
|
- lib
|
43
43
|
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
44
45
|
requirements:
|
45
|
-
- - '>='
|
46
|
+
- - ! '>='
|
46
47
|
- !ruby/object:Gem::Version
|
47
48
|
version: '0'
|
48
49
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
49
51
|
requirements:
|
50
|
-
- - '>='
|
52
|
+
- - ! '>='
|
51
53
|
- !ruby/object:Gem::Version
|
52
54
|
version: '0'
|
53
55
|
requirements: []
|
54
56
|
rubyforge_project:
|
55
|
-
rubygems_version:
|
57
|
+
rubygems_version: 1.8.23
|
56
58
|
signing_key:
|
57
|
-
specification_version:
|
59
|
+
specification_version: 3
|
58
60
|
summary: Written as an exercise
|
59
61
|
test_files:
|
60
62
|
- spec/maze_solver_spec.rb
|
61
63
|
- spec/spec_helper.rb
|
64
|
+
has_rdoc:
|
checksums.yaml
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz: 338a84ceee7c42e5f6dca79527f869eae3903f3c
|
4
|
-
data.tar.gz: ccfd4aef6af02f769b59fc14974a8c314776a612
|
5
|
-
SHA512:
|
6
|
-
metadata.gz: a989cc66fee38cae3df91bb38a7cf0f9eca56cc0bc545f56da9e8a910d8e31024db68df8972d8261d8a7f15b775193bde39a1ef82f80cd9825c5e77cc2a2905b
|
7
|
-
data.tar.gz: 09ebc17ba7541645bcebff9282459b8fed2deb947a46d5d333a3505802e79bc414c2cccfbfa7c4d655d0510f21bd71c2d40889456e91d61b2dc37375882f24b4
|