Patheos

BuddhistCatholicContemplativeEvangelicalGeneral ChristianHinduJewishLatter-day SaintMuslimNew VisionsProgressive Christian

Beliefs

Topics

Columnists

Resource Library

Research Tools

Video

Podcasts

Thought Readers

Faith In Media Tracker

Newsletters

Ad-Free & Subscriber Log-In Subscription Support

Patheos

Ad-Free & Subscriber Log-In Subscription Support

Follow Patheos Patheos on Facebook Patheos on YouTube

Cry 'Woof'

Adventures in Clojure: The World is Mine!

Last updated on: July 12, 2014 at 3:15 am   by willduquette

Adventures in Clojure: The World is Mine! 2014-07-10T21:16:00-05:00 willduquette

LOGO Patheos Explore the world's faith through different perspectives on religion and spirituality! Patheos has the views of the prevalent religions and spiritualities of the world.

So I’m, in a rather desultory fashion, putting together a simple text adventure in the Clojure programming language. (You can find the code at GitHub, should you be interested.)

The first thing you need in a text adventure game is a world to move around in. The world usually consists of a collection of rooms. Each room can have any number of links to other rooms; each link is in a particular direction. Directions include the four cardinal directions (north, south, east, and west) and their quarters (northeast, northwest, southeast, southwest); up and down, and sometimes in and out. Mathematically speaking, what we have is a directed graph, with rooms for the nodes and links between rooms as the edges.

A room has quite a bit of data associated with it. A room will have at least the following:

In addition, a room is usually a container for objects. It may contain some objects as the game starts, and the player can usually drop objects and leave them behind.

In Clojure, it’s common to represent objects like rooms as maps. A map is a table of keys and values: given the key, you can retrieve the value. (It’s called a “map” because it’s a mapping from keys to values.) The keys and values can be any kind of Clojure data value (including quite complicated ones), but when you use a map to represent an object the keys are usually Clojure keyword symbols.

Here’s an example of a room object:

{:name        "Home Base"
 :description "Your apartment.  Looks kind of scruffy."
 :links       {:out   :street
               :south :bedroom
               :east  :kitchen}}

The words beginning a colon (e.g., :name) are keyword symbols. I’ve specified the room’s name, description and links, all in one concise data value.

Note that the value associated with the key :links is another map: a map from directions to room IDs.

Room IDs? Where did that come from? What is this room ID of which you speak?

In a language like C or Java or Tcl it’s common to keep object records in a collection of some kind, and look them up as needed given an ID. In C it would often be a pointer; in Tcl it would be a string. In Clojure, you might use a keyword; and that’s what I’m doing. I’m going to assign each room a unique ID, a keyword, and use that to refer to the room. When I want to pass a room to a function, I’ll pass the keyword and let the function look up the room’s data.

In short, I’ll quite literally have a map of rooms:

(def rooms {
   :home    {...}
   :bedroom {...}
   :kitchen {...}
   :street  {...}})

This is somewhat perverse in Clojure code; I understand that the done thing is to pass the room’s full map to functions, and even use it as a key in other maps. I’m not used to thinking that way, but I didn’t choose this
structure just because it’s familiar.

The problem with the map of rooms is that it’s a cyclic graph. That means that if you want to define the links between rooms as you define the rooms, you need to be able to link to rooms that haven’t been defined yet…and that means using some kind of indirection.

To put it another way: when I define the “Home Base” room, I haven’t yet defined the :street room; but I know that its ID is going to be :street so I’ll use that in the link. I’ll get around to defining the details of the :street room later on in the code.

Woof Guide to Technology

clojure

text adventure

Previous Post



July 11, 2014

Bertie Wooster Sings the Blues!

Next Post



July 13, 2014 Lumen Fidei: The Crystallized Love of God


Browse Our Archives

Select a Category Admin App o' the Morning Ask the Dogmat Believing Thomas Bits of Fiction Blasts from the Past Bootstrapping the Interior Life Catholic Mom Christian Living Deep Thoughts from the Doghouse Dominicana Evangelii Gaudium Interviews It Was A Dark and Stormy Night Links to This and That Lumen Fidei Meme Watch On Writing Operation Gratitude Pedant Errant Photography Quill Random Barkings S'Mary's World Shakers of Salt Software Development The Dog Whistler The Dogmat Sez Third Orders Thomas Aquinas Uncategorized Vikings at Dinos Woof Guide to Cinema Woof Guide to History Woof Guide to Literature Woof Guide to Obscure Catholic Music Woof Guide to Philosophy Woof Guide to Popular Music Woof Guide to Technology Woof Guide to Theology Words I Wish I'd Written Works For Me

Select a Month September 2016 August 2016 June 2016 May 2016 April 2016 March 2016 February 2016 January 2016 December 2015 November 2015 October 2015 September 2015 August 2015 July 2015 June 2015 May 2015 April 2015 March 2015 February 2015 January 2015 December 2014 November 2014 October 2014 September 2014 August 2014 July 2014 June 2014 May 2014 April 2014 March 2014 February 2014 January 2014 December 2013 November 2013 October 2013


Follow Us!

Follow Us on Facebook Follow Us on Twitter



TAKE THE

Religious Wisdom Quiz

What Christian tradition is noted for its belief in nonresistance and simple living?

Select your answer to see how you score.

Anglican Episcopalian Methodist Amish

Related posts from Cry 'Woof'

Catholic

Second Look: Amazon Echo after One Week

Cry 'Woof'

Catholic

First Look: Amazon Echo, or Alexa Tells You...

Cry 'Woof'

Catholic

Kindle and the Calibre App

Cry 'Woof'

Catholic

Book Reader Notes: Kindle vs. iBooks vs. Google...

Cry 'Woof'

TRENDING AT PATHEOS Catholic

Choosing Virtue Over Luxury And Self-Interest

St. Basil the Great understood two different ways we can find our...

Why AI Needs Limits: Lessons From The Sorcerer’s...

The story of the Sorcerer’s Apprentice is famous in the modern age...

Faith, Reason, and Transubstantiation

“It would be easier for the world to survive without the sun...

If Baptism Washes Away Sin, Why Do Catholics...

The seven sacraments stand as the foundation of Catholic ecclesial life. The...

The Power of Setting a Good Example

Remember last August when the Chicago White Sox hosted a "Pope Hat...

How Grandma Tourism Helps Grandparents Live Their Vocation

Grandma, The New Travel Influencer What started as a nostalgia-inspired social media...

view all Catholic blogs

Patheos on Facebook Patheos on YouTube

Ad-Free & Subscriber Log-In Subscription Support

Copyright 2008-2026, Patheos, part of the Radiant Digital network of media brands. All rights reserved.

Subscription Support Ad-Free & Subscriber Log-In

Follow Patheos Patheos on Facebook Patheos on YouTube