maze_magic 0.2.1.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -0
- data/Gemfile +1 -0
- data/README.md +35 -0
- data/lib/maze_magic.rb +1 -6
- data/lib/maze_magic/generate.rb +4 -11
- data/lib/maze_magic/maze/edge.rb +11 -0
- data/lib/maze_magic/maze/horizontal_wall.rb +11 -0
- data/lib/maze_magic/maze/passage.rb +11 -0
- data/lib/maze_magic/maze/vertical_wall.rb +11 -0
- data/lib/maze_magic/maze_generator/recursive_backtracking.rb +50 -6
- data/lib/maze_magic/maze_generator/recursive_backtracking/east.rb +25 -0
- data/lib/maze_magic/maze_generator/recursive_backtracking/north.rb +25 -0
- data/lib/maze_magic/maze_generator/recursive_backtracking/south.rb +25 -0
- data/lib/maze_magic/maze_generator/recursive_backtracking/west.rb +25 -0
- data/lib/maze_magic/version.rb +1 -1
- metadata +10 -11
- data/lib/maze_magic/edge.rb +0 -9
- data/lib/maze_magic/horizontal_wall.rb +0 -9
- data/lib/maze_magic/maze_generator/east.rb +0 -23
- data/lib/maze_magic/maze_generator/instructions_grid_to_cells_grid.rb +0 -59
- data/lib/maze_magic/maze_generator/north.rb +0 -23
- data/lib/maze_magic/maze_generator/south.rb +0 -23
- data/lib/maze_magic/maze_generator/west.rb +0 -23
- data/lib/maze_magic/passage.rb +0 -9
- data/lib/maze_magic/vertical_wall.rb +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f7447fa71e7555290286900742895d52354352e8
|
4
|
+
data.tar.gz: 66c642337e7bbcf27cfa02f7fa540f5249063fa2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4afc27121862a26cd4c29cae4c798500e0fd21b7fff84ed439d2bf71340ebf2f2cdeafeb94bd3f292fcc3bb4272a8c6dc5e6e5dc9457fa7e7acdb72e83426e10
|
7
|
+
data.tar.gz: d617ea146e65f0b689563405205b60c9d04397e6e0844d6c5cc11c7a1837577784cd276096e5e80122a73db63daf76f5d7f2988fd372f5774f80756b007498a7
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
[![Build Status](https://travis-ci.org/equivalent/maze_magic.svg)](https://travis-ci.org/equivalent/maze_magic)
|
2
2
|
[![Code Climate](https://codeclimate.com/github/equivalent/maze_magic/badges/gpa.svg)](https://codeclimate.com/github/equivalent/maze_magic)
|
3
|
+
[![Test Coverage](https://codeclimate.com/github/equivalent/maze_magic/badges/coverage.svg)](https://codeclimate.com/github/equivalent/maze_magic/coverage)
|
3
4
|
|
4
5
|
# MazeMagic
|
5
6
|
|
@@ -19,6 +20,9 @@ Ruby Maze generating gem.
|
|
19
20
|
|_________|___|_________|_____________|_______|___|_____|_____|___|_________|_____|_________|___|_________|_____________|
|
20
21
|
```
|
21
22
|
|
23
|
+
![](https://41.media.tumblr.com/59fd2a42d464f41af75887e2c2c7dcaf/tumblr_nvy4dupkKf1uicc6wo1_400.png)
|
24
|
+
|
25
|
+
|
22
26
|
## Installation
|
23
27
|
|
24
28
|
Add this line to your application's Gemfile:
|
@@ -75,6 +79,37 @@ MazeMagic::Renderer::ConsoleRenderer.new(cells_grid: maze).call
|
|
75
79
|
`MazeMagic::Generate` is just an Interface. For more complex usage check
|
76
80
|
`lib/maze_magic/generate.rb`, ...or specs.
|
77
81
|
|
82
|
+
#### Usage in web-app
|
83
|
+
|
84
|
+
You can check https://github.com/equivalent/a-maze-ing for example Rails
|
85
|
+
application, but the point is that you will represent the "Maze Wall Representation
|
86
|
+
Objects" as walls/passages. (e.g. `<div class="horizontal-wall"></div>`, ...;
|
87
|
+
or `<img src="/public/horizontal-wall.png">`)
|
88
|
+
|
89
|
+
stupid example:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
@maze = generate_maze(height: 5, width: 5)
|
93
|
+
```
|
94
|
+
|
95
|
+
```erb
|
96
|
+
<div id="maze">
|
97
|
+
<% @maze.each_with_index do |row, index| %>
|
98
|
+
<div class="maze-row maze-row-<%= index %>">
|
99
|
+
<% row.each do |maze_rep| %>
|
100
|
+
<% if maze_rep.is_a? MazeMagic::HorizontalWall %>
|
101
|
+
<img src="/public/horizontal-wall.png">
|
102
|
+
<% elsif maze_rep.is_a? MazeMagic::Passage %>
|
103
|
+
<img src="/public/passage.png">
|
104
|
+
# ...
|
105
|
+
<% end %>
|
106
|
+
<% end %>
|
107
|
+
</div>
|
108
|
+
<div class=clear-fix></div>
|
109
|
+
<% end %>
|
110
|
+
</div>
|
111
|
+
```
|
112
|
+
|
78
113
|
## Maze generating Algorithm
|
79
114
|
|
80
115
|
At this point there is just altered version of [Recursive Backtracking
|
data/lib/maze_magic.rb
CHANGED
@@ -5,16 +5,11 @@ require 'maze_magic/grid'
|
|
5
5
|
require 'maze_magic/maze_generator'
|
6
6
|
require 'maze_magic/maze_generator/randomnes'
|
7
7
|
|
8
|
-
%w(north south east west).each do |file|
|
9
|
-
require "maze_magic/maze_generator/#{file}"
|
10
|
-
end
|
11
|
-
|
12
8
|
%w(horizontal_wall vertical_wall passage edge).each do |file|
|
13
|
-
require "maze_magic/#{file}"
|
9
|
+
require "maze_magic/maze/#{file}"
|
14
10
|
end
|
15
11
|
|
16
12
|
require 'maze_magic/maze_generator/recursive_backtracking'
|
17
|
-
require 'maze_magic/maze_generator/instructions_grid_to_cells_grid'
|
18
13
|
|
19
14
|
require 'maze_magic/renderer/console_renderer'
|
20
15
|
require 'maze_magic/generate'
|
data/lib/maze_magic/generate.rb
CHANGED
@@ -11,9 +11,8 @@ module MazeMagic
|
|
11
11
|
|
12
12
|
def generate_maze
|
13
13
|
initialize_instructions_grid
|
14
|
-
apply_maze_algorithm_to_grid
|
15
14
|
generate_maze_cells_from_instructions
|
16
|
-
|
15
|
+
maze
|
17
16
|
end
|
18
17
|
|
19
18
|
def algorithm
|
@@ -39,12 +38,6 @@ module MazeMagic
|
|
39
38
|
@instructions_grid = grid_klass.new(args)
|
40
39
|
end
|
41
40
|
|
42
|
-
def apply_maze_algorithm_to_grid
|
43
|
-
MazeMagic::MazeGenerator::RecursiveBacktracking
|
44
|
-
.new(grid: instructions_grid)
|
45
|
-
.call
|
46
|
-
end
|
47
|
-
|
48
41
|
## Generate Maze Cell objects from instructions:
|
49
42
|
#
|
50
43
|
# 2 dimensional array of these singleton objects:
|
@@ -64,10 +57,10 @@ module MazeMagic
|
|
64
57
|
# |___|_____|
|
65
58
|
#
|
66
59
|
def generate_maze_cells_from_instructions
|
67
|
-
@maze ||=
|
60
|
+
@maze ||= algorithm
|
68
61
|
.new(grid: instructions_grid)
|
69
|
-
.tap { |generator| generator.
|
70
|
-
.
|
62
|
+
.tap { |generator| generator.generate }
|
63
|
+
.maze
|
71
64
|
end
|
72
65
|
|
73
66
|
def grid_klass
|
@@ -1,3 +1,6 @@
|
|
1
|
+
%w(north south east west).each do |file|
|
2
|
+
require_relative "recursive_backtracking/#{file}"
|
3
|
+
end
|
1
4
|
module MazeMagic
|
2
5
|
module MazeGenerator
|
3
6
|
## RecursiveBacktracking algorithm for generating Maze
|
@@ -9,23 +12,27 @@ module MazeMagic
|
|
9
12
|
# I've wraped the algorith with objects
|
10
13
|
#
|
11
14
|
class RecursiveBacktracking
|
15
|
+
extend Forwardable
|
12
16
|
include Randomnes
|
13
17
|
|
14
|
-
attr_reader :grid
|
18
|
+
attr_reader :grid, :maze
|
19
|
+
|
20
|
+
def_delegators :grid, :width, :height
|
15
21
|
|
16
22
|
def initialize(grid:)
|
17
23
|
@grid = grid
|
18
24
|
end
|
19
25
|
|
20
|
-
def
|
26
|
+
def generate
|
21
27
|
preseeder.call
|
22
|
-
|
23
|
-
|
28
|
+
carve_instructions_to_grid(grid, grid.start_x, grid.start_y)
|
29
|
+
generate_maze
|
30
|
+
maze
|
24
31
|
end
|
25
32
|
|
26
33
|
private
|
27
34
|
|
28
|
-
def
|
35
|
+
def carve_instructions_to_grid(grid, current_x, current_y)
|
29
36
|
directions = [
|
30
37
|
North.instance,
|
31
38
|
South.instance,
|
@@ -40,10 +47,47 @@ module MazeMagic
|
|
40
47
|
if next_y.between?(0, grid.length-1) && next_x.between?(0, grid[next_y].length-1) && grid[next_y][next_x] == 0
|
41
48
|
grid[current_y][current_x] |= direction.to_i
|
42
49
|
grid[next_y][next_x] |= direction.oposite.to_i
|
43
|
-
|
50
|
+
carve_instructions_to_grid(grid, next_x, next_y)
|
44
51
|
end
|
45
52
|
end
|
46
53
|
end
|
54
|
+
|
55
|
+
def generate_maze
|
56
|
+
@maze = []
|
57
|
+
@maze << [edge] + Array.new((width * 2 - 1), hw) + [edge]
|
58
|
+
|
59
|
+
height.times do |y|
|
60
|
+
row = []
|
61
|
+
row << vw
|
62
|
+
|
63
|
+
width.times do |x|
|
64
|
+
row << ((grid[y][x] & South.instance.to_i != 0) ? passage : hw)
|
65
|
+
if grid[y][x] & East.instance.to_i != 0
|
66
|
+
row << (((grid[y][x] | grid[y][x+1]) & South.instance.to_i != 0) ? passage : hw)
|
67
|
+
else
|
68
|
+
row << vw
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
@maze << row
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def vw
|
77
|
+
MazeMagic::Maze::VerticalWall.instance
|
78
|
+
end
|
79
|
+
|
80
|
+
def hw
|
81
|
+
MazeMagic::Maze::HorizontalWall.instance
|
82
|
+
end
|
83
|
+
|
84
|
+
def passage
|
85
|
+
MazeMagic::Maze::Passage.instance
|
86
|
+
end
|
87
|
+
|
88
|
+
def edge
|
89
|
+
MazeMagic::Maze::Edge.instance
|
90
|
+
end
|
47
91
|
end
|
48
92
|
end
|
49
93
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module MazeMagic
|
2
|
+
module MazeGenerator
|
3
|
+
class RecursiveBacktracking
|
4
|
+
class East
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
def direction_x
|
8
|
+
1
|
9
|
+
end
|
10
|
+
|
11
|
+
def direction_y
|
12
|
+
0
|
13
|
+
end
|
14
|
+
|
15
|
+
def oposite
|
16
|
+
West.instance
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_i
|
20
|
+
4
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module MazeMagic
|
2
|
+
module MazeGenerator
|
3
|
+
class RecursiveBacktracking
|
4
|
+
class North
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
def direction_x
|
8
|
+
0
|
9
|
+
end
|
10
|
+
|
11
|
+
def direction_y
|
12
|
+
-1
|
13
|
+
end
|
14
|
+
|
15
|
+
def oposite
|
16
|
+
South.instance
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_i
|
20
|
+
1
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module MazeMagic
|
2
|
+
module MazeGenerator
|
3
|
+
class RecursiveBacktracking
|
4
|
+
class South
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
def direction_x
|
8
|
+
0
|
9
|
+
end
|
10
|
+
|
11
|
+
def direction_y
|
12
|
+
1
|
13
|
+
end
|
14
|
+
|
15
|
+
def oposite
|
16
|
+
North.instance
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_i
|
20
|
+
2
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module MazeMagic
|
2
|
+
module MazeGenerator
|
3
|
+
class RecursiveBacktracking
|
4
|
+
class West
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
def direction_x
|
8
|
+
-1
|
9
|
+
end
|
10
|
+
|
11
|
+
def direction_y
|
12
|
+
0
|
13
|
+
end
|
14
|
+
|
15
|
+
def oposite
|
16
|
+
East.instance
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_i
|
20
|
+
8
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/maze_magic/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: maze_magic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomas Valent
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -70,22 +70,21 @@ files:
|
|
70
70
|
- bin/console
|
71
71
|
- bin/setup
|
72
72
|
- lib/maze_magic.rb
|
73
|
-
- lib/maze_magic/edge.rb
|
74
73
|
- lib/maze_magic/generate.rb
|
75
74
|
- lib/maze_magic/grid.rb
|
76
|
-
- lib/maze_magic/
|
75
|
+
- lib/maze_magic/maze/edge.rb
|
76
|
+
- lib/maze_magic/maze/horizontal_wall.rb
|
77
|
+
- lib/maze_magic/maze/passage.rb
|
78
|
+
- lib/maze_magic/maze/vertical_wall.rb
|
77
79
|
- lib/maze_magic/maze_generator.rb
|
78
|
-
- lib/maze_magic/maze_generator/east.rb
|
79
|
-
- lib/maze_magic/maze_generator/instructions_grid_to_cells_grid.rb
|
80
|
-
- lib/maze_magic/maze_generator/north.rb
|
81
80
|
- lib/maze_magic/maze_generator/randomnes.rb
|
82
81
|
- lib/maze_magic/maze_generator/recursive_backtracking.rb
|
83
|
-
- lib/maze_magic/maze_generator/
|
84
|
-
- lib/maze_magic/maze_generator/
|
85
|
-
- lib/maze_magic/
|
82
|
+
- lib/maze_magic/maze_generator/recursive_backtracking/east.rb
|
83
|
+
- lib/maze_magic/maze_generator/recursive_backtracking/north.rb
|
84
|
+
- lib/maze_magic/maze_generator/recursive_backtracking/south.rb
|
85
|
+
- lib/maze_magic/maze_generator/recursive_backtracking/west.rb
|
86
86
|
- lib/maze_magic/renderer/console_renderer.rb
|
87
87
|
- lib/maze_magic/version.rb
|
88
|
-
- lib/maze_magic/vertical_wall.rb
|
89
88
|
- maze_magic.gemspec
|
90
89
|
homepage: https://github.com/equivalent/maze_magic
|
91
90
|
licenses:
|
data/lib/maze_magic/edge.rb
DELETED
@@ -1,59 +0,0 @@
|
|
1
|
-
module MazeMagic
|
2
|
-
module MazeGenerator
|
3
|
-
class InstructionsGridToCellsGrid
|
4
|
-
attr_reader :instructions_grid, :cells_grid
|
5
|
-
|
6
|
-
def initialize(grid:)
|
7
|
-
@instructions_grid = grid
|
8
|
-
end
|
9
|
-
|
10
|
-
def call
|
11
|
-
@cells_grid = []
|
12
|
-
cells_grid << [edge] + Array.new((width * 2 - 1), hw) + [edge]
|
13
|
-
|
14
|
-
height.times do |y|
|
15
|
-
row = []
|
16
|
-
row << vw
|
17
|
-
|
18
|
-
width.times do |x|
|
19
|
-
row << ((instructions_grid[y][x] & South.instance.to_i != 0) ? passage : hw)
|
20
|
-
if instructions_grid[y][x] & East.instance.to_i != 0
|
21
|
-
row << (((instructions_grid[y][x] | instructions_grid[y][x+1]) & South.instance.to_i != 0) ? passage : hw)
|
22
|
-
else
|
23
|
-
row << vw
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
cells_grid << row
|
28
|
-
end
|
29
|
-
cells_grid
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
def width
|
35
|
-
instructions_grid.width
|
36
|
-
end
|
37
|
-
|
38
|
-
def height
|
39
|
-
instructions_grid.height
|
40
|
-
end
|
41
|
-
|
42
|
-
def vw
|
43
|
-
MazeMagic::VerticalWall.instance
|
44
|
-
end
|
45
|
-
|
46
|
-
def hw
|
47
|
-
MazeMagic::HorizontalWall.instance
|
48
|
-
end
|
49
|
-
|
50
|
-
def passage
|
51
|
-
MazeMagic::Passage.instance
|
52
|
-
end
|
53
|
-
|
54
|
-
def edge
|
55
|
-
MazeMagic::Edge.instance
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
data/lib/maze_magic/passage.rb
DELETED