Chapter 5. Building a Text Game Engine

When you write a program, no matter which programming language you’re using or what your program does, it will probably need to work with text. Sure, one day we may all have Ethernet ports at the base of our skulls (100Mbps Ethernet will have been fully adopted by then, of course). But until the day arrives when you can just exchange thoughts with your MacBook using a direct hookup, you’ll be stuck using alphabetic text for input and output in your software.

Computers have always had a bit of a tenuous relationship with text. Although we tend to think of text processing as a central task for computer hardware and software (indeed, the 8-bit byte is the standard design element in modern computers, in large part, due to how well suited it is for encoding Western character sets), the truth of the matter is that the human concept of text is really alien to a computer.

In this chapter, you’ll learn how to use Lisp to manipulate text. You’ll see, once again, that the Lispy approach to solving problems allows you to create code that is full of elegance and symmetry. To demonstrate this approach, we will do something that would seem to make thinking with text unavoidable: build the engine for a simple text adventure game. However, we’ll do this in a way that avoids constraining our code by artificially forcing the human notion of text into its design. This will allow us to write code that focuses on the strengths of a computer.

As you read this chapter, remember that handling text is not a computer’s strength. It is a necessary evil best kept to a minimum.

The Wizard's Adventure Game

In this game, you are a wizard’s apprentice. You’ll explore the wizard’s house. When we complete the game (in Chapter 17), you’ll be able to solve puzzles and win a magical donut.

Our Game World

Here is a picture of our game world:

image with no caption

As you can see, we can visit three different locations: a living room, an attic, and a garden. Players can move between places using the door and the ladder to the attic.

Think of this game world as a simple directed graph with three nodes (represented as ellipses) and four edges (represented as arrows):

image with no caption

Players move between nodes by traveling along the edges in either direction. Wherever the players are, they can interact with various objects around them.

image with no caption

Basic Requirements

Our game code will need to handle a few basic things:

  • Looking around

  • Walking to different locations

  • Picking up objects

  • Performing actions on the objects picked up

In this chapter, we’ll address the first three of these requirements. To perform more complex actions on objects, we’ll use the more advanced Lisp techniques covered in later chapters. Because of this, our game engine will be somewhat limited in its abilities until we finish it in Chapter 17.

When looking around in our game world, you will be able to “see” three kinds of things from any location:

  • Basic scenery

  • One or more paths to other locations

  • Objects that you can pick up and manipulate

Let’s add features for these one at a time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.93.141