Skip to content

Commit

Permalink
Fix backtrace to multidimensional
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrohml committed Dec 30, 2015
1 parent c1b8f2b commit 4cad05f
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 23 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -8,4 +8,5 @@ gem 'json', '~> 1.8.1'
group :test do
gem 'rspec', '~> 3.1.0'
gem 'coveralls', require: false
gem 'pry'
end
8 changes: 8 additions & 0 deletions Gemfile.lock
Expand Up @@ -2,6 +2,7 @@ GEM
remote: https://rubygems.org/
specs:
OptionParser (0.5.1)
coderay (1.1.0)
coveralls (0.8.10)
json (~> 1.8)
rest-client (>= 1.6.8, < 2)
Expand All @@ -16,8 +17,13 @@ GEM
http-cookie (1.0.2)
domain_name (~> 0.5)
json (1.8.1)
method_source (0.8.2)
mime-types (2.99)
netrc (0.11.0)
pry (0.10.3)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rake (10.1.0)
rest-client (1.8.0)
http-cookie (>= 1.0.2, < 2.0)
Expand All @@ -43,6 +49,7 @@ GEM
json (~> 1.8)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.0)
slop (3.6.0)
structured_warnings (0.1.4)
term-ansicolor (1.3.2)
tins (~> 1.0)
Expand All @@ -59,6 +66,7 @@ DEPENDENCIES
OptionParser (~> 0.5.1)
coveralls
json (~> 1.8.1)
pry
rake (~> 10.1.0)
rspec (~> 3.1.0)
rubytree (~> 0.9.4)
Expand Down
19 changes: 15 additions & 4 deletions bin/rmaze
Expand Up @@ -20,6 +20,9 @@ OptionParser.new do |opts|
opts.on("-h", "--height height", "Specify the maze height (default: #{options[:height]})") do |height|
options[:height] = height
end
opts.on("-d", "--depth depth", "Specify the maze depth (default: #{options[:depth]})") do |depth|
options[:depth] = depth
end
opts.on("-f", "--format format", "Specify the format output (default: #{options[:format]})") do |format|
options[:format] = format.to_sym
end
Expand All @@ -31,7 +34,12 @@ OptionParser.new do |opts|
end
end.parse!

maze = Maze.new options[:width], options[:height]
if not options[:depth]
maze = Maze.new options[:width], options[:height]
else
maze = Maze.new options[:width], options[:height], options[:depth]
end

maze.set_raw_value_all 1

case options[:algorithm]
Expand All @@ -40,13 +48,16 @@ when :backtrace
backtrace.generate
else
$stderr.puts "Error: the algorithm must be set."
exit(-1)
exit -1
end

case options[:format]
when :ascii
maze.matrix.each_with_index do |row, index|
puts row.map(&:to_s).join(' ').gsub('0', ' ').gsub('1', '#')
maze.matrix.each do |row|
row = [row] if maze.dimensions.length == 2
row.each do |depth|
puts depth.map(&:to_s).join(' ').gsub('0', ' ').gsub('1', '#')
end
end
when :json
puts maze.to_json
Expand Down
2 changes: 1 addition & 1 deletion lib/algorithm/backtrace.rb
Expand Up @@ -18,7 +18,7 @@ def generate
@maze.set_value *cell.coords.clone.push(0)
end

current_cell = @maze.cell *@maze.dimensions.map { |d| rand(d) }
current_cell = all_cells[rand(all_cells.length)]
visited_cells.push current_cell

while visited_cells.size != @maze.total_cells
Expand Down
17 changes: 15 additions & 2 deletions lib/maze/maze.rb
Expand Up @@ -11,7 +11,7 @@ def allocate(value = 0)
axis = [value] * dr
else
(0...dr).each do
axis << axis_before.clone
axis << Marshal.load(Marshal.dump(axis_before)) # deep clone
end
end
axis_before = axis
Expand Down Expand Up @@ -48,7 +48,8 @@ def evaluate_indices(dim_sizes)
end

public
attr_reader :dimensions, :width_full, :height_full, :matrix
attr_accessor :matrix
attr_reader :dimensions

def initialize(*dimensions)
@dimensions = dimensions.map { |d| d.to_i }.freeze
Expand All @@ -57,6 +58,18 @@ def initialize(*dimensions)
@hash = @dimensions.reduce(""){ |accum, d| "#{accum}#{d}" }.to_i
end

def self.from_array(matrix)
dimensions = []
matrix_aux = matrix
while matrix_aux.is_a? Array
dimensions.push matrix_aux.length
matrix_aux = matrix_aux[0]
end
maze = Maze.new *dimensions
maze.matrix = Marshal.load(Marshal.dump(matrix)) # deep clone
maze
end

def total_cells
dimensions.reduce(1) { |accum, d| accum*d }
end
Expand Down
2 changes: 1 addition & 1 deletion rmaze.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'rmaze'
s.version = '2.0.0'
s.version = '2.1.0'
s.date = '2014-10-01'
s.licenses = ["MIT"]
s.platform = Gem::Platform::RUBY
Expand Down
4 changes: 2 additions & 2 deletions spec/maze/maze_cell_spec.rb
Expand Up @@ -81,7 +81,7 @@
expect(maze_cell.has_wall_backward(1)).to be_truthy
end

it '#connected?' do
it '#connected? (0)' do
coords = [0, 0]
@maze.set_raw_value_all 0
maze_cell = @maze.cell *coords
Expand All @@ -99,7 +99,7 @@
expect(maze_cell).to be_connected
end

it '#connected? (3x3)' do
it '#connected? (1)' do
maze = Maze.new 3, 3
maze.set_raw_value_all 1
maze_cell1 = maze.cell 1, 1
Expand Down
38 changes: 25 additions & 13 deletions spec/maze/maze_spec.rb
@@ -1,7 +1,7 @@
describe Maze do
it '#initialize 2d' do
width = rand(10) + 15
height = rand(10) + 15
width = rand(3) + 15
height = rand(3) + 15
maze = Maze.new(width, height)
expect(maze.dimensions[0]).to eq(width)
expect(maze.dimensions[1]).to eq(height)
Expand All @@ -10,7 +10,7 @@
end

it '#initialize Nd' do
dimensions = ([nil]*(rand(10)+3)).map { |_| 1 + rand(4) }
dimensions = ([nil]*(rand(3)+3)).map { |_| 1 + rand(4) }
maze = Maze.new *dimensions
matrix_iter = maze.matrix
dimensions.each_with_index do |d, d_index|
Expand All @@ -20,13 +20,25 @@
end
end

it '#self.from_array 2d' do
maze = Maze.new 4, 8
maze.set_raw_value_all 1
maze.set_value 1, 1, 0
maze_clone = Maze.from_array maze.matrix
expect(maze.matrix).to eq(maze_clone.matrix)
maze_clone.set_value 1, 1, 1
expect(maze.matrix).not_to eq(maze_clone.matrix)
maze_clone.set_raw_value_all 0
expect(maze.matrix).not_to eq(maze_clone.matrix)
end

it '#total_cells 2d' do
maze = Maze.new 4, 8
expect(maze.total_cells).to eq(32)
end

it '#total_cells Nd' do
dimensions = ([nil]*(rand(10)+3)).map { |_| 1 + rand(4) }
dimensions = ([nil]*(rand(3)+3)).map { |_| 1 + rand(4) }
maze = Maze.new *dimensions
expect(maze.total_cells).to eq(maze.dimensions.reduce(1) { |accum, d| accum*d })
end
Expand Down Expand Up @@ -63,7 +75,7 @@
end

it '#coords_to_indices Nd' do
dimensions = ([nil]*(rand(10)+3)).map { |_| 1 + rand(4) }
dimensions = ([nil]*(rand(3)+3)).map { |_| 1 + rand(4) }
maze = Maze.new *dimensions
coords = dimensions.map { |d| rand(d) }
indices = maze.coords_to_indices *coords
Expand All @@ -76,7 +88,7 @@
end

it '#get_raw_value Nd' do
dimensions = ([nil]*(rand(10)+3)).map { |_| 1 + rand(4) }
dimensions = ([nil]*(rand(3)+3)).map { |_| 1 + rand(4) }
maze = Maze.new *dimensions
coords = dimensions.map { |d| rand(d) }
indices = maze.coords_to_indices *coords
Expand All @@ -92,7 +104,7 @@
end

it '#set_raw_value Nd' do
dimensions = ([nil]*(rand(10)+3)).map { |_| 1 + rand(4) }
dimensions = ([nil]*(rand(3)+3)).map { |_| 1 + rand(4) }
maze = Maze.new *dimensions
coords = dimensions.map { |d| rand(d) }
value = rand(99) + 1
Expand All @@ -112,7 +124,7 @@
end

it '#set_raw_value_all Nd' do
dimensions = ([nil]*(rand(10)+3)).map { |_| 1 + rand(4) }
dimensions = ([nil]*(rand(3)+3)).map { |_| 1 + rand(4) }
maze = Maze.new *dimensions
maze.set_raw_value_all 11
coords = maze.dimensions.map { |d| rand(d) }
Expand All @@ -126,7 +138,7 @@
end

it '#get_value Nd' do
dimensions = ([nil]*(rand(10)+3)).map { |_| 1 + rand(4) }
dimensions = ([nil]*(rand(3)+3)).map { |_| 1 + rand(4) }
maze = Maze.new *dimensions
coords = dimensions.map { |d| rand(d) }
expect(maze.get_value(*coords)).to eq(0)
Expand All @@ -141,7 +153,7 @@
end

it '#set_value Nd' do
dimensions = ([nil]*(rand(10)+3)).map { |_| 1 + rand(4) }
dimensions = ([nil]*(rand(3)+3)).map { |_| 1 + rand(4) }
maze = Maze.new *dimensions
coords = dimensions.map { |d| rand(d) }
value = rand(99) + 1
Expand All @@ -161,7 +173,7 @@
end

it '#cell Nd' do
dimensions = ([nil]*(rand(10)+3)).map { |_| 1 + rand(4) }
dimensions = ([nil]*(rand(3)+3)).map { |_| 1 + rand(4) }
maze = Maze.new *dimensions
coords = dimensions.map { |d| rand(d) }
cell = maze.cell *coords
Expand Down Expand Up @@ -211,7 +223,7 @@
maze = Maze.new 2, 2, 2
maze.set_raw_value_all 1
maze.set_value 1, 1, 1, 0
expect(maze.to_json).to eq('[[[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,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,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,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,0,1],[1,1,1,1,1]]]')
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]]]')
end

it '#hash 2d' do
Expand All @@ -220,7 +232,7 @@
end

it '#hash Nd' do
dimensions = ([nil]*(rand(10)+3)).map { |_| 1 + rand(4) }
dimensions = ([nil]*(rand(3)+3)).map { |_| 1 + rand(4) }
maze = Maze.new *dimensions
expect(maze.hash).to eq(dimensions.reduce(""){ |accum, d| "#{accum}#{d}" }.to_i)
end
Expand Down

0 comments on commit 4cad05f

Please sign in to comment.