rmaze 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e9e0430fb1d38141827770a72ae3438a451ed1ca
4
- data.tar.gz: 15a61f5a73f128132ed27b0a2f08b2bfaa5bb3e4
3
+ metadata.gz: 6cd11260d180ed896e3d6c195868dba17329769c
4
+ data.tar.gz: 4f226f575426c669a51370872b4748dfa901cbf7
5
5
  SHA512:
6
- metadata.gz: a5a3263729d2f09ded3176dbef2531779c8ebb1f7f3ab38342b473dabd4a94142c1ab7709276c264788eca2f201da5a39728bd4720186fd5da88c096521bfc8a
7
- data.tar.gz: 324f2f23e46d31728e3e4e77f71f31fd958aea40b4bf056b348a33b42c9f9213f490c3e0b3fe25bc68bd0d3ad19aacc8a79820fa26cb105162180f4ea106b03d
6
+ metadata.gz: 9dd1c214c397c6f513f5a7d1fe0b8ebe06803593def2dac27690800bfdf12851f7d61c056e88ee3b3b916fa9b7161c6759c095aa5b242f589fbac2b26ff762be
7
+ data.tar.gz: 7e77ace8be06484d318a4dcaf47637defa0744459ca3a7c23455026dac6388f38df1bcb9ade840b5e16942d295aacfc6b5e6ffa6b711918c526f608db36c89b3
data/Gemfile CHANGED
@@ -3,8 +3,10 @@ source 'https://rubygems.org'
3
3
  gem 'OptionParser', '~> 0.5.1'
4
4
  gem 'rubytree', '~> 0.9.4'
5
5
  gem 'rake', '~> 10.1.0'
6
+ gem 'json', '~> 1.8.1'
6
7
 
7
8
  group :test do
8
9
  gem 'rspec', '~> 3.1.0'
9
10
  gem 'coveralls', require: false
11
+ gem 'pry'
10
12
  end
@@ -2,6 +2,7 @@ GEM
2
2
  remote: https://rubygems.org/
3
3
  specs:
4
4
  OptionParser (0.5.1)
5
+ coderay (1.1.0)
5
6
  coveralls (0.8.10)
6
7
  json (~> 1.8)
7
8
  rest-client (>= 1.6.8, < 2)
@@ -16,8 +17,13 @@ GEM
16
17
  http-cookie (1.0.2)
17
18
  domain_name (~> 0.5)
18
19
  json (1.8.1)
20
+ method_source (0.8.2)
19
21
  mime-types (2.99)
20
22
  netrc (0.11.0)
23
+ pry (0.10.3)
24
+ coderay (~> 1.1.0)
25
+ method_source (~> 0.8.1)
26
+ slop (~> 3.4)
21
27
  rake (10.1.0)
22
28
  rest-client (1.8.0)
23
29
  http-cookie (>= 1.0.2, < 2.0)
@@ -43,6 +49,7 @@ GEM
43
49
  json (~> 1.8)
44
50
  simplecov-html (~> 0.10.0)
45
51
  simplecov-html (0.10.0)
52
+ slop (3.6.0)
46
53
  structured_warnings (0.1.4)
47
54
  term-ansicolor (1.3.2)
48
55
  tins (~> 1.0)
@@ -58,6 +65,8 @@ PLATFORMS
58
65
  DEPENDENCIES
59
66
  OptionParser (~> 0.5.1)
60
67
  coveralls
68
+ json (~> 1.8.1)
69
+ pry
61
70
  rake (~> 10.1.0)
62
71
  rspec (~> 3.1.0)
63
72
  rubytree (~> 0.9.4)
data/bin/rmaze CHANGED
@@ -6,6 +6,7 @@ require 'optparse'
6
6
  options = {
7
7
  :width => 10,
8
8
  :height => 10,
9
+ :format => :ascii,
9
10
  :algorithm => :backtrace
10
11
  }
11
12
  OptionParser.new do |opts|
@@ -19,6 +20,12 @@ OptionParser.new do |opts|
19
20
  opts.on("-h", "--height height", "Specify the maze height (default: #{options[:height]})") do |height|
20
21
  options[:height] = height
21
22
  end
