ICS 3C/U Text Adventure Game

Handling text commands:

In your game, the user will be typing in words. Your program will have to recognize these words and then do the correct action. These actions will be in the form of methods.

There are a couple of ways to do this. I’ll show you one way, if you know another please let me know.

Here’s how it will work:


Of course, your main loop should have ways to exit.

I can think of 3 ways: the player wins, the player loses/dies, the player quits.

The winning condition may be one of the last things you code.

Big Picture

n  Decide if you want to do this project by yourself or with someone else.

n  Decide what type of game it will be (solving problems like in Zork, or attacking monsters, buying weapons and armour and leveling up).
What is the general idea of how you’ll play and what you have to do in the game?

Why not have a look at Colossal Cave, Zork and Nethack – three of the most famous text adventure games. (Hitchhiker’s Guide to the Galaxy is also good, but too hard.)
Nethack is way too complicated for us and isn’t really text adventure – it’s “Roguelike”

Parsing text commands

Planning

·  Make a list of all of the possible commands that can be entered into your game

·  How many words is the longest command? Hopefully not more than 3.

·  The first thing that we’ll do is make it all lowercase.

·  Ignore the word “the”. The second thing we’ll do is get rid of all articles (the, a, an) so that “Take the sandwich” becomes “take sandwich”

·  There may be a few special commands that have a lot of words

·  When we start using prepositions, it gets a lot more complicated. I don’t know how adventure games actually do this. Examples:
Take the candle from the candle holder
Put the coin into the slot. Place the lens on top of the case.

·  Do you want to use “take hammer” as well as “pick up hammer”?

·  All of your “open” commands could be the same pattern which would make it simpler:
”open door” or “open A with B”

·  Similarly with others “put A into B”. “hit A with B”. (what about “place” or “smash” ??)

·  You might get around some problems by using the word “use” for everything. Or maybe not.

·  How will you handle directions? N, North …
(I typically just use N,S,E,W, as well as U, D for up and down).

Programming

Ø  Write the main loop part of the program.

o  So you need a scanner,

o  You need to separate the first 3 words from the input string.

o  Here is how you would do part of the stuff in the main loop
(I’ll assume that you have stored the first three words in the strings “word1” “word2” and “word3”)

switch(word1) {

case "i":

case "inventory":

//displayInventory();

System.out.println("display inventory");

break;

case "eat":

//eatItem(word2);

System.out.println("eat " + word2);

break;

default:

System.out.println("invalid command");

}

What happens here is:

-  SWITCH is the same as a series of if statements based on "word1" .

-  if the first word typed is “i” or “inventory”, you’ll run the display inventory program.

-  if the first word is “eat” you’ll run the eat program with word2

-  anything else is an invalid command

Ø  Think of the other commands that you will use in your game and add them in. **Start Simple!**