A text adventure
In this project you build a text adventure: a game that describes where you are, reads what you want to do (go north, take key), and answers in text. The world isn't written into the program. It comes from a file, world.txt, so the same program can play any world that file describes.
When it's finished, this is a whole world: an old lighthouse with five rooms, two things to find, a locked door and a goal.
room;Shore;Cold waves break on gray pebbles.
room;Cottage;A keeper's cottage. The stove is cold.
room;Boathouse;An old rowboat lies upside down. It smells of tar.
room;Tower;Stone stairs climb into the dark.
room;Lamp Room;A great glass lens waits for a flame.
exit;Shore;north;Cottage
exit;Shore;east;Boathouse
exit;Cottage;south;Shore
exit;Cottage;east;Tower
exit;Boathouse;west;Shore
exit;Tower;west;Cottage
exit;Tower;up;Lamp Room
exit;Lamp Room;down;Tower
item;oil;Cottage
item;key;Boathouse
lock;Cottage;east;key
goal;Lamp Room;oil;You fill the lamp with oil and light it. Its beam sweeps across the sea.
A player who types these commands, one per line:
go north
take oil
go east
go south
go east
take key
go west
go north
use key
go east
go up
sees this:
[Shore]
Cold waves break on gray pebbles.
Exits: north, east
[Cottage]
A keeper's cottage. The stove is cold.
Exits: south, east
You see: oil
You take the oil.
The way east is locked.
[Shore]
Cold waves break on gray pebbles.
Exits: north, east
[Boathouse]
An old rowboat lies upside down. It smells of tar.
Exits: west
You see: key
You take the key.
[Shore]
Cold waves break on gray pebbles.
Exits: north, east
[Cottage]
A keeper's cottage. The stove is cold.
Exits: south, east
You unlock the way east with the key.
[Tower]
Stone stairs climb into the dark.
Exits: west, up
[Lamp Room]
A great glass lens waits for a flame.
Exits: down
You fill the lamp with oil and light it. Its beam sweeps across the sea.
You won in 7 moves.
What you need
Modules 16 to 24, especially: string methods such as split, startsWith and substring (module 16), classes with constructors, getters and toString (modules 17 and 18), lists of objects (module 19), reading a file with a Scanner, try and catch (module 20), objects as method parameters and as instance variables, like the player's Room and addExit(Exit exit) (module 23), and a list as an instance variable created in the constructor, with methods that add to it, go through it, empty it and remove from it (module 24). The last milestone also writes a file, with a class called PrintWriter; its task shows you how.
How you build it
The program has a class for each kind of thing in the game: Room, Exit and Player, with Main reading the world and the commands. Each line of world.txt starts with its kind (room, exit, item, lock, goal), and each milestone teaches the program one more kind, together with the commands that go with it:
- Rooms and commands: rooms from the file, and a loop that reads commands.
- Exits and moves:
go northtakes you to the next room. - Things to carry:
take,dropandinventory. - A locked door: an exit that opens with the right item, and
use. - A goal: the game ends when you reach it, and counts your moves.
- Saving and loading:
savewrites the game to a file, andloadreads it back.
Lines of a kind the program doesn't know yet are skipped, so a world file written for a later milestone still works with an earlier program.
Milestones
- Rooms and commands
- Exits and moves
- Things to carry
- A locked door
- A goal
- Saving and loading