Member-only story

Build a maze with Python

Let me out !

Keno Leon
6 min readMar 29, 2021

--

Some months ago I failed an interview question from Google that required me to write a program to navigate a maze in the least amount of steps, it is by no means a small task since a very popular book on AI ( AI a modern approach ) devotes a lot of pages to this subject, but what I got stumped with and found more interesting was the code needed to make a maze ( which wasn’t provided ) and since I like GUIs and games I thought it would be a cool little thing to learn about, so without much ado, here we go, we’ll be making a basic maze game in python !

Here's the code snippets in Repo form if you prefer...https://github.com/KenoLeon/Medium-Mazes

Step 1. The first thing we need is a canvas/data structure where we can draw structured data, a numpy backed grid fits the bill nicely, I am using PySimpleGui + numpy but you can use another GUI library if you wish, I will also be using and modifying some grid functions from a previous post on Grids ( but you can skip reading it if the code makes sense to you ) :

Should give you:

Notes: We are using square grids to make our life easier, so a grid with cellCount 10 is a 10x10 grid. There are drawGrid() and drawCell(coordinates) methods for drawing the grid and placing a single cell. In order to read a numpy array as the map for the grid, uncomment the placeCells() call, should give you a populated grid based on the random numpy cellMap array.

Step 2: It would be nice if we could move around the canvas with the up/down/left/right arrows of the keyboard…

--

--

Keno Leon
Keno Leon

No responses yet

Write a response