text adventure game

I’m working on a text adventure game. I have a general idea for the design, well maybe more than just that, but I’m not sure how to put it all together.
Here’s what I have:
Game class (starts the game)
Character class (has inventory, name, and location)
Inventory class (has list of Items, add/remove functionality)
Item class(es) (has name and bool isUseable)
Room class (has description of room, room variables like locked doors or lamps, items that you can pick up)
RoomVariable class(es) (has objects in the room that can change state like a lamp or a chest)

that’s what i have so far, i’m not sure how to put it all together… any suggestions? thanks a lot

seems like a solid start :smiley:
one suggestion:

instead of having a RoomVariable class, let each room have a list of items. Those are the items inside that room. Each of those items have a use() method. An item of class “Lamp” would illuminate the room. An item of class “Sword” can be picked up, and you can swing it around.

Good luck. You still have a looong way to go!

Ok, that is a big help. I can just have different types of items. But now I’m wondering about a solid location system. I had an idea to make a Location class that can hold x, y, and z coordinates in a 3D map, and the character has methods to modify or retrieve that location. Or would it be better to just store x, y, and z in the character class?

Well this begins to sound more like a MUD thena text adventure.

Because TextAdvetures are only single player they typically dont have ANY objects like this. What they have is a finite state machine where each state canhave an invetory attached and a parser for generating events that mvoe you from one state to the next.

In a regular text adventure, I would store a list of the characters in the location, just like the items. Only the player would be seperate, and I would just store a reference to the Location object he’s in, not 3D coordinates.

If the NPCs are moving from area to area, I still wouldn’t use coordinates. The only way I would use coordinates is if there were some sort of pathfinding to find a path between different areas. That’s a little bit different from a regular text adventure, but it might be fun. Then again, if you’re going to do that, you might as well add graphics or at least make it a Rogue-like game.

Well I want to take the least complicated approach right now. I have been working on this game (on and off) for over a year and I would like to finish it so that I can move on with my life and work on other (more useful) projects.
I don’t NEED a Location object, and obviously coordinates aren’t the right choice for me. I just want a simple text adventure, no battle system, just a small to medium sized quest in which there is one playable character, no NPCs, and you go find items, use them on stuff, and eventually finish the quest and find the end or the last treasure or something stupid.
So, I think I’ll run with the idea to store the character in the Room location, but then I’m not sure how to change the location… each room object knows where its exits are, so I could check say a moveNorth() command on the player, but then what might be a good approach to store the map? maybe the character can have some kind of list of rooms, so if he can moveNorth() then you know which room to move him to. or could there be a Map class? I’ll put some thought into it before posting again…

It sounds like each room is a node in a graph. I would just let each node be aware of its neighbors, a la:


enum Direction { N, S, E, W, U, D};
interface Room {
  /* Check if the player can move in the given direction from this room. */
  boolean canMove(Direction d);
  /* Get the next room in the given direction, or null if it is an illegal move. */
  Room next(Direction d);
}

Then your game needs to know the player’s current location (could be a property of the player or of the game, depending on your design), and your interpreter needs to be able to change the location, e.g.



class Interpreter {
  ...
  public void interpret(String cmd) {
    ...
    if (cmd.equals("n"))
      if (player.location().canMove(Direction.N))
        player.moveTo(player.location().next(Direction.N));
     else
        console.write("You cannot go that direction.");
    ...
  } 
}

Clearly, it could be more elegant and I’m waving my hands a bit, but this is the approach I would take.

You might want to take a look at John Estell’s “Nifty” Adventure Game assignment: http://nifty.stanford.edu/2002/EstellNifty2002/pokagon.html. The design isn’t given, but it has some good advice and sample data files. Also, it looks like the solution is constructed in procedural C++, but as Jeff said earlier, text adventures are basically finite state machines, and that’s the same whether you model them procedurally or object-orientedly.

Um and youve been working on this for a year???

