Project

maze_magic

0.0
No commit activity in last 3 years
No release in over 3 years
Ruby gem for generating Maze in form of nested Arrays
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.10
~> 10.0
~> 3.3
 Project Readme

Build Status Code Climate Test Coverage

MazeMagic

Ruby Maze generating gem.

 _______________________________________________________________________________________________________________________ 
| | |  _________     _____  |___  |  _  |    _____  |  ___  |___   ___  |  _  |  ___  |   |___   _|    ___________  |   |
| | | |  _  |   |_| |_    |___  |  _|  _| | |  ___| | |   |_  |  _| |  _| |_  |_  | | | |___  |_____|  _  |  _  | | |_| |
| |_____|_  | |_  |   |_|___  | |_|   |  _| |  _____|  _| |_____| |  _| |   |  ___|_____|  ___|  _  | | |___| | | |_  | |
|_______  |_| |  _| | |  _  | | |  _|_| |  _|_  |___  |   |  _  | | |   | | |_|  ___  | |___  | | | |___  |   | | |  _| |
|_  |  ___|  _| |___| | | | | | | |  ___| |  _|___  | | |_|_  | | | | |___|_   _____| |  _____| | |_  | | |_| | | |___  |
|   | |  _  |_________| |___  | |  _____| |   |  ___| |_______| | | |___|   |_  |  ___|_  |  _  | |  _|_______| |    _| |
| | | |_  |_  | |    _|  _  |_| |___|  ___| | | |  ___|_  |  ___| |_______|_  |_| | |  ___|___| |  _|  _________| |_____|
| | |_  |___| |___| |  ___|_  |_______|  _| |___|___   ___| |   |   |  _______|  _| |_  |  _____|_  | | |    ___| | |   |
| |___| |   | |  ___| | |  _______|  _  |  _|   |   |_|  ___| | | | |_  |_    |_  |  _| |_  | |   |___|  _| |  ___|___| |
|_________|___|_________|_____________|_______|___|_____|_____|___|_________|_____|_________|___|_________|_____________|

Installation

Add this line to your application's Gemfile:

gem 'maze_magic'

And then execute:

$ bundle

Usage

Simple use:

require 'maze_magic'

# maze is a 2 dimensional array of these singleton objects:
#
#       MazeMagic::Edge.instance              # ' '
#       MazeMagic::HorizontalWall.instance    #  _
#       MazeMagic::VerticalWall.instance      #  |
#       MazeMagic::Passage.instance           # ' '
#
# so something like:
#
#    [[Edge, HW, HW, HW, HW, Edge],[VW, P, P,VW,P,P,P, HW],...]
#
def generate_maze(height: 5, width: 5)
  MazeMagic::Generate
    .new(height: height, width: width)
    .tap { |g| g.generate_maze }
    .maze
end

maze = generate_maze

# optional: render in console
MazeMagic::Renderer::ConsoleRenderer.new(cells_grid: maze).call

#  _________ 
# |_  |  _  |
# |  _| |  _|
# | |_  | | |
# | |  _| | |
# |___|_____|

More Complex use:

MazeMagic::Generate is just an Interface. For more complex usage check lib/maze_magic/generate.rb, ...or specs.

Usage in web-app

You can check https://github.com/equivalent/a-maze-ing for example Rails application, but the point is that you will represent the "Maze Wall Representation Objects" as walls/passages. (e.g. <div class="horizontal-wall"></div>, ...; or <img src="/public/horizontal-wall.png">)

stupid example:

@maze = generate_maze(height: 5, width: 5)
<div id="maze">
  <% @maze.each_with_index do |row, index| %>
    <div class="maze-row maze-row-<%= index %>">
      <% row.each do |maze_rep| %>
        <% if maze_rep.is_a? MazeMagic::HorizontalWall  %>
           <img src="/public/horizontal-wall.png">
        <% elsif maze_rep.is_a? MazeMagic::Passage %>
           <img src="/public/passage.png">
           # ...
        <% end %>
      <% end %>
    </div>
    <div class=clear-fix></div>
  <% end %>
</div>

Maze generating Algorithm

At this point there is just altered version of Recursive Backtracking algorithm, but the the gem can be extended by any algorithm.

Contributing

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 code of conduct.

License

The gem is available as open source under the terms of the MIT License.

TODO