Mazes - Minecraft on the Raspberry Pi

Sun 05 January 2014

Minecraft for the Raspberry Pi has been with us for about a year now. It's cut down quite a bit from the normal version, being much closer to the pocket edition available for phones and tablets. I wasn't particularly interested in it until I read recently that it comes with a python API. This makes it easy to write scripts to do a great many things, for example, rapidly building structures like castles, bridges and towers. Not only that, but you can make things appear and disappear, or change their structure - think staircases in Hogwarts!

I'm not going to go into the detail of how to do it, because that's already been done very well. See this blog post about a free book written on the subject. I've not read it all, but from the bits I dipped into, it seems like an excellent resource for teaching children (and possibly adults) who are into minecraft and curious about coding in python. The author of the book, Craig Richardson, has a blog full of interesting tips too. I also found this API reference by Martin O'Hanlon to be very handy.

So, on a rainy, winter's Saturday evening, I decided to dabble with this python API and write some code to generate mazes in minecraft. This is a rough sketch of what my quickly hacked-together code does:

  1. Sets a number of rooms, a room size and a room height.
  2. Creates a big cuboid of stone using setBlocks() to house the entire maze.
  3. Creates an entrance and an exit at opposite corners.
  4. Moves an imaginary player to the centre of the first room.
  5. Hollows out the room.
  6. Randomly chooses a wall to make a door in.
  7. If the door coincides with the exit, then STOP - our job is done!
  8. If the wall is exterior, go back to 6.
  9. Move through the door to the centre of the next room, go back to step 5.

It's not particularly efficient, because the imaginary player will often visit the same rooms over and over again, until, by chance, it enters the room adjacent to the exit and chooses to make a door there. But, on the upside, the algorithm does create a viable maze in a few seconds, without any risk of getting stuck in a never-ending loop. To speed things along, and reduce the load on my pi, I stopped it hollowing rooms after the first visit.

To make the maze a little more interesting, and not completely dark, there is a 50% probability of a torch being planted in the centre of a room. Also, to add a little distinctiveness and colour, there is a lower probability that the block under the torch might be made of iron, gold or diamond.

Another thing I added to the script was the ability to erase the previous maze. If any command line argument is given (super hacky!) then the script performs the setBlocks command with block.AIR instead of block.STONE and then exits.

The final thing worth mentioning is how I set up the development environment. I tried using IDLE which came bundled with Raspbian, but it wasn't to my taste. Geany is my weapon of choice for python coding, but instead of rubbing a lamp, I did this:

apt-get install geany

But another problem soon presented itself: the minecraft window was a little messed up and it wouldn't go behind other windows, including geany. Also, when it came time to look up stuff in the web, I found midori on the pi frustratingly slow.

So, to speed things along, I switched to working on my laptop and installed sshfs and mounted a directory with a command similar to this:

sshfs raspberrypi:/home/pi/minecraftpimaze minecraftpimaze

which meant that I could access all the python files on the raspberry pi from my laptop. I fired up geany, ssh'd into the pi in geany's terminal window and was soon coding away in a very sweet development environment with my laptop sitting on the table next to the pi. I should mention that the pi was connected to an old TV, and it took a bit of fettling to get a decent resolution - for sage advice on this point see this blog post.

Of course, I spent almost no time actually trying to solve the mazes I'd created, because, as I'm sure you will agree, the real fun was in coding their creation.