Special

Introducing the “Welcome to Xojo” Bundle!

New to Xojo and looking for guidance? We've put together a terrific bundle to welcome you! Xojo Bundle

This bundle includes six back issues of the magazine -- all of year 21 in printed book and digital formats -- plus a one-year subscription (beginning with 22.1) so you'll be learning all about Xojo for the next year. It's the perfect way to get started programming with Xojo. And you save as much as $35 over the non-bundle price!

This offer is only available for a limited time as supplies are limited, so hurry today and order this special bundle before the offer goes away!

Article Preview


Buy Now

Issue 4.1

COLUMN

Adventure Games

A look at a simple state machine

Issue: 4.1 (September/October 2005)
Author: Thomas Reed
Author Bio: Thomas Reed has been programming as a hobbyist for more than 20 years, and fell in love with the Mac in 1984.
Article Description: No description available.
Article Length (in bytes): 9,918
Starting Page Number: 34
Article Number: 4116
Related Link(s): None

Excerpt of article text...

When I was a kid, I learned the basics of BASIC (forgive the unintentional pun). However, being primarily self-taught, I knew nothing that my BASIC language book could not teach me, which was nothing more than syntax. I possessed no understanding of such things as data structures and algorithms. I suppose I did know about arrays, but I had yet to figure out exactly what they were good for. When I imagined such advanced concepts as artificial intelligence, all I could envision was a huge conglomeration of loops and if-then statements, hard-coding every possible response to every possible stimulus. Ahh, the ignorance of youth!

It was they heyday of the Commodore-64 and the text-based adventure games made popular by Infocom. Since I had always been very interested in Dungeons & Dragons, the idea of creating my own adventure game fascinated me. However, my first attempts were cumbersome to write and play, since every room had its own convoluted if-then-else statement designed to handle all allowed user inputs. As you might imagine, my first games never got larger than maybe a dozen rooms with only a handful of things you could interact with, in only very limited ways. Not much fun.

One day, while browsing in a bookstore, I found a book that would introduce me to a side of programming I didn't even know existed. It was called "Creating Adventure Games on Your Computer," by Tim Hartnell. This book introduced me to a very simplified form of the finite state machine, although I did not know enough to call it that at the time. From that point on, my understanding of computers took an exponential leap.

I am going to have to face the possibility that some of you may be young enough to have missed the text adventure craze, or perhaps less of a geek than I, and may therefore be unfamiliar with them. For those of you in this category, a text adventure game is one in which there are no graphics whatsoever -- not even rudimentary ASCII-art graphics. The player reads a description of the environment his or her character is in, and types commands to interact with that environment. For example, one might read "It is a dark and stormy night. You are standing at the bottom of a flight of stairs, in the foyer of an abandoned house. A door exits to the south, and a dark hallway leads off to the west." The player interacts with the game by typing commands like "go up stairs", "pick up rock" or even "tickle dragon's tummy." In response to the user's command, some aspect of the game environment may change, and this may in turn open up the possibility to execute different commands.

This is practically the definition of the finite state machine. Such an algorithm has a state, and inputs can cause a state transition (in other words, change the state). How the state changes depends on the current state. For example, in an adventure game, the command "go north" may invoke a state transition if the player's character is standing in an open field. At the top of a north-facing cliff, on the other hand, the same input probably won't trigger a state change. (Or it may transition to a "suicide" state, depending on how depraved the programmer is!)

Without further ado, let's look at the fairly limited implementation of a finite state machine introduced to me by Hartnell's book. Any text adventure game is essentially just a series of rooms, each with certain contents and features, joined together by doors, ladders, stairs, magic portals, trap doors or even a giant bird that carries you off to its nest. Each of these represents a state change, from one state (aka, room) to another.

...End of Excerpt. Please purchase the magazine to read the full article.