Tools for Learning Clojure, Part V: A Game!

Tools for Learning Clojure, Part V: A Game!

Previous Parts: I, II, III, IV.

I’ve always maintained that the only way to really learn a programming language is to code up some kind of project using it: not something out of a book, but something of my own, something that’s fun. For many years, my project of choice was the old fashioned text adventure. I got started with computers in the mid-1970’s, when we all we had were dumb-terminals and today’s beautifully 3-D rendered games were not even a dream. Text was king; and text adventure games like Advent and Zork seemed like magic. I spent a lot of time writing small games in those days, but doing a full-up “adventure game” always seemed beyond my skills, or perhaps beyond my tools. I tried it in BASIC and didn’t get far; I tried it in Pascal and floundered. And then in college I got a LISP system called LISP-80 that ran on my Kaypro 4, and I discovered that LISP is incredibly well suited to writing text adventures. I don’t have the code to that game anymore, more’s the pity, but I remember quite a lot about it. Memory was very limited, so I had to design the game world in areas, and load and delete different areas as you moved about the world. I had great fun.

It was perhaps inevitable, consequently, that my attempts to learn Clojure would lead me in the direction of writing a text adventure. Back then, I had aspirations to writing a game others might play, or even a serious system for writing adventure games. Things have changed significantly since then; if writing what’s now called “interactive fiction” is your thing, there are a number of excellent systems out there that will help you. The Inform system is particularly amazing, and I spent quite a lot of time playing with it before I realized that I didn’t really want to write adventure games for people to play. Instead, I wanted the fun of implementing the system that the game is written in. The game itself is secondary.

My goal, then, is to write a simple text adventure: a network of rooms that you move around in using simple commands, with objects to pick up and move and manipulate. The focus isn’t on writing a detailed game, but rather on figuring out how to do the various tasks efficiently and beautifully in Clojure.

And you can play along, too! I’m keeping my code at GitHub; the project is called whd-advent, and you can go look at it. You can look at the code, and my notes, and you can even download it and give it a try.

Mind you, I don’t suggest you do that unless you’re really interested in playing with Clojure, because it isn’t much of a game.

Here’s an example of what the game code looks like:

This defines one room in the game world, your “Home Base”. You can go north (:n) to the “Street” (:street); and at the beginning of the game the room contains a sofa, a TV, and a ball. (It happens to be a soccer ball. Go figure.) The room’s description is a given as a vector of strings; note the bit that says [:tv :broken]. That’s a conditional part of the description; it’s included only if it is a fact that the TV is broken. Once you fix the TV, it will go away.

Note also the definition of the sofa. There’s some keys in the sofa; I imagine they are under a cushion, and will need to be searched for. Baby steps.


Browse Our Archives

Follow Us!


TAKE THE
Religious Wisdom Quiz

How do you show your love for God?

Select your answer to see how you score.