How to Make a Text Adventure Game

How to make a text adventure game

First make sure that know what a text adventure game is.

There are two basic types:

(i) the classic text adventure like Zork, Colossal Cavern, Hitchikers Guide to the Galaxy
Here you type in commands using words. You’re exploring the world.

(ii) a game that is combat based, where you have certain specific keys to do certain things.

e.g. a=attack, … There can also be stores, etc. You’re not exploring the world.

Some things can be applied to both types, but I’m going to focus on the first type.

Main tasks

1.  parsing

2.  making and connecting rooms

3.  making and placing items

Parsing

-  Get a command from the user.

-  Make it all upper or lower case

-  Remove useless words (the, a, an, )

-  Possibly do other modifications (change “pick up” to “pickup”, “look at” to “lookat”)

-  Split up into words: word1, word2, word3, word4.

-  Types of commands:

·  Directions (1 word, or “go North”)

·  1 word: verb. à do the command associated with this word

·  2 words: verb noun à pass the second word to the command for the verb

·  3 words: verb adjective noun
here the adjective is used if there are more than one of that type of noun in the room or inventory. “read green book” instead of “read black book”
à not sure how to handle this well

·  4 words: verb noun preposition à may be too complex right now.
“put key in lock”
“unlock chest with key”
“hit car with hammer”
“put lantern on table”


Examples of parsing code:

switch(word1) {

//ONE WORD COMMANDS

case "quit":

System.out.print("Do you really want to quit the game? ");

String ans = getCommand().toUpperCase();

if (ans.equals("YES") || ans.equals("Y")) {

System.out.print("Thanks for playing. Bye.");

return false;

}

case "n": case "s": case "w": case "e": case "u": case "d":

case "north": case "south":

case "west": case "east": case "up": case "down":

moveToRoom(word1.charAt(0));

break;

case "i": case "inventory":

showInventory();

break;

case "sleep":

sleep();

break;

case "look":

lookAtRoom(true);

break;

case "help":

printHelp();

break;

//TWO WORD COMMANDS

case "read":

readObject(word2);

break;

case "lookat":

case "examine":

lookAtObject(word2);

break;

case "pickup": case "take":

takeObject(word2);

break;

case "drop":

dropObject(word2);

break;

case "eat":

eatItem(word2);

break;

case "move": //move an item. These are things you can't pick up.

moveItem(word2);

break;

Rooms

If you want to make a grid of rooms, you could do something like this:

//make a Room class:public class Room { boolean isAccessable=false; boolean northExit=false; boolean eastExit=false; boolean southExit=false; boolean westExit=false; String description=null; public ArrayList<String> items = new ArrayList<String>();

static Room[][] roomTable = new Room[ROOM_ROWS][ROOM_COLS];

//Room 1roomTable[0][0]=new Room();roomTable[0][0].description="You're standing next to your tank. The left track is broken.";roomTable[0][0].isAccessable=true;roomTable[0][0].northExit=true;roomTable[0][0].eastExit=true;roomTable[0][0].southExit=true;roomTable[0][0].westExit=true;roomTable[0][0].addItem("Spare Track Section");//roomTable[0][0].addNPC

//Room 2roomTable[0][1]=new Room();roomTable[0][1].description="";//roomTable[0][1].isAccessable=true;roomTable[0][1].northExit=true;roomTable[0][1].eastExit=true;roomTable[0][1].southExit=true;roomTable[0][1].westExit=true;//roomTable[0][1].addItem("");

Items

·  First make a list of most of the items in your game.

·  Then write down what each will do, how you will use it.

·  This will give you an idea of the general properties of the items: food, weapon, something htat can be read, etc.

·  Now we create our item class

class Item {

String name = ""; //this may not be unique, depending ...

String descrRoom = ""; //description of item to meld with room description.

String descrLook = ""; //description of item when you look at it.

String descrRead = ""; //what is displayed when you read the item. If empty, then there is nothing that you can read on it.

int foodpoints = 0; // how much health the food give.

// 0=nonedible

boolean isCarryable = true;

·  And make a constructor … that takes the name and the description as parameters and sets the instance variables

·  To create items, we’ll use a static method and put it in the Item class. It could go anywhere, but it makes sense here.

·  When we make items, we have to put them somewhere. We’re going to place them in the appropriate room. Because we can have more than one item in a room , we’re going to store them in an array list. Each room will have an array list of items called “items”.

-  Arraylists are ordered, but we should not depend on things being in a certain order.

-  They are of variable size, so we can add and remove things from them easily.

-  The only problem is searching through an arraylist to find the item you want.
You will almost always be searching for an item based on its name. If there are two items with the same name, you will always get the first one. E.g. if you have two health potions, one with health=10 and the other with health=20, you’ll get the first one each time.

·  NOTE: if you’re making a store, then you need to have an arraylist of items that the store has as well as their price … Add the price as an instance variable in the item class.


static void setUpItems(ArrayList<Room> roomList) {

Item it = new Item(“sandwich", "a ham sandwich with mustard");

it.descrRoom = "You smell a sandwich nearby.";

it.foodpoints = 10;

roomList.get(1).items.add(it); //add it to room 1.

it = new Item("knife", "a sharp knife with a bone handle");

it.descrRoom = "There is a knife embedded in the tree trunk.";

roomList.get(1).items.add(it);

it = new Item("paper", "A crumpled piece of paper with writing on it.");

it.descrRoom= "Some pieces of paper have blown under a bush.";

it.descrRead = "To leave the planet, fuel the space ship hidden in the cave.";

roomList.get(15).items.add(it);

...

}

There is a problem: we do not want to have to know what number each room is. That’s dumb.

Solution 1: search through the roomlist by name

à You need to know how to search though an arraylist ß

Room tempRoom = null;

for (Room r : roomlist) {

if (r.name.equals(“path”) {

tempRoom = r; //copy the room r to the temproom.

//r will disappear at the end of the loop

break;

}

}

if (tempRoom != null) {

tempRoom.items.add(it);
} else {

System.out.println(“ERROR: There is no room with that name”);

}

//double check this to make sure that it works.

Inventory

An inventory is just an arraylist of items. It could also be an arraylist of strings.

When you pick something up, “take sword”

·  see if the sword is in the current room

·  if there is no sword, then say “there is no sword in this location”

·  else

·  add the sword to the inventory arraylist

·  delete the sword from the itemlist in the current room

When you drop something: “drop sword”

·  see if the sword is in your inventory

·  if so, add the sword to the itemlist in the current room

·  remove the sword from the inventory