In this assignment, you will be extending the game you made for the last assignment.
You will extend an existing class hierarchy to add additional functionality.
You will gain experience with exceptions.
You will read from and write to files.
Dungeon 2.
This is the exciting sequel to your previous assignment. Dungeon 2 adds these new features:
Higher resolution graphics.
Items that are reuseable, in particular pickaxes.
Rocks that block the hero, but can be broken through using a pickaxe.
Hero status display showing number of items collected and elapsed time.
Game can actually be won by collecting all the gems.
A high score list that tracks the fastest heros.
Getting started.
Download the file dungeon2.zip.
This contains the assets as well as some provided classes and some stubbed out classes.
NOTE: You will use your Cell.java, CellPassage.java, CellDoor.java, Hero.java and Dungeon.java classes from the last assignment as a starting point.
So copy these files into your new Dungeon 2 project directory.
If there are problems with your previous submission, we will help you fix it up.
Dungeon definition.
The dungeon definition is similar to the previous assignment, but there are now breakable rocks in the dungeon denoted by an 'R'.
Also the second line now contains the number of gems, keys, and pickaxes to randomly place in the dungeon.
As such, the files from your last dungeon assignment won't work with the new DungeonGame2 main program.
Here is what d4.txt looks like:
13 10
10 4 2
1 1
W W W W W W W W W W W W W
W - W - - - - - - D - - W
W - R - W W W - W W - - W
W - W - S - S - W W - - W
W - R - W W W - W W W O W
W - W - W W W D W W - - W
W - W - W - - - - D - - W
W W W - W - W - - W - - W
W - - - W - W - - W - - W
W W W W W W W W W W W W W
Updated Config. The provided Config class has been changed in the following ways:
Uses a new set of image assets.
Increases the cell size to 64 pixels.
Adds a quit action so players can hit 'q' to end the game.
Adds the method rockImages to provide the progression of images for the rock.
Adds the method itemWasConsumed to test whether using an item has caused it to be consumed (e.g. a key opening a door, a pickaxe that has broken).
Adds the method mapItemToConsumedSound to provide sounds effect when an item is consumed.
Part 0: Dungeon creation using file input. We have provided a partially completed DungeonFactory class.
This class provides static methods that handle all the work associated with instantiating a Dungeon object.
The code that used to live in the main game program to read in a file should now be all tucked away in this factory class.
You are to implement the createFromFile method.
This method should construct a Dungeon using file input via a Scanner object.
Once complete, you can test it by running DungeonGame2 running it with a single command line argument:
% java DungeonGame2 d3.txt
Part 1: Changes to APIs.
We need a new method that allows us to ask any object of type Cell (or its subclasses) if it currently contains a given item.
Add the method containsItem to the Cell API.
This method takes an Item as input and returns true if the cell contains that type of item, false otherwise.
Hint: You will have to implement the method in one class as well as override it in another class.
Using the new containsItem method, implement a new method in Dungeon named getCount.
This method takes an Item as input and returns the integer count of the number of cells in the dungeon that have that item.
This is used by DungeonGame2 to determine when a player has cleared all the gems from the dungeon.
It should also work for counting the number of other objects (e.g. keys or pickaxes) in the dungeon.
The Dungeon API needs two new methods: getWidth and getHeight.
These methods return integer values specifying how many cells wide and hight the dungeon is.
This is required by DungeonGame2 in order to setup its drawing window.
The Dungeon API also now needs a method getElapsedTime that returns a floating-point value giving the elapsed time of the game in seconds.
You may want to use the provided Stats class to help you do this.
This is used by DungeonGame2 to add a new entry to the high score list.
At this point, your program should compile.
You should be able to run it on a dungeon without rocks by reading from standard input:
% java DungeonGame2 d3.txt
If you implemented containsItem and getCount correctly, you should be able to win the game by collecting all the gems.
If last time you implemented collectItem in Hero in a general manner, you should notice that collecting a pickaxe results in a new sound effect.
If not, you'll need to add logic to the Hero class in the next part to handle pickaxes.
Part 2: Breakable rocks and hero status.
To support the new breakable rock feature, we have added a new PICKAXE value to the Item enumeration.
We have also added rockImages to Config that returns an array of image filenames.
The first image in the array is the starting point for a rock.
Each time a rock is pickaxed, it goes to the next image and the use item sound effect is played.
Every time the pickaxe is used, there is a 5% chance that the axe breaks.
If the axe breaks, the item consumed sound effect is played.
Once it reaches the final image, the passage way is clear and the hero is thereafter allowed to walk through that cell.
We have given you only a basic stub for CellRock.
A variety of changes need to happen to realize your rock-breaking dream:
Decide what class CellRock should extend.
Add instance variable(s) to CellRock to allow objects of this type to track the set of images as well as the current state of the rock.
Override the methods attemptToMove and draw in CellRock to make CellRock behave as described.
Modify set in Dungeon to allow creation of CellRock objects.
Modify collectItem in Hero to keep track of the number of pickaxes the hero has (this might not be necessary if you implement this last time in a nice general way).
Modify attemptToUse in Hero to deal with objects like the pickaxe that may or may not be consumed during use.
To help with this use the itemWasConsumed method in Config.
You will also need to use the mapItemToConsumeSound which plays a sound when an item is used up (e.g. when a key opens a door, or when the pickaxe breaks).
You should play the use item sound effect first, and then play the consume sound effect if an item was actually used up.
You can test out your progress by running on a dungeon that includes rocks:
% java DungeonGame2 d4.txt
You should also add a status line at the top of the screen that shows the number of collected gems, keys, pickaxes, and elapsed time (rounded to two decimal places). The exact position, color, font size, and justification is up to you, just be sure that it is readable and doesn't obstruct a player's view. You can assume the top row of the dungeon will be a wall as in d4.txt.
For example: Gems: 5 Keys: 2 Pickaxes: 0 Time: 72.52Part 3: High scores.
Any good video game needs a high score screen.
You need to complete the class HighScores that tracks the name and times of the fastest players.
The HighScores class has a specified maximum size.
Once the high score list is full, a new winner must have a time strictly less than one of the current entries in the list.
The list is kept in order from fastest to slowest.
The HighScores class is given a filename that is used to load the previous best players.
If the given filename does not exist, it initializes an empty high score list.
The filename has a line for each entry, starting with the player's name followed by whitespace and then the player's elapsed time to clear the dungeon. Refer to the lecture on file I/O. This topic will be presented in class Friday 2/17.
We have provided a test main program.
Assuming there is no scores.txt file to begin with, here is the output of two consecutive runs:
% java HighScores scores.txt 10% more scores.txt
Keith 4.99
Bob 5.0
George 5.0
Hank 6.6
Abe 10.0
Carol 15.5
Edith 17.2
Dave 23.7
Lisa 23.7
Frank 23.9
% java HighScores scores.txt 10% more scores.txt
Keith 4.99
Keith 4.99
Bob 5.0
George 5.0
Bob 5.0
George 5.0
Hank 6.6
Hank 6.6
Abe 10.0
Abe 10.0
Once you have implemented HighScores, you can test it out by giving two additional command line arguments to DungeonGame2 specifying the high score filename and the player's name.
% java DungeonGame2 d4.txt scores.txt Speedy
The game displays the high scores before the start of the game.
Press spacebar to start the game.
You can finish the game by either collecting all the gems or by hitting the 'q' key.
Pressing the spacebar again will show you the high score list again.
If you placed into the list, your new entry will be highlighted in red.
Grading
Grade Item
Points Possible
Points Earned
Program Compiles
1
Program Runs
1
javadoc Comments on All Classes and Methods
4
Part 1: API Changes
6
Part 2: Breakable Rocks
6
Part 3: Hero Status Line
6
Part 4: High Scores
6
Total
30
Where did you get the graphics?
For this assignment, Keith started with this 128 x 128 tile set from the Slash'EM game.
Extra credit.
Make the game better.
You might want to use some of the extra images provided in dungeon2-extra-assets.zip.
Or you could also cut them out of the extremely large (20MB) image linked above.
Please see the note below on how to submit your extra-credit creation.
Be sure to submit your regular submission first and ensure it works per the assignment specifications.
Submission.
Submit all the classes you changed: Cell.java, CellPassage.java, CellRock.java, Dungeon.java, Hero.java, DungeonFactory.java and HighScores.java.
Be sure each submitted source file has the required header with your name and a description of the program and all your classes and methods use javadoc comments.
For extra-credit, please submit a single zip file containing all your programs/graphics/sounds/configuration files via the special extra-credit drop box.