STR Coding Club logo STR Coding ClubLearn to code, together
Club Projects

Build a Python Maze Solver as a Club Group Project

Most club projects split badly once the group has a real spread of skill. The two students who have done a few contest problems take the hard part, the newer members get the menu or the colour scheme, and by week three half the room is...

A printed text maze on a desk with a dotted path drawn from start to exit beside a laptop

Most club projects split badly once the group has a real spread of skill. The two students who have done a few contest problems take the hard part, the newer members get the menu or the colour scheme, and by week three half the room is watching someone else type. A maze solver is one of the few projects I have run where that split does not happen, because the hard part and the easy part are genuinely separate pieces that only meet at the end.

The finished program reads a maze from a text file, finds the shortest path from the start to the exit, and prints the maze again with the path drawn in. That sounds like a single task. In practice it breaks into four jobs that a mixed group of six to ten students can work on at the same time, and every job has something worth learning in it.

Why a maze works better than another game

I used to run text adventures and dice games as the default club project. They are fine, and our dice game project still gets good feedback. The weakness is that their logic is shallow. Once the loop and the input handling work, there is nothing left for the stronger members to chew on.

A maze solver has a real algorithm at its centre, breadth first search, which is the same idea contest problems lean on constantly. Stronger students get a properly hard problem. Newer students get file reading, string handling and printing, which are the skills they need anyway. And there is an honest test at the end: either the path on screen is the shortest one or it is not, which you can check by counting steps by hand.

It also produces something you can show. A maze printed with a line of dots running through it looks like an achievement, even to a parent who does not code.

The four parts and who takes them

Agree on one shared data format before anyone writes code. We use a list of strings, one string per row, where # is a wall, a space is open floor, S is the start and E is the exit. Once everyone knows that, the parts below can be built separately and joined in the last session.

  1. The maze files. Two newer members draw five mazes in a plain text editor, from a tiny 7 by 5 grid up to one around 41 by 21. They also make two broken ones on purpose: a maze with no exit and a maze where the exit is walled off. This job teaches more than it looks like, because someone has to decide what counts as valid.
  2. The loader. One newer member writes a function that opens a file, strips the newline from each line, and returns the grid plus the positions of S and E as row and column pairs. The checks go in here: exactly one S, exactly one E, every row the same width.
  3. The solver. Two stronger members write breadth first search. Given the grid, the start and the exit, it returns the list of positions on the shortest path, or None if there is no route.
  4. The printer. One or two members write a function that takes the grid and a path and prints the maze with dots on the path squares. A stretch goal is to animate it, printing the search spreading outward one step at a time.

With eight students, I put two on files, two on the loader with one writing tests, two on the solver and two on the printer. The pairs matter. A loader pair where one person writes the function and the other writes three small test mazes for it finds bugs in the first week that would otherwise surface on demo day.

How the solver works, in plain terms

Breadth first search keeps a queue of squares to visit. It starts with just the S square. Each round, it takes the square at the front of the queue, looks at the four neighbours above, below, left and right, and adds any open neighbour it has not seen yet to the back of the queue. Because it always finishes everything at distance 3 before touching distance 4, the first time it reaches E is guaranteed to be along a shortest path.

To rebuild the path, the solver keeps a dictionary that records, for each square, which square it came from. When it reaches E, it walks that dictionary backwards to S and reverses the list. That dictionary is usually where the solver pair gets stuck, and it is a good place to be stuck, since the same trick appears in a large share of grid problems on contest sites.

Use collections.deque for the queue, not a plain list. Popping from the front of a list gets slow on large mazes. On a 41 by 21 maze you will not notice. On a 401 by 201 maze a member generated as a joke in my last group, the list version took around 9 seconds and the deque version under half a second. Showing that difference live is one of the better five minutes I have had at club.

The advice I would ignore: let beginners pick their own part

A lot of club guides say to let members choose what they work on, so everyone stays motivated. With this project I assign the parts, and I think that is the better call. Left to choose, beginners tend to pick the solver because it sounds like the real thing, then stall for two sessions and quietly drift to their phones. Stronger members, left to choose, sometimes take the printer because it is quick, and finish it in forty minutes.

What works better is assigning by current skill and promising a swap. Once the loader is done and tested, its pair gets to write a second solver using depth first search, and then the group compares the two paths. Depth first search often finds a path, but not the shortest one, and seeing a long winding route next to the short one teaches the difference better than any explanation I could give.

I will admit this is based on maybe a dozen groups, all of them secondary school students. With adult learners who already know what they want to practise, free choice may well work fine.

A six session plan

We meet for 90 minutes a week. Session one is the data format, the pair assignments, and drawing the first two mazes on paper as a group so everyone shares the same picture. Session two, every pair writes the simplest version of its part that runs at all. Session three is for tests, and the solver pair should be able to solve the 7 by 5 maze by the end of it, even if the code is ugly.

Session four is joining the pieces. Do it on one laptop on a projector, with each pair explaining its function's inputs and outputs before it gets plugged in. This is where mismatched assumptions show up, usually someone storing positions as column then row while everyone else used row then column. If the club keeps the code in a shared repository, our notes on running a Git group project cover how to avoid merge chaos on this day.

Session five covers the broken mazes and the stretch goals. Session six is a short demo, with each pair showing one thing they changed after testing. A few more projects in the same spirit are collected under club projects if your group finishes early.

FAQ

Do students need to know about graphs before starting?

No. Treat each open square as a place and its neighbours as the places you can step to. The word graph can come later.

What Python version should we use?

Anything from 3.8 onward works. The project only needs the standard library, mainly collections for the queue.

How big should the test mazes be?

Start with 7 by 5 for checking by hand, then move to around 41 by 21. A very large generated maze is useful for showing speed differences.

What if the solver pair finishes first?

Ask them to add diagonal moves as an option, or to count how many squares each search visited before reaching the exit.

SD
Selene Dragomir

Selene coaches students for programming contests. She writes about practice problems, debugging and the habits that help beginners get unstuck on their own.

More posts by Selene

More in Club Projects

01doorganico 084goad 3hot8 4465456 after5internprogram ahandfulofblessings ahmadpc ahomcybersecurity allodialresources annodex antiquesandperiodcollectibles aotc asianworldkashmir beedlectrics bloggertemplatefree bluefeatheragency bodyweightsolution campaign08blog carfixadvisor catstreetcollective cbstvsports cnfanspurchasing committeeof500yearsofdignityandresistance covertopsdjs cs-kaiyuangame cuestacomunicaciontotal cxoutlet dancebetgame deskgearhub destinyvillastay detectmagazine di33 diamondsteelbuildings donmo dyanadropship emarsysfinance emimattl escapechan eskitarih evautoplay everythingweddings-orlando farmzone feedviewerapp foxstudiosscratch gamevui123 gleasongrade greetingsday hacklabasu haskell iw-kaiyun jagoanproperti jilidskripsi joltbusinessfinance juegodepokergratis juegospucca kaeru-festival latestmehndidesigns lestanzediottaviano litemagazines localcommunityhero lustrsazan mariashebets menswheels newwebstechs1 nikkit ootheday oxigenvpn plurfitness prefetial probudgetcarpetcleaning psychicmrsmartin pyaten qvantor rejoignezlogis rexzbot riselayouts ruchin rwxqsgd5 sakaltime shibiaowang shop-bot skrtips spotlesshaven tetapatrium topselller uneplanteartificielle vipertechcr webtech1223411 wo-kygaming yamaha-mekar yeknava yoybuysheets zenunwa yamaha-mekar jilidskripsi ankertoto ankertoto ankertoto