MAZES
Generate and then solve mazes with C
David Bolton demonstrates how a Raspberry Pi can create mazes in C/SDL and then solve them with animations.
Credit: https://github.com/joewing
OUR EXPERT
David Bolton has been programming since before the war. Which war? Don’t ask. He wrote a 1,000-player RPG game called Quest in 1989 that is still running today.
QUICK TIP
Open another terminal while in the maze and run Scrot in the terminal to get a screen grab. It saves a date/ time-stamped PNG file in the current folder. Raspberry Pi OS comes with Scrot already installed.
You can find many mazegeneration programs in C on the web. Many of those output the mazes as texts. In this example, we’re going to set up Visual Studio Code (aka VS Code) and write a Cprogram that uses the SDL2 library to draw the maze and then solve it.
For this tutorial, the program was developed on a Raspberry Pi 4B, but a 3B would work equally as well. We’ll install this software:
•SDL2 library
•Visual
Studio
Code and extension
• The Clang compiler and linker
The SDL2 (Simple Direct Media) library isn’t installed by default on a Pi. To compile against SDL2, we need the development versions, which include header files for C and libraries to link against. To see what’s available, run this command in a terminal:
$ apt-cache search libsdl2 | grep dev
libsdl2-dev -Simple DirectMedia Layer development files
libsdl2-gfx-dev - development files for SDL2_gfx
libsdl2-image-dev -Image loading library for Simple
DirectMedia Layer 2, development files
libsdl2-mixer-dev -Mixer library for Simple
DirectMedia Layer 2, development files
libsdl2-net-dev -Network library for Simple
DirectMedia Layer 2, development files
libsdl2-ttf-dev -TrueType Font library for Simple
DirectMedia Layer 2, development files
Of the six libraries shown, we only need libsdl2-dev. Before you install it, ensure your system is up to date:
If there are updates, follow it with:
Install SDL2 with this command:
$ sudo apt install libsdl2-dev
Next is Visual Studio Code. Although it’s possible to compile without an IDE, you will find it much easier with one. VS Code calls itself an editor but if it looks like an IDE and acts like one, to our mind it is an IDE.
To start installing Visual Studio Code, click the down arrow, then click Open on the pop-up menu.
Install Visual Studio Code
Open up the Chromium browser and go to https:// code.visualstudio.com/Download/.
Depending on whether you are running the 32-bit or the 64-bit Raspberry Pi OS, click on the Arm32 or the Arm64 buttons just to the right of .deb.