No offense but I think maybe you dont really knwo what your doing and vastly over-complciating your problem.

i wrote a basic text adventure engine years (about 25 years) ago (simple scott adams style noun-verb parser) in about a month of free time when I was betwene highschool and college in BASIC ona super-pet. I could write it again probably in a weekend in Java.

The state machine is very simple and straightfoward. The only things that should take any time at all to write are the parser IF you want to do more complex late-infocom style commands and the data for your adventure-- and the vast majority of the work should be in the design and keying of the data.

Jeff, you’re right, I don’t really know what I’m doing. And about having been working on this for a year, I put in a good month about a year ago, I’ve thought about some parts here and there, and then recently I’ve tried to put something together again. I guess it’s more accurate to say that I started this project a year ago.
So I guess I should be asking the question what resources are there for me to learn what I’m doing and get a better idea how to make this type of game? Thanks.

No offense either, but why don’t you post something useful, instead ?

Google came up with these:
http://www.ifwiki.org/index.php/FAQ#How_can_I_write_my_own_game.3F
http://en.wikipedia.org/wiki/TADS
http://www.xyzzynews.com/xyzzy.1e.html

I did, above. To quote:

“Because TextAdvetures are only single player they typically dont have ANY objects like this. What they have is a finite state machine where each state can have an invetory attached and a parser for generating events that move you from one state to the next.”

If you know what a state machine is, implemntation should be obvious from that statement.

If you don’t then say so, and Ill explain further.

I think I do know, but I can’t apply it to text adventures, and from what little I managed to google, I couldn’t find a nice recipe. So yes, I’d be very interested!

Okay, really simply.

A Finite State Automata (FSA), also called a “state machine” is a machine that exists in one and only one state at a time. It takes as input an event. An event causes a transition to a new state. Part of that transition may be to generate output.

FSA transitions can be described by a 2D array where the X coordinate is the state, the Y coordinate is the event and contents of the cell is the transition which would be a new X coordinate to change to plus output or other action information. This array is often called a “transition table”

Lets imagine for a moment that you have a world of two rooms. NorthRoom and SouthRoom and 2 events-- “Go North” and “Go South”.

Your state transition table might look like this:

States:
0 - North Room
1 - South Room

Events
0 - go north
1 - go south

Table:

[tr][td] [/td][td] Room 0 [/td][td] Room 1 [/td][/tr]
[tr][td] Event 0 [/td][td][/td][td]set state to room 0[/td][/tr]
[tr][td] Event 1 [/td][td]set state to room 1[/td][td][/td][/tr]

Now this by itself is not enough to be the adventure game ebcause there is no roomdescription yet. No output.
Typically, an assumed part of every transition in sucha game is “print descritpion of new state.” You keep that in another table like this:

[tr][td] Room Number [/td][td] Room Description [/td][/tr]
[tr][td] Room 0 [/td][td]A big spacious cavern with a door to the south[/td][/tr]
[tr][td] Room 1 [/td][td]A tiny little hole with a door to the north.[/td][/tr]

Thats all the basics. Lets stop and make sure you are foillowing that then Ill explain how you add inventory, conditional actions, chnages to rooms and winning/losing.

I’m not exactly understanding the state machine concept, but it does seem a whole lot simpler than trying to build a bunch of classes. Can you explain more or give other resources on it? Thanks.

the STATE of an object is exactly what it sounds like…“What” or “Where”. By Finite State it means that the object can have only a finate number of applicable states…one. For a text adventure game like Jeff’s describing your PLAYER object will have a STATE which will be the location in your game world where the PLAYER currently exists.

The other key part of the FSM is how you get from one state to another ( transition ). in Jeff’s example that transition will be the movement options you give the PLAYER. When you define a location in the game, you will decide how many ways out of the room exist and what locations they lead to. When the player selects one of those options, you change thier STATE to the one representing the new location. This will trigger “something” to happen in your game. At a minimum it will be providing a text output of the location’s description, although it could also be something like “you’re attacked by a flame troll and die a miserable death.”