23
+ opts.on("-d", "--depth depth", "Specify the maze depth (default: #{options[:depth]})") do |depth|
24
+ options[:depth] = depth
25
+ end
26
+ opts.on("-f", "--format format", "Specify the format output (default: #{options[:format]})") do |format|
27
+ options[:format] = format.to_sym
28
+ end
22
29
 
23
30
  opts.separator ""
24
31
  opts.separator "Algorithms:"
@@ -27,7 +34,12 @@ OptionParser.new do |opts|
27
34
  end
28
35
  end.parse!
29
36
 
30
- maze = Maze.new options[:width], options[:height]
37
+ if not options[:depth]
38
+ maze = Maze.new options[:width], options[:height]
39
+ else
40
+ maze = Maze.new options[:width], options[:height], options[:depth]
41
+ end
42
+
31
43
  maze.set_raw_value_all 1
32
44
 
33
45
  case options[:algorithm]
@@ -36,9 +48,17 @@ when :backtrace
36
48
  backtrace.generate
37
49
  else
38
50
  $stderr.puts "Error: the algorithm must be set."
39
- exit(-1)
51
+ exit -1
40
52
  end
41
53
 
42
- maze.matrix.each_with_index do |row, index|
43
- puts row.map(&:to_s).join(' ').gsub('0', ' ').gsub('1', '#')
54
+ case options[:format]
55
+ when :ascii
56
+ maze.matrix.each do |row|
57
+ row = [row] if maze.dimensions.length == 2
58
+ row.each do |depth|
59
+ puts depth.map(&:to_s).join(' ').gsub('0', ' ').gsub('1', '#')
60
+ end
61
+ end
62
+ when :json
63
+ puts maze.to_json
44
64
  end
@@ -18,7 +18,7 @@ class Backtrace
18
18
  @maze.set_value *cell.coords.clone.push(0)
19
19
  end
20
20
 
21
- current_cell = @maze.cell *@maze.dimensions.map { |d| rand(d) }
21
+ current_cell = all_cells[rand(all_cells.length)]
22
22
  visited_cells.push current_cell
23
23
 
24
24
  while visited_cells.size != @maze.total_cells
@@ -11,7 +11,7 @@ class Maze
11
11
  axis = [value] * dr
12
12
  else
13
13
  (0...dr).each do
14
- axis << axis_before.clone
14
+ axis << Marshal.load(Marshal.dump(axis_before)) # deep clone
15
15
  end
16
16
  end
17
17
  axis_before = axis
@@ -48,7 +48,8 @@ class Maze
48
48
  end
49
49
 
50
50
  public
51
- attr_reader :dimensions, :width_full, :height_full, :matrix
51
+ attr_accessor :matrix
52
+ attr_reader :dimensions
52
53
 
53
54
  def initialize(*dimensions)
54
55
  @dimensions = dimensions.map { |d| d.to_i }.freeze
@@ -57,6 +58,18 @@ class Maze
57
58
  @hash = @dimensions.reduce(""){ |accum, d| "#{accum}#{d}" }.to_i
58
59
  end
59
60
 
61
+ def self.from_array(matrix)
62
+ dimensions = []
63
+ matrix_aux = matrix
64
+ while matrix_aux.is_a? Array
65
+ dimensions.push matrix_aux.length
66
+ matrix_aux = matrix_aux[0]
67
+ end
68
+ maze = Maze.new *dimensions
69
+ maze.matrix = Marshal.load(Marshal.dump(matrix)) # deep clone
70
+ maze
71
+ end
72
+
60
73
  def total_cells
61
74
  dimensions.reduce(1) { |accum, d| accum*d }
62
75
  end
@@ -126,6 +139,10 @@ class Maze
126
139
  set_between_cells cell_a, cell_b, 1
127
140
  end
128
141
 
142
+ def to_json
143
+ JSON.unparse @matrix
144
+ end
145
+
129
146
  def hash
130
147
  @hash
131
148
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'rmaze'
3
- s.version = '2.0.0'
3
+ s.version = '2.1.0'
4
4
  s.date = '2014-10-01'
5
5
  s.licenses = ["MIT"]
6
6
  s.platform = Gem::Platform::RUBY
@@ -15,4 +15,5 @@ Gem::Specification.new do |s|
15
15
  s.require_paths = ["lib"]
16
16
  s.add_runtime_dependency 'rubytree', '~> 0.9'
17
17
  s.add_runtime_dependency 'OptionParser', '~> 0.5.1'
