GithubHelp home page GithubHelp logo

maze's Introduction

Maze

The goal of this project is to generate a maze and solve it using algorithms like A*.

๐ŸŽฏ Objectives :

  • Learn how to use Pygame;
  • Implement a maze generator;
  • Implement a path finding algorithm (A*).

Maze generator

In order to generate mazes, I used a backtracking algorithm (ref.).

The algorithm starts by creating a grid. Each cell has four walls (right, left, top and bottom). The algorithm stops running when every cell has been visited. It selects a random cell to be the current one. We then mark it as 'visited' and put this cell on a stack. After that, it goes through the maze by selecting the next cell from one of the neighboring cells that hasnโ€™t been visited yet . It opens the wall connecting the two cells, and marks the next cell as the current one after putting it on the stack.

If the current cell has no unvisited neighbors, the current cell is set as the cell at the top of the stack. This is the backtracking part of the algorithm.

Animation of the generation :

maze_generation

Maze solver

In this section, we will see different algorithms to solve the maze we generated earlier.

Algorithm A*

The algorithm we use to solve the maze is A* ref., it's a heuristic based algorithm. A heuristic is a function that help us determine which cell we should visit.

For this algorithm, the heuristic used will be the distance of the current cell from the exit of the maze. We also determine the notion of cost. The cost of moving from one cell to another is 1; the cost of being in the first cell is 0.

A* begins by initializing two queues, "open" and "close" which store cells of the maze. "open" is a priority queue meaning that elements are stored and sorted based on the value of the cost to which we add the heuristic. Sorting the "open" queue allows us to visit the best candidate first.

The algorithm takes an entry cell and put it in the "open" queue.

While the "open" queue is not empty or the first element is not the exit, we select the first element of the queue which will be the current cell.

Then, for every neighbor of the current cell, we check if they are not already present in the "close" queue or if they are present in the "open" queue with a better cost. If not, we add those neighbors to the "open" queue. Once every neighbor are checked, we remove the current cell from "open" and add it to "close".

Animation of the A* algorithm :

(In blue you can see the cell in 'open' and in red the current cell.)

a_star

โš™๏ธ Requirements :

  • Pygames v 2.0.1

๐Ÿš€ Installation :

To install all the dependencies you can use the command :

pip install -r requirements.txt

maze's People

Contributors

cmoineau avatar

Watchers

 avatar  avatar

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.