I have never made an FSM adventure game before ( good tip Jeff! ) but I have studied their use in AI. They are often used for tasks such as determining if your NPC enemies will ATTACK, PATROL, RETREAT, etc. For example, your NPC may be in the PATROL state which means they will follow a predetermined path. If a player attacks the NPC or moves within a certain range of it, the NPC will enter the ATTACK state which changes its movement to pursue the player. If they NPC suffers more than 80% damage they will enter the RETREAT state and move as quickly as possible away from the player.

A quick google of “Finite State Machines +Games” kicked out the typical 50k hit output. However, the first is this:

http://ai-depot.com/FiniteStateMachines/

I only had time to give it a quick scan but it looks like a very good intro to FSMs that gets deeper as the article goes on. And it’s on AI Depot wich is a pretty good reference site which I’ve used in the past. Give it a quick read.

Hope this helps the OP, and I look forward to Jeff’s next installment of managing the inventory in this game model. ;D

I’m with you, and thank you for taking the time to explain it!

The problem I had, was figuring out how entirely unrelated states (the room you’re in is one set of states, your inventory is another, and every NPC’s attitude towards you can be a state machine in itself) can be modeled in a single state machine, without requiring an explosion of states (#rooms * #items * #NPC’s * #attitudes).

Because I guess an explosion of states is also an explosion of the amount of work that needs to be done in the story-telling department (would you need to describe the same room for every possible NCP, even if the room’s description depends on only one NPC?)

One thing I forgot to throw out as a warning…transitions are not automatically reciprocal.

If you have NorthRoom and SouthRoom and set a transition from NorthRoom to SouthRoom, you do NOT automagically get a transition from SouthRoom back into NorthRoom. It’s VERY, VERY important to really understand your layout and how you expect to be able to move around the world and to test each possible transition from every location. Otherwise, Murphy’s Law states that of the one thousand locations in your world, 90% of your players will find themselves going into the one location that you forgot to code an exit transition for. :o

this thread is getting more and more interesting. i was just lurking before, but i want to throw in my request for jeff to continue his example as well. :slight_smile:

I think it’s important to point out that FSA are an idea, a mathematical model. Implementations of FSA can be done in any language, in any paradigm. Adopting an FSA model for your game does not preclude the definition of classes. If you’re using Java (which I assume you are since you’re on this forum), I recommend an OO-approach to FSA.

In fact, if you look at the code outline I supplied earlier, you’ll see that it can be used in an FSA: the state of the game is defined by the location of the player, which is a Room. The set of possible transitions are represented by connectors between rooms and the transitions are implemented through the command interpreter. This is still an FSA, just an indirect implementation.

You could implement an FSA directly, probably with State and Transition classes, but this takes you a level away from your game. The point of OOP (or a point of OOP, if you prefer) is to provide solutions to problems that are close to their own domain. Rather than define a problem in terms of mathematical operations, a problem is defined by objects that represent the elements of the problem “in the real world”. By lifting yourself out to the level of traditional FSA (not FSA-in-games), you may as well just use Pascal. There’s nothing wrong with this, but it is against the grain of Java and OOP.

bahuman makes a great point: if you want to implement pure FSA, then you would by definition need an exponential number of states, since you’re dealing with multiple variables (location, inventory, possibly other things). Hence, I recommend learning about FSA, keeping in mind the lessons of structured programming, but applying modern techniques of OOP and design patterns to your problem.

But hey, I’m on vacation, so I best get out and have some fun… just wanted to share my two cents.

For text adventures you can use this: http://www.inform-fiction.org/I7/Inform%207.html
A remarkable piece of software that has evolved since the time when Jeff wrote his game 25 years ago.

Example: http://www.inform-fiction.org/I7/ex183.html#e183
That’s the actual source code for the game! Cool, huh? :slight_smile: