rmaze 1.0.0 → 1.5.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: 8de1761ea1c434699b107bfd7ac8f67066ccbb72
4
- data.tar.gz: 5a00adebfa0161e2bc9da4c708d35080f687e385
3
+ metadata.gz: 85febacd983d96080346ecd776ce57a069bf3298
4
+ data.tar.gz: 3fc99125e1fe8afd4c5e01d945b1f0548e9e39e3
5
5
  SHA512:
6
- metadata.gz: ee50ce8778253e1915c1c4591ebb4e2ed6c08a68ef834cfd77116b88d68a18533c7f29ebe57a7f59b2b3b6434d98593d49fa3f6d1bc188723d71f35dfa9786cf
7
- data.tar.gz: f225f4e02d3955852991b646319cbb9dcee0e713df7f55c544bd293a8325b0366805fa055799396c0fc18a7b87c88ffd217db3189fe0f7296ed1943b18f1f186
6
+ metadata.gz: 873a51c14c421519625a731e828a571ad8cbf0f375f09ced7d5e7cd31bc34d811788d16d886abad3319b6de1cf6ad21b6dd95232d67ee6efd3fc3eaf27a4609f
7
+ data.tar.gz: 291daa9e8d1f598c013de04306988d83a3f10e280dec2b92ecfb7c40be682ea45193611b14f2ae486afff75d46d0ae2104e7f87b14b8725b7a71446f09f4c195
File without changes
data/Gemfile CHANGED
@@ -2,6 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  gem 'OptionParser', '~> 0.5.1'
4
4
  gem 'rubytree', '~> 0.9.4'
5
+ gem 'rake', '~> 10.1.0'
5
6
 
6
7
  group :test do
7
8
  gem 'rspec', '~> 3.1.0'
@@ -6,6 +6,7 @@ GEM
6
6
  docile (1.1.5)
7
7
  json (1.8.1)
8
8
  multi_json (1.10.1)
9
+ rake (10.1.0)
9
10
  rspec (3.1.0)
10
11
  rspec-core (~> 3.1.0)
11
12
  rspec-expectations (~> 3.1.0)
@@ -33,6 +34,10 @@ PLATFORMS
33
34
 
34
35
  DEPENDENCIES
35
36
  OptionParser (~> 0.5.1)
37
+ rake (~> 10.1.0)
36
38
  rspec (~> 3.1.0)
37
39
  rubytree (~> 0.9.4)
38
40
  simplecov
41
+
42
+ BUNDLED WITH
43
+ 1.11.2
data/README.md CHANGED
@@ -1,4 +1,64 @@
1
- Maze
2
- ====
1
+ # RMaze
2
+ Ruby library and tool for 2D maze generation
3
3
 
4
- Ruby library for maze generation
4
+ [![Build Status](http://travis-ci.org/pedrohml/rmaze.svg?branch=master)](http://travis-ci.org/pedrohml/rmaze)
5
+ [![Security](http://hakiri.io/github/pedrohml/rmaze/master.svg)](https://hakiri.io/github/pedrohml/rmaze/master)
6
+
7
+ #### Description
8
+ This simple library/tool generates 2D mazes with customizable dimensions.
9
+
10
+ #### Tool usage
11
+ The **rmaze** is in initial version and its simple to use
12
+
13
+ ```
14
+ Usage: rmaze.rb [options]
15
+
16
+ Basic options:
17
+ -w, --width width Specify the maze width (default: 10)
18
+ -h, --height height Specify the maze height (default: 10)
19
+
20
+ Algorithms:
21
+ -b, --backtrace Choose backtrace algorithm (default)
22
+ ```
23
+
24
+ #### Tool usage examples
25
+
26
+ Default usage (*width=10, height=10*)
27
+ ```
28
+ > rmaze
29
+
30
+ # # # # # # # # # # # # # # # # # # # # #
31
+ # # #
32
+ # # # # # # # # # # # # # # # # # #
33
+ # # # # #
34
+ # # # # # # # # # # # # # # # #
35
+ # # # # # #
36
+ # # # # # # # # # # # # # #
37
+ # # # # # # # #
38
+ # # # # # # # # # # # # # #
39
+ # # # # # # #
40
+ # # # # # # # # # # # # # # # # #
41
+ # # # #
42
+ # # # # # # # # # # # # # # # # # #
43
+ # # # # # #
44
+ # # # # # # # # # # # # # #
45
+ # # # # # # # # # #
46
+ # # # # # # # # # # # # #
47
+ # # # # # # #
48
+ # # # # # # # # # # # # # # # #
49
+ # # # #
50
+ # # # # # # # # # # # # # # # # # # # # #
51
+ ```
52
+
53
+ Generate mazes with customized dimensions (*width=5, height=2*)
54
+ ```
55
+ > rmaze -w 5 -h 2
56
+
57
+ # # # # # # # # # # #
58
+ # #
59
+ # # # # # # # # #
60
+ # # #
61
+ # # # # # # # # # # #
62
+ ```
63
+
64
+ Enjoy !
@@ -0,0 +1,19 @@
1
+ # encoding: UTF-8
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+
6
+ begin
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ desc 'Rake test'
10
+ task default: :test
11
+ rescue LoadError
12
+ fail
13
+ end
14
+
15
+ desc 'Pre-commit hook (test)'
16
+ task pre_commit: :test
17
+
18
+ desc 'Run spec tests'
19
+ task test: :spec
@@ -9,7 +9,7 @@ options = {
9
9
  :algorithm => :backtrace
10
10
  }
11
11
  OptionParser.new do |opts|
12
- opts.banner = "Usage: maze_gen.rb [options]"
12
+ opts.banner = "Usage: rmaze [options]"
13
13
 
14
14
  opts.separator ""
15
15
  opts.separator "Basic options:"
@@ -37,4 +37,4 @@ else
37
37
  end
38
38
 
39
39
  maze.generate
40
- maze.debug
40
+ maze.print
@@ -0,0 +1,37 @@
1
+ require 'solver/tree_solver'
2
+
3
+ class Game
4
+ def initialize(maze)
5
+ @maze = maze
6
+ end
7
+
8
+ def select_goal_cell(dificulty)
9
+ dificulty = dificulty.to_sym
10
+ solver = TreeSolver.new(@maze)
11
+ start_x, start_y = 0, 0
12
+ paths = solver.paths(start_x, start_y)
13
+ leafs = {}
14
+ paths.each_leaf do |leaf|
15
+ leafs[leaf.node_depth] = leaf
16
+ end
17
+ depths = leafs.keys.sort
18
+ case dificulty
19
+ when :easy
20
+ selected_depth = depths[(depths.size*0.25).ceil]
21
+ when :medium
22
+ selected_depth = depths[(depths.size*0.5).ceil]
23
+ when :hard
24
+ selected_depth = depths.last
25
+ else
26
+ selected_depth = depths[rand((depths.size/2.0).ceil) + (depths.size/2.0).ceil]
27
+ end
28
+ leafs[selected_depth].content
29
+ end
30
+
31
+ def generate_goal(dificulty)
32
+ selected_cell = select_goal_cell(dificulty)
33
+ i, j = @maze.xy_to_ij(selected_cell.x, selected_cell.y)
34
+ @maze.matrix[i][j] = 2
35
+ @maze
36
+ end
37
+ end
@@ -2,8 +2,6 @@ require 'maze/maze_cell'
2
2
 
3
3
  class Maze
4
4
  protected
5
- attr_reader :matrix
6
-
7
5
  def initialize_matrix
8
6
  @matrix = []
9
7
  (0...@height_full).each do
@@ -30,7 +28,7 @@ class Maze
30
28
  end
31
29
 
32
30
  public
33
- attr_reader :mirrored, :width, :height, :width_full, :height_full
31
+ attr_reader :mirrored, :width, :height, :width_full, :height_full, :matrix
34
32
 
35
33
  def initialize(width, height)
36
34
  @mirrored = false
@@ -40,7 +38,7 @@ class Maze
40
38
  @hash = "#{@width}#{@height}".to_i # optimized pre-computed hash
41
39
  end
42
40
 
43
- def debug
41
+ def print
44
42
  @matrix.each do |row|
45
43
  puts row.join(' ').gsub(/0/, ' ').gsub(/1/, '#').gsub(/2/, 'X')
46
44
  end
@@ -8,7 +8,7 @@ class MazeCell
8
8
  @hash = "#{@maze.hash}#{@x}#{@y}".to_i
9
9
  end
10
10
 
11
- def debug
11
+ def print
12
12
  i, j = @maze.xy_to_ij(@x, @y)
13
13
  puts " #{@maze.value(i - 1, j)} "
14
14
  puts "#{@maze.value(i, j - 1)} #{@maze.value(i, j)} #{@maze.value(i, j + 1)}"
@@ -67,6 +67,11 @@ class MazeCell
67
67
  neighbours << down unless has_wall_down?
68
68
  neighbours
69
69
  end
70
+
71
+ def value
72
+ i, j = xy_to_ij(@x, @y)
73
+ @maze.value(i, j)
74
+ end
70
75
 
71
76
  def hash
72
77
  @hash
@@ -0,0 +1,34 @@
1
+ require 'tree'
2
+
3
+ class TreeSolver
4
+ attr_reader :maze
5
+
6
+ def initialize(maze)
7
+ @maze = maze
8
+ end
9
+
10
+ def paths(start_x, start_y)
11
+ current_cell = @maze.cell(start_x, start_y)
12
+ root_node = current_node = Tree::TreeNode.new("#{current_cell.x},#{current_cell.y}", current_cell)
13
+ visited_cells = [current_cell]
14
+ stack_node = []
15
+ stack_cell = []
16
+ while visited_cells.size != @maze.width * @maze.height
17
+ neighbours = current_cell.connected_neighbours
18
+ unvisited_neighbours = neighbours - visited_cells
19
+ if unvisited_neighbours.size == 0
20
+ current_cell = stack_cell.pop
21
+ current_node = stack_node.pop
22
+ elsif unvisited_neighbours.size >= 1
23
+ current_cell = unvisited_neighbours.shift
24
+ stack_cell += unvisited_neighbours
25
+ stack_node += [current_node]*unvisited_neighbours.size
26
+ end
27
+ visited_cells << current_cell
28
+ new_node = Tree::TreeNode.new("#{current_cell.x},#{current_cell.y}", current_cell)
29
+ current_node << new_node
30
+ current_node = new_node
31
+ end
32
+ root_node
33
+ end
34
+ end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'rmaze'
3
- s.version = '1.0.0'
3
+ s.version = '1.5.0'
4
4
  s.date = '2014-10-01'
5
5
  s.licenses = ["MIT"]
6
6
  s.platform = Gem::Platform::RUBY
@@ -10,6 +10,9 @@ Gem::Specification.new do |s|
10
10
  s.email = 'pedrohml@gmail.com'
11
11
  s.files = `git ls-files`.split("\n")
12
12
  s.test_files = `git ls-files -- spec/*`.split("\n")
13
+ s.executables << 'rmaze'
13
14
  s.homepage = 'https://github.com/pedrohml/rmaze'
14
15
  s.require_paths = ["lib"]
16
+ s.add_runtime_dependency 'rubytree', '~> 0.9'
17
+ s.add_runtime_dependency 'OptionParser', '~> 0.5.1'
15
18
  end
@@ -0,0 +1,33 @@
1
+ describe Game do
2
+ before do
3
+ @maze = MazeBTrace.new(8, 8)
4
+ @maze.generate
5
+ @game = Game.new(@maze)
6
+ end
7
+
8
+ it '#select_goal_cell' do
9
+ selected_cell = @game.select_goal_cell(:hard)
10
+ expect([selected_cell.x, selected_cell.y]).not_to eq([0, 0])
11
+ expect(selected_cell.connected_neighbours.size).to eq(1)
12
+ end
13
+
14
+ it '#generate_goal :easy' do
15
+ maze = @game.generate_goal(:easy)
16
+ expect(maze.matrix.flatten).to include(2)
17
+ end
18
+
19
+ it '#generate_goal :easy' do
20
+ maze = @game.generate_goal(:medium)
21
+ expect(maze.matrix.flatten).to include(2)
22
+ end
23
+
24
+ it '#generate_goal :easy' do
25
+ maze = @game.generate_goal(:hard)
26
+ expect(maze.matrix.flatten).to include(2)
27
+ end
28
+
29
+ it '#generate_goal :easy' do
30
+ maze = @game.generate_goal(:random)
31
+ expect(maze.matrix.flatten).to include(2)
32
+ end
33
+ end
@@ -82,10 +82,10 @@ describe MazeCell do
82
82
  expect(maze_cell != nil).to be_truthy
83
83
  end
84
84
 
85
- it '#debug' do
85
+ it '#print' do
86
86
  allow($stdout).to receive(:puts).exactly(3).times
87
87
  maze_cell = @maze.cell(0, 0)
88
- maze_cell.debug
88
+ maze_cell.print
89
89
  end
90
90
 
91
91
  it '#inspect' do
@@ -40,8 +40,8 @@ describe Maze do
40
40
  expect(@maze.inspect).to eq("#<Maze: @width=#{@width}, @height=#{@height}>")
41
41
  end
42
42
 
43
- it '#debug' do
43
+ it '#print' do
44
44
  allow($stdout).to receive(:puts).exactly(@maze.height_full).times
45
- @maze.debug
45
+ @maze.print
46
46
  end
47
47
  end
@@ -0,0 +1,13 @@
1
+ describe TreeSolver do
2
+ before do
3
+ @maze = MazeBTrace.new(4, 4)
4
+ @maze.generate
5
+ @tree_solver = TreeSolver.new(@maze)
6
+ end
7
+
8
+ it '#paths' do
9
+ paths = @tree_solver.paths(0, 0)
10
+ expect(paths.name).to eq('0,0')
11
+ expect(paths.size).to eq(16)
12
+ end
13
+ 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: 1.0.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pedro Lira
@@ -9,13 +9,43 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2014-10-01 00:00:00.000000000 Z
12
- dependencies: []
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubytree
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: OptionParser
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.5.1
13
41
  description: Ruby library for maze generation
14
42
  email: pedrohml@gmail.com
15
- executables: []
43
+ executables:
44
+ - rmaze
16
45
  extensions: []
17
46
  extra_rdoc_files: []
18
47
  files:
48
+ - ".gemtest"
19
49
  - ".gitignore"
20
50
  - ".rspec"
21
51
  - ".ruby-gemset"
@@ -23,15 +53,20 @@ files:
23
53
  - Gemfile
24
54
  - Gemfile.lock
25
55
  - README.md
56
+ - Rakefile
57
+ - bin/rmaze
58
+ - lib/game/game.rb
26
59
  - lib/maze/maze.rb
27
60
  - lib/maze/maze_btrace.rb
28
61
  - lib/maze/maze_cell.rb
29
62
  - lib/rmaze.rb
30
- - maze_gen.rb
63
+ - lib/solver/tree_solver.rb
31
64
  - rmaze.gemspec
65
+ - spec/game/game_spec.rb
32
66
  - spec/maze/maze_btrace_spec.rb
33
67
  - spec/maze/maze_cell_spec.rb
34
68
  - spec/maze/maze_spec.rb
69
+ - spec/solver/tree_solver_spec.rb
35
70
  - spec/spec_helper.rb
36
71
  homepage: https://github.com/pedrohml/rmaze
37
72
  licenses:
@@ -53,12 +88,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
88
  version: '0'
54
89
  requirements: []
55
90
  rubyforge_project:
56
- rubygems_version: 2.2.2
91
+ rubygems_version: 2.4.8
57
92
  signing_key:
58
93
  specification_version: 4
59
94
  summary: RMaze
60
95
  test_files:
96
+ - spec/game/game_spec.rb
61
97
  - spec/maze/maze_btrace_spec.rb
62
98
  - spec/maze/maze_cell_spec.rb
63
99
  - spec/maze/maze_spec.rb
100
+ - spec/solver/tree_solver_spec.rb
64
101
  - spec/spec_helper.rb