Description
Write a simple adventure game program with a text interface. Part 1 is more of a game skeleton than an actual game, but we will be adding to the game in several later assignments.
Specification
The main class for your program should be called Adventure. When the program starts it prints a prompt for a command. It then reads a line of input, parses the input into a command and parameters, and then executes the command. The command and its parameters (if it has any parameters) should be in the same line of input. If the command or parameters are incorrect the program should print an error message. The program should continue to print a prompt for a command, get input, and execute the input until the user enters the quit command.
Commands are not case sensitive. The user can enter go, GO, Go, or gO for the go command.
Here are the commands that your program should accept and execute:
- go
- Move the character to a new map location. After the go command, and on the same line, will be a parameter of north, south, east, or west.
- inventory
- List the items that the character is carrying. For this assignment, the items will not change and will always be:
brass lantern rope rations staff
- quit
- Exit the program.
Commands are recognized by the first letter, so any word that starts with the letter g (upper or lower case) should be interpreted as the go command.
Your program should keep track of the player character’s location and print the location after each valid command. The location is a pair of coordinates, with the first coordinate telling what row of the map the character is in and the second telling what column the character is in.
The character starts in location 0,0, which is at the top left corner of the map. The go command will change the character’s location as follows:
east | Add one to the column (second coordinate) |
west | Subtract one from the column (second coordinate) |
north | Subtract one from the row (first coordinate) |
south | Add one to the row (first coordinate) |
For this assignment we are using a 5×5 map that has five rows and five columns. The row and column coordinates must not be negative, and must not have a value greater than four. Your program should not allow a command that moves the character to a location outside these bounds, and it should print a message if the user enters a command that would take the character out of bounds.
You will probably only need one class for this assignment. Your class should not be part of a package. Some Java IDEs will automatically create a package for each new project, so if yours does that you will need to remove the package declarations.
Sample
Here is a sample class file that you can try to see what the program does: Adventure.class
Here is a sample transcript: transcript.txt (
Even though this is a simple program, think about how you can write your code to make it easy to modify later. We will have several assignments that modify and build on the Adventure class.
Hints
The easiest way to do input is to use java.util.Scanner. You should not make more than one scanner for a stream, and since we are using only one stream (the standard input stream) you should not make more than one scanner in your program. I will deduct points from your score on this assignment if your program has more than one scanner on the same input stream.
A good way to break up an input line into words is to use the String.split method. (The String class is part of the java.lang package.)
You will find a lot of useful information for this assignment in Chapters 2 and 3 of the book, including help with compiling and running Java programs, using the Scanner class, and working with strings and arrays.
Here are some additional notes to help you get started on this assignment: notes
Turn in
Make sure that your program compiles and runs from the command line:
javac Adventure.java
java Adventure
You won’t get credit for your program if your program doesn’t compile from the command line or can’t be run from the command line.
Put your source files into a zip file and turn the zip file in on Canvas. Your files should not be inside a folder in the zip file, and there should not be any files other than source files and an optional README.txt file.
Grading
10 Inventory command prints inventory
10 Commands can be abbreviated
10 Commands are not case sensitive
10 Location is printed after each command
10 Location is correct
10 Location is not out of bounds, and the program prints a message if a go command would take the character out of bounds
20 All four Go parameters work
10 Prints error message for invalid commands and parameters
10 Quit command exits program
100 TOTAL