18
+ s.add_runtime_dependency 'json', '~> 1.8.1'
18
19
  end
@@ -81,7 +81,7 @@ describe MazeCell do
81
81
  expect(maze_cell.has_wall_backward(1)).to be_truthy
82
82
  end
83
83
 
84
- it '#connected?' do
84
+ it '#connected? (0)' do
85
85
  coords = [0, 0]
86
86
  @maze.set_raw_value_all 0
87
87
  maze_cell = @maze.cell *coords
@@ -99,7 +99,7 @@ describe MazeCell do
99
99
  expect(maze_cell).to be_connected
100
100
  end
101
101
 
102
- it '#connected? (3x3)' do
102
+ it '#connected? (1)' do
103
103
  maze = Maze.new 3, 3
104
104
  maze.set_raw_value_all 1
105
105
  maze_cell1 = maze.cell 1, 1
@@ -1,7 +1,7 @@
1
1
  describe Maze do
2
2
  it '#initialize 2d' do
3
- width = rand(10) + 15
4
- height = rand(10) + 15
3
+ width = rand(3) + 15
4
+ height = rand(3) + 15
5
5
  maze = Maze.new(width, height)
6
6
  expect(maze.dimensions[0]).to eq(width)
7
7
  expect(maze.dimensions[1]).to eq(height)
@@ -10,7 +10,7 @@ describe Maze do
10
10
  end
11
11
 
12
12
  it '#initialize Nd' do
13
- dimensions = ([nil]*(rand(10)+3)).map { |_| 1 + rand(4) }
13
+ dimensions = ([nil]*(rand(3)+3)).map { |_| 1 + rand(4) }
14
14
  maze = Maze.new *dimensions
15
15
  matrix_iter = maze.matrix
16
16
  dimensions.each_with_index do |d, d_index|
@@ -20,13 +20,25 @@ describe Maze do
20
20
  end
21
21
  end
22
22
 
23
+ it '#self.from_array 2d' do
24
+ maze = Maze.new 4, 8
25
+ maze.set_raw_value_all 1
26
+ maze.set_value 1, 1, 0
27
+ maze_clone = Maze.from_array maze.matrix
28
+ expect(maze.matrix).to eq(maze_clone.matrix)
29
+ maze_clone.set_value 1, 1, 1
30
+ expect(maze.matrix).not_to eq(maze_clone.matrix)
31
+ maze_clone.set_raw_value_all 0
32
+ expect(maze.matrix).not_to eq(maze_clone.matrix)
33
+ end
34
+
23
35
  it '#total_cells 2d' do
24
36
  maze = Maze.new 4, 8
25
37
  expect(maze.total_cells).to eq(32)
26
38
  end
27
39
 
28
40
  it '#total_cells Nd' do
29
- dimensions = ([nil]*(rand(10)+3)).map { |_| 1 + rand(4) }
41
+ dimensions = ([nil]*(rand(3)+3)).map { |_| 1 + rand(4) }
30
42
  maze = Maze.new *dimensions
31
43
  expect(maze.total_cells).to eq(maze.dimensions.reduce(1) { |accum, d| accum*d })
32
44
  end
@@ -63,7 +75,7 @@ describe Maze do
63
75
  end
64
76
 
65
77
  it '#coords_to_indices Nd' do
66
- dimensions = ([nil]*(rand(10)+3)).map { |_| 1 + rand(4) }
78
+ dimensions = ([nil]*(rand(3)+3)).map { |_| 1 + rand(4) }
67
79
  maze = Maze.new *dimensions
68
80
  coords = dimensions.map { |d| rand(d) }
69
81
  indices = maze.coords_to_indices *coords
@@ -76,7 +88,7 @@ describe Maze do
76
88
  end
77
89
 
78
90
  it '#get_raw_value Nd' do
79
- dimensions = ([nil]*(rand(10)+3)).map { |_| 1 + rand(4) }
91
+ dimensions = ([nil]*(rand(3)+3)).map { |_| 1 + rand(4) }
80
92
  maze = Maze.new *dimensions
81
93
  coords = dimensions.map { |d| rand(d) }
82
94
  indices = maze.coords_to_indices *coords
@@ -92,7 +104,7 @@ describe Maze do
92
104
  end
93
105
 
94
106
  it '#set_raw_value Nd' do
