Homework J8

Due: Tuesday, 6 Dec 2005 by 10 a.m.



Home | Resources | Homeworks | Slides | Labs | Contacts | Submit | Forums

Purpose

This assignment will give you practice using Vectors and two-dimensional arrays as we finish building our simple text adventure game.  In this assignment you will create a Map class that represents a grid of rooms, each connected to one or more of its neighbors.  The grid will be represented as a two-dimensional array of Room objects; you will use loops to initialize each of the rooms in the array to contain a random description and, with some probability, a random monster and a random weapon.  Constructors for the Map class will allow the user to specify the size of the grid (number of rows & columns), the percentage chance that a given room contains a monster/weapon, etc.  You will also write a method to test whether all monsters have been killed. Finally, you will also implement a createMaze() method that connects the rooms in a random fashion to create a maze, using an algorithm we describe. When finished, you will have created a simple game in which a player wanders through a randomly-generated maze of rooms, finding weapons and fighting monsters.  When all monsters are destroyed, the player wins.

The Map class needs to be in a Map.java file, for which we are providing the skeleton code with a few of the methods already implemented.  Also, you will need the Game.java, which contains the main() method that will run the game.

Components

Of course, we will build on the Creature, Parser, Weapon, Room, Descriptions, and MapPrinter classes that you have implemented over the last few homeworks and labs. To avoid being caught unawares by possible bugs in your code from previous assignments, you are encouraged to use the precompiled versions of these classes, provided here, while coding and debugging your assignment. Right-click on the class name, select "save as...", and save the file. Remember that to use the precompiled .class files, they must be saved to the same directory as the Map.java file that you are creating for this assignment.

We strongly suggest you use the precompiled versions above while coding and debugging this assignment, unless you are very confident that your homework submission was completely correct and bug-free.

Note that when you compile the code, you will receive a warning saying, "Map.java uses unchecked or unsafe operations.  Recompile with -Xlint:unchecked for details."  You can safely ignore this warning (it deals with the fact that the Vector class uses generics, which is something that is not gone over until CS 201).

Documentation for these classes can be found here.

The Supporting Classes

Weapon

We are going to add a text description to the name of a weapon to let the user know how powerful it is.  For example, if you have a katana and you find a pair of nunchucks, how do you know which is better?  Note that for this game, all weapons do the same amount of damage (yes, this is not the most realistic, but much simpler for you to code).  Thus, we will be adding a modifier to help the player determine how powerful of a weapon it is.  The modifiers are: cursed, normal, shiny, high quality, elite, and magical.  This modification need only be made to the getFullName() method in the Weapon class.  The new code for this method is as follows.

public String getFullName() {
	String modifier = "";
	if ( damageModifier < 0 )
		modifier = "cursed";
	else if ( damageModifier < 3 )
		modifier = "normal";
	else if ( damageModifier < 6 )
		modifier = "shiny";
	else if ( damageModifier < 9 )
		modifier = "high quality";
	else if ( damageModifier < 12 )
		modifier = "elite";
	else
		modifier = "magical";
	return modifier + " " + getName();
}

Note that we cannot modify the getName() method, as that is needed elsewhere.  The Weapon class that we provide has this method already implemented.

Room

The Room class is from homework J7, with the extension described here.  The Room class has been extended with two new fields:

and the following methods:

Lastly, when the Room class calls the getName() method from a Weapon object, it should (usually) call getFullName() instead.  The one exception to this is when the user does a 'get' (to pick up a weapon), the Room class needs to check if the player's weapon is "bare hands" (via player.getWeapon().getName().equals("bare hands")).  This should stay as getName().

The Room class that we provide has these methods and fields already implemented.

Game

We provide this class in the Game.java file. As in the previous homework, the Game class provides the main() method for running the game program.  Unlike the previous version, the Game class provided for this homework uses your Map class to set up the rooms, creatures, and weapons. Once this is set up, the game creates the Creature object that represents the player, prints out an introduction, and enters a loop that repeatedly calls the enterRoom() method of the Room object in which the player is located until the player dies, quits, or kills all of the monsters in the game. 

You do not need to modify or submit Game.java.

Map Class

Instance Variables

Your Map class should use the following instance variables:

Provided Methods

We provide two methods for you, as if they are coded incorrectly, the rest of your program will have problems working.

We also provide the skeleton code for isValidRoom() and getRoom(), but this method must be completed.  It is included so that the Map.java file will compile properly.

Shorter Methods

Your Map class should provide the following methods:

Harder Methods

The following methods are more complex and are explained in greater detail:

Advice

Use the connectAllRooms() method first, since it is much simpler than the connectMaze() method.  You can test the generation of monsters, weapons, random room descriptions, etc. on the resulting fully-connected Map, and move on to implementing the connectMaze() method once you are confident that the rest of the code is correct.

You will find lab 11 useful in debugging this homework, especially the generateMaze() method, since the lab develops a class to print out the rooms in a Map along with their connectivity. If you are having trouble debugging the generateMaze() method, you may wish to print out the map at every step of the maze algorithm -- this will help you track what the code is doing, step by step. After you print the maze each time, a call to stdin.nextLine() will pause the program until you enter a string (or just hit <return> to enter an empty string), allowing you to inspect the progress of the algorithm and then hit <return> to see the next step of the algorithm.  This technique of stepping through a program is a very useful and general approach to debugging!

Submission

When you are finished, submit just your Map.java file. We will test it with the precompiled .class files that we provide above, so even if you have used your own versions of those classes, be sure to test your Map code with the precompiled classes as well!