CS 50 Homework 9 (Section 1 — Prof. Marshall)

Due by class time Tuesday, April 9

In this assignment, you will modify and extend the code for the Pomona Adventure Game discussed in lecture. Use the code in the CS 50 class folder under Lectures/week10/game as your starting point.

If you wish, you may work with one other person on this assignment. If you work with a partner, turn in only one copy of your code, but include both of your names in a comment at the top of each file.

Part 1

Create a few new places and connect them to existing places in the world. In particular, create the places "Walker Beach", "Clark I", and "Norton Hall". Walker Beach should be north of Walker Hall and south of Honnold Library (Note: you'll need to remove the existing path from Walker Hall to Honnold). Clark I should be north of Frary Dining Hall and east of Walker Beach. Norton should be east of Clark V and south of Frary Dining Hall. If you wish, you may create other places as well.

Part 2

Surprisingly, trolls have a particular fondness for twinkies. Modify the eat method for trolls so that if a troll attempts to eat a person who is carrying the twinkies, the troll will eat the twinkies instead of the person. In your code, you should make sure to remove the twinkies from the person's possessions, since the twinkies no longer exist. Also remember to remove the twinkies from the place as well. You may find it useful to lower a troll's "hunger threshold" in order to test out your code more easily. In addition, you might want to temporarily change the restlessness of the beings to 0 so that you can completely control their movements for testing purposes.

Part 3

Pomona's Office Against Student Affairs has asked us for help in expanding the features offered by the new Pomona "Student Disservice Card" system. Luckily, our simulation is just what's needed for trying out new ideas.

To model a student disservice card, we can define a new kind of object, called an SDcard, which is a special kind of Thing. Besides inheriting the standard properties of a Thing, each SD-card has an ID number, which uniquely identifies the card. An SDcard object supports a method id, which returns the card's ID number.

    public class SDcard extends Thing {
	private int cardID;

	public SDcard(int id, Place start) {
	    super("Card" + id, start);
	    cardID = id;
	}

	public int id() {
	    return cardID;
	}
    }

For example, we can create a new SD-card with ID number 123 and put it initially in Alexander Hall as follows:

     pomona.add(new SDcard(123, alexander));

Write a new Being method called cards that takes no parameters and returns a Vector of all of the SD-cards carried by the being, or null if the being is not carrying any cards.

Part 4

A being may move to a new place only if the place returns true when its willAcceptBeing method is called. For an ordinary place, this method always returns true. Create a new kind of place, a CardAccessedPlace, that accepts a being only if they are carrying an SD-card whose ID is a member of a list of registered IDs associated with the place. In addition to a willAcceptBeing method, we need a registerCard method which takes a card and adds the card's ID number to the list. This method should register the card only if the card is already inside the place. This way we can create and register cards with the place as their starting point, so that only those cards will let us back in.

Complete the class definition given below. Also modify the SDcard constructor so that if the card's starting place is a CardAccessedPlace, the card will be automatically registered at that place.

    public class CardAccessedPlace extends Place {
        private Vector registeredIDs;

        public CardAccessedPlace(String name, World w) {
            super(name, w);
            registeredIDs = new Vector();
        }

        public boolean willAcceptBeing(Being b) {
            ...
        }

        public void registerCard(SDcard card) {
            ...
        }
    }

The Director of Housing wants to use this new system to ensure that the student residences are only entered by students living at the residence. Redefine Clark I, Clark V, and Norton Hall to be CardAccessedPlaces. Create a few registered cards inside each of the residences. Create some students in each of the residences and have them each take a registered card. Demonstrate that they can only re-enter their own residences.

Part 5

There has been a spate of card thefts on campus recently. Obviously these criminals need to be stopped. Create a new kind of being called an Ogre, which is like a Troll but only eats people who are carrying a card that has been reported stolen. You can use Grendel as an example of a being that does special things when its performAction method is called. To make the Ogres especially effective, they should have a high degree of restlessness.

Write a new World method called reportStolenCard that takes a card ID number as a parameter and dispatches a new ogre to hunt down the felon. You should keep track of the stolen card numbers (not the cards themselves) in a Vector inside the World object. The reportStolenCard method should add the ID to the list of stolen IDs, create and install a new ogre in the world, and print a short message announcing that the card has been reported stolen. Naturally, the ogre should start its hunt from the dungeon. An ogre should be able to go anywhere, even without an SD-card.

Part 6

Some students find the new card locks on the residences a nusiance. It is difficult to get together to do homework assignments and their friends who used to drop by to chat never do so anymore because they can't get in without a valid card. Luckily, the cards are easy to duplicate and distribute to friends.

To discourage the use of duplicate cards, the Director of Housing has decided to monitor the use of cards. If a card is used in two places at the same time then one of the copies must be forged.

Implement a new World method called informBigBrother that takes a card ID and a place as parameters. This method should monitor all the information to detect forged cards and report them by calling reportStolenCard. The time is available from the clock via the time method. Modify the code for CardAccessedPlaces so that they inform Big Brother whenever someone gains access with a card.

Now you have the elements of a simple game. Your goal is to leave your residence, gain access to all the other residences, and return, without being eaten. To make the game more interesting, you should also create several students and define a performAction method for students so that a student will try to move around campus, collecting SD-cards and other interesting things that they find, and occasionally leaving some of their possessions behind when they move on. Be sure to leave a few forged cards around just for fun.

If you wish, you may add other features to your simulated world, or extend the game in any other way you like.


Turning in Your Homework

To turn in your homework, put all of your Java source code files into a single folder named Your Name HW 9 and drop this folder into the drop box. If you work with a partner, include both of your names. If you are an off-campus student, you may copy your folder to a CLEAN floppy disk (one containing no other files or folders), and hand this in at the beginning of class, instead of using the drop box.