95
- dimensions = ([nil]*(rand(10)+3)).map { |_| 1 + rand(4) }
107
+ dimensions = ([nil]*(rand(3)+3)).map { |_| 1 + rand(4) }
96
108
  maze = Maze.new *dimensions
97
109
  coords = dimensions.map { |d| rand(d) }
98
110
  value = rand(99) + 1
@@ -112,7 +124,7 @@ describe Maze do
112
124
  end
113
125
 
114
126
  it '#set_raw_value_all Nd' do
115
- dimensions = ([nil]*(rand(10)+3)).map { |_| 1 + rand(4) }
127
+ dimensions = ([nil]*(rand(3)+3)).map { |_| 1 + rand(4) }
116
128
  maze = Maze.new *dimensions
117
129
  maze.set_raw_value_all 11
118
130
  coords = maze.dimensions.map { |d| rand(d) }
@@ -126,7 +138,7 @@ describe Maze do
126
138
  end
127
139
 
128
140
  it '#get_value Nd' do
129
- dimensions = ([nil]*(rand(10)+3)).map { |_| 1 + rand(4) }
141
+ dimensions = ([nil]*(rand(3)+3)).map { |_| 1 + rand(4) }
130
142
  maze = Maze.new *dimensions
131
143
  coords = dimensions.map { |d| rand(d) }
132
144
  expect(maze.get_value(*coords)).to eq(0)
@@ -141,7 +153,7 @@ describe Maze do
141
153
  end
142
154
 
143
155
  it '#set_value Nd' do
144
- dimensions = ([nil]*(rand(10)+3)).map { |_| 1 + rand(4) }
156
+ dimensions = ([nil]*(rand(3)+3)).map { |_| 1 + rand(4) }
145
157
  maze = Maze.new *dimensions
146
158
  coords = dimensions.map { |d| rand(d) }
147
159
  value = rand(99) + 1
@@ -161,7 +173,7 @@ describe Maze do
161
173
  end
162
174
 
163
175
  it '#cell Nd' do
164
- dimensions = ([nil]*(rand(10)+3)).map { |_| 1 + rand(4) }
176
+ dimensions = ([nil]*(rand(3)+3)).map { |_| 1 + rand(4) }
165
177
  maze = Maze.new *dimensions
166
178
  coords = dimensions.map { |d| rand(d) }
167
179
  cell = maze.cell *coords
@@ -200,13 +212,27 @@ describe Maze do
200
212
  expect(maze.get_raw_value(*indices)).to eq(1)
201
213
  end
202
214
 
215
+ it '#to_json 2d' do
216
+ maze = Maze.new 4, 8
217
+ maze.set_raw_value_all 1
218
+ maze.set_value 1, 1, 0
219
+ expect(maze.to_json).to eq('[[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],[1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]]')
220
+ end
221
+
222
+ it '#to_json 3d' do
223
+ maze = Maze.new 2, 2, 2
224
+ maze.set_raw_value_all 1
225
+ maze.set_value 1, 1, 1, 0
226
+ expect(maze.to_json).to eq('[[[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1]],[[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1]],[[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1]],[[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,0,1],[1,1,1,1,1]],[[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1]]]')
227
+ end
228
+
203
229
  it '#hash 2d' do
204
230
  maze = Maze.new 4, 8
205
231
  expect(maze.hash).to eq(48)
206
232
  end
207
233
 
208
234
  it '#hash Nd' do
209
- dimensions = ([nil]*(rand(10)+3)).map { |_| 1 + rand(4) }
235
+ dimensions = ([nil]*(rand(3)+3)).map { |_| 1 + rand(4) }
210
236
  maze = Maze.new *dimensions
211
237
  expect(maze.hash).to eq(dimensions.reduce(""){ |accum, d| "#{accum}#{d}" }.to_i)
212
238
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rmaze
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pedro Lira
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.5.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.8.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.8.1
41
55
  description: Ruby library for maze generation
42
56
  email: pedrohml@gmail.com
43
57
  executables:
@@ -89,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
103
  version: '0'
90
104
  requirements: []
91
105
  rubyforge_project:
92
- rubygems_version: 2.4.6
106
+ rubygems_version: 2.4.8
93
107
  signing_key:
94
108
  specification_version: 4
95
109
  summary: RMaze