Coder Social home page Coder Social logo

martinopiaggi / unity-maze-generation-using-disjoint-sets Goto Github PK

View Code? Open in Web Editor NEW
11.0 2.0 1.0 49 KB

Random maze generation in Unity 3d using disjoint sets

License: MIT License

C# 100.00%
depth-first-search disjoint-set disjoint-sets disjoint-unions labyrinth labyrinth-game labyrinth-generation maze maze-generator random-generation

unity-maze-generation-using-disjoint-sets's Introduction

Unity-Maze-generation-using-disjoint-sets

mazeCover

Maze generator using disjoin-sets in Unity 3D. There are no cycles and the random maze is solvable by a unique path. I have implemented a Depth First Search to show the path for visualization purposes.

Maze generation visualization

The generation is very quick (50k cells in less than 100 ms) but It's also possible to view the generation at human speed:

parameters

I inserted a delay before the start and a delay at each iteration, so that it's possible to play and see the maze growing visually.

gifMazwe

So simple, but so elegant.

Disjoint-sets are super simple. In this project, they are implemented using two classical optimizations:

  • Optimization with Path Compression
  • Optimization with Union by Rank
public class DisjointSet 
{
    private int[] _set;
    private int[] _rank;

    public DisjointSet(int size)
    {
        _set = new int[size];
        _rank = new int[size];
    }
    
    public void MakeSet(int x)
    {
        _set[x] = x;
        _rank[x] = 0;
    }
    
    public int FindSet(int x)
    {
        if (x != _set[x]) return FindSet(_set[x]);
        return x;
    }

    public void UnionSet(int x, int y)
    {
        var parentX = FindSet(x);
        var parentY = FindSet(y);
        if (_rank[x] > _rank[y]) _set[parentY] = parentX;
        else
        {
            _set[parentX] = parentY;
            if (_rank[x] == _rank[y]) _rank[y]++;
        }
    }
}

References

Disjoint Sets on Wikipedia

Algorithm to generate the maze

unity-maze-generation-using-disjoint-sets's People

Contributors

martinopiaggi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

matty86

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.