maze_magic 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 25a41050524d8cfe7ae0abf1795f87015d6b4c86
4
+ data.tar.gz: 6bfb0374d773201bb5b8f46f9e8c85733ad0202c
5
+ SHA512:
6
+ metadata.gz: bc3c0b157789ef61eb2dbaa5b0f9e3dde9f6333b2aa2461ad3b62057e26de77b5f1ba946eb9c613e8640c7dba9579104f473b3d58e7af840e21a05592e2176a3
7
+ data.tar.gz: e50657baaea38e8c4780357b120be77926dc7eb60adfe8a4300c2b1479f2ac467e56eea42ffc97c174457a692d008ba57ff2888610b8c8a8da60fe8cdc9dd9fa
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .ruby-version
11
+ .ruby-gemset
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in maze_magic.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Tomas Valent
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # MazeMagic
2
+
3
+ Ruby Maze generating gem.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'maze_magic', github: 'equivalent/maze_magic'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ ```ruby
20
+ # init grid
21
+ grid = MazeMagic::Grid.new(height: 5, width: 5)
22
+
23
+
24
+ # _________
25
+ # |_|_|_|_|_|
26
+ # |_|_|_|_|_|
27
+ # |_|_|_|_|_|
28
+ # |_|_|_|_|_|
29
+ # |_|_|_|_|_|
30
+
31
+ # apply maze algorithm instructions to grid
32
+ MazeMagic::MazeGenerator::RecursiveBacktracking
33
+ .new(grid: grid)
34
+ .call
35
+
36
+ # translate maze instructions to Cells:
37
+ #
38
+ # MazeMagic::Edge.instance # ' '
39
+ # MazeMagic::HorizontalWall.instance # _
40
+ # MazeMagic::VerticalWall.instance # |
41
+ # MazeMagic::Passage.instance # ' '
42
+ #
43
+ cells_grid = MazeMagic::MazeGenerator::InstructionsGridToCellsGrid
44
+ .new(grid: grid)
45
+ .tap { |generator| generator.call }
46
+ .cells_grid
47
+
48
+ # optional - render the cells viea console
49
+ MazeMagic::Renderer::ConsoleRenderer.new(cells_grid: cells_grid).call
50
+
51
+ # _________
52
+ # |_ | _ |
53
+ # | _| | _|
54
+ # | |_ | | |
55
+ # | | _| | |
56
+ # |___|_____|
57
+
58
+ ```
59
+
60
+ ## Maze generating Algorithm
61
+
62
+ At this point there is just altered version of [Recursive Backtracking
63
+ algorithm](http://weblog.jamisbuck.org/2010/12/27/maze-generation-recursive-backtracking),
64
+ but the the gem can be extended by any algorithm.
65
+
66
+
67
+ ## Contributing
68
+
69
+ Bug reports and pull requests are welcome. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
70
+
71
+
72
+ ## License
73
+
74
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
75
+
76
+ ## TODO
77
+
78
+ * add support for more Algorithms (e.g. http://www.jamisbuck.org/mazes/)
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "maze_magic"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,9 @@
1
+ module MazeMagic
2
+ class Edge
3
+ include Singleton
4
+
5
+ def to_console_print
6
+ ' '
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,22 @@
1
+ module MazeMagic
2
+ class Grid
3
+ extend Forwardable
4
+
5
+ attr_reader :height, :width, :start_x, :start_y
6
+
7
+ def_delegators :grid_map, :size, :each, :length, :[], :to_s, :to_a
8
+
9
+ def initialize(width:, height:, start_x: 0, start_y: 0)
10
+ @width = width
11
+ @height = height
12
+ @start_x = start_x
13
+ @start_y = start_y
14
+ end
15
+
16
+ private
17
+
18
+ def grid_map
19
+ @grid_map ||= Array.new(height) { Array.new(width, 0) }
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,9 @@
1
+ module MazeMagic
2
+ class HorizontalWall
3
+ include Singleton
4
+
5
+ def to_console_print
6
+ '_'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,23 @@
1
+ module MazeMagic
2
+ module MazeGenerator
3
+ class East
4
+ include Singleton
5
+
6
+ def direction_x
7
+ 1
8
+ end
9
+
10
+ def direction_y
11
+ 0
12
+ end
13
+
14
+ def oposite
15
+ West.instance
16
+ end
17
+
18
+ def to_i
19
+ 4
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,59 @@
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
@@ -0,0 +1,23 @@
1
+ module MazeMagic
2
+ module MazeGenerator
3
+ class North
4
+ include Singleton
5
+
6
+ def direction_x
7
+ 0
8
+ end
9
+
10
+ def direction_y
11
+ -1
12
+ end
13
+
14
+ def oposite
15
+ South.instance
16
+ end
17
+
18
+ def to_i
19
+ 1
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ module MazeMagic
2
+ module MazeGenerator
3
+ module Randomnes
4
+ def self.included(base)
5
+ base.send(:attr_writer, :preseeder, :randomizer)
6
+ end
7
+
8
+ def preseeder
9
+ @preseeder ||= MazeMagic::MazeGenerator.default_preseeder
10
+ end
11
+
12
+ def randomizer
13
+ @randomizer ||= MazeMagic::MazeGenerator.default_randomizer
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,49 @@
1
+ module MazeMagic
2
+ module MazeGenerator
3
+ ## RecursiveBacktracking algorithm for generating Maze
4
+ #
5
+ # original author Jamis Buck
6
+ #
7
+ # http://weblog.jamisbuck.org/2010/12/27/maze-generation-recursive-backtracking
8
+ #
9
+ # I've wraped the algorith with objects
10
+ #
11
+ class RecursiveBacktracking
12
+ include Randomnes
13
+
14
+ attr_reader :grid
15
+
16
+ def initialize(grid: grid)
17
+ @grid = grid
18
+ end
19
+
20
+ def call
21
+ preseeder.call
22
+ algorithm(grid, grid.start_x, grid.start_y)
23
+ grid
24
+ end
25
+
26
+ private
27
+
28
+ def algorithm(grid, current_x, current_y)
29
+ directions = [
30
+ North.instance,
31
+ South.instance,
32
+ East.instance,
33
+ West.instance
34
+ ]
35
+ .sort_by{randomizer.call}
36
+
37
+ directions.each do |direction|
38
+ next_x, next_y = current_x + direction.direction_x, current_y + direction.direction_y
39
+
40
+ if next_y.between?(0, grid.length-1) && next_x.between?(0, grid[next_y].length-1) && grid[next_y][next_x] == 0
41
+ grid[current_y][current_x] |= direction.to_i
42
+ grid[next_y][next_x] |= direction.oposite.to_i
43
+ algorithm(grid, next_x, next_y)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,23 @@
1
+ module MazeMagic
2
+ module MazeGenerator
3
+ class South
4
+ include Singleton
5
+
6
+ def direction_x
7
+ 0
8
+ end
9
+
10
+ def direction_y
11
+ 1
12
+ end
13
+
14
+ def oposite
15
+ North.instance
16
+ end
17
+
18
+ def to_i
19
+ 2
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module MazeMagic
2
+ module MazeGenerator
3
+ class West
4
+ include Singleton
5
+
6
+ def direction_x
7
+ -1
8
+ end
9
+
10
+ def direction_y
11
+ 0
12
+ end
13
+
14
+ def oposite
15
+ East.instance
16
+ end
17
+
18
+ def to_i
19
+ 8
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ module MazeMagic
2
+ module MazeGenerator
3
+ def self.default_preseeder
4
+ ->{ srand(MazeMagic::MazeGenerator.default_randomizer.call(0xFFFF_FFFF).to_i) }
5
+ end
6
+
7
+ def self.default_randomizer
8
+ ->(*args){ rand(*args) }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module MazeMagic
2
+ class Passage
3
+ include Singleton
4
+
5
+ def to_console_print
6
+ ' '
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ module MazeMagic
2
+ module Renderer
3
+ class ConsoleRenderer
4
+ attr_reader :cells_grid
5
+ attr_writer :printer
6
+
7
+ def initialize(cells_grid:)
8
+ @cells_grid = cells_grid
9
+ end
10
+
11
+ def call
12
+ cells_grid.each do |row|
13
+ row.each do |cell|
14
+ printer.call(cell.to_console_print)
15
+ end
16
+ printer.call("\n")
17
+ end
18
+ end
19
+
20
+ def printer
21
+ @printer ||= ->(*args){print(*args)}
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module MazeMagic
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,9 @@
1
+ module MazeMagic
2
+ class VerticalWall
3
+ include Singleton
4
+
5
+ def to_console_print
6
+ '|'
7
+ end
8
+ end
9
+ end
data/lib/maze_magic.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'forwardable'
2
+ require 'singleton'
3
+ require 'maze_magic/version'
4
+ require 'maze_magic/grid'
5
+ require 'maze_magic/maze_generator'
6
+ require 'maze_magic/maze_generator/randomnes'
7
+
8
+ %w(north south east west).each do |file|
9
+ require "maze_magic/maze_generator/#{file}"
10
+ end
11
+
12
+ %w(horizontal_wall vertical_wall passage edge).each do |file|
13
+ require "maze_magic/#{file}"
14
+ end
15
+
16
+ require 'maze_magic/maze_generator/recursive_backtracking'
17
+ require 'maze_magic/maze_generator/instructions_grid_to_cells_grid'
18
+
19
+ require 'maze_magic/renderer/console_renderer'
20
+
21
+ module MazeMagic
22
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'maze_magic/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "maze_magic"
8
+ spec.version = MazeMagic::VERSION
9
+ spec.authors = ["Tomas Valent"]
10
+ spec.email = ["equivalent@eq8.eu"]
11
+
12
+ spec.summary = %q{Maze generating gem}
13
+ spec.description = %q{Ruby gem for generating Maze in form of nested Arrays}
14
+ spec.homepage = 'https://github.com/equivalent/maze_magic'
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.10"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.3"
33
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: maze_magic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tomas Valent
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.3'
55
+ description: Ruby gem for generating Maze in form of nested Arrays
56
+ email:
57
+ - equivalent@eq8.eu
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/maze_magic.rb
73
+ - lib/maze_magic/edge.rb
74
+ - lib/maze_magic/grid.rb
75
+ - lib/maze_magic/horizontal_wall.rb
76
+ - lib/maze_magic/maze_generator.rb
77
+ - lib/maze_magic/maze_generator/east.rb
78
+ - lib/maze_magic/maze_generator/instructions_grid_to_cells_grid.rb
79
+ - lib/maze_magic/maze_generator/north.rb
80
+ - lib/maze_magic/maze_generator/randomnes.rb
81
+ - lib/maze_magic/maze_generator/recursive_backtracking.rb
82
+ - lib/maze_magic/maze_generator/south.rb
83
+ - lib/maze_magic/maze_generator/west.rb
84
+ - lib/maze_magic/passage.rb
85
+ - lib/maze_magic/renderer/console_renderer.rb
86
+ - lib/maze_magic/version.rb
87
+ - lib/maze_magic/vertical_wall.rb
88
+ - maze_magic.gemspec
89
+ homepage: https://github.com/equivalent/maze_magic
90
+ licenses:
91
+ - MIT
92
+ metadata:
93
+ allowed_push_host: https://rubygems.org
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.8
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Maze generating gem
114
+ test_files: []