Project 5: Playing with objects

The goal of this project is to practice writing a simple class.

1. Introduction

One of the purposes of objects is for them to be components in a software system. Many programs written in an object-oriented language are viewed as collections of interacting objects. In this project, I am providing almost the entire system for a program (including a graphical user interface). Your task is to develop one class to complete the system.

The program is a game which involves a series of levels. In each level, the user controls a character which must collect treasures and avoid monsters. The character is controlled by five buttons: to move up, down, left, and right, and to disappear or reappear. The monsters will move toward you, and if one "gets" you, the game is over. The character collects treasure by moving into a space that current holds a treasure. When all treasures in a level are collected, then the level is cleared and you move on to the next level. When all levels are completed, the game is won.

As mentioned above, one control button makes your character appear or disappear. The advantage of disappearing is that the monsters can't see you (but they can still get you if the happen to move into you-- or if you happen to move into them-- while you are invisible). The disadvantage is that you can't see yourself either, and you can't collect any treasure while you are invisible. In the lower levels, the monsters will stop moving if they don't see you, but in the higher levels, they will remember where you were when you disappeared and keep moving toward that spot.

2. Setup

To make a directory for this project, clone the repository from the class directory:

hg clone /cslab/class/csci235/project/project5

In that directory, you'll find two files. The extension jar stands for "Java archive" and contains a collection of archived Java classfiles. Since there is a complex set of classes for this project, this is where they've been hidden. Player.java is the source file for the class you need to finish.

You can play the game by compiling Player.java and running

java -cp .:game.jar MonsterGame

(The flag -cp .:game.jar tells the Java virtual machine to look both in the current directory (".") and the file game.jar for classfiles. If you're curious to know more about the -cp flag, jar files, and the like, just ask.)

As you try out the game, you'll see a green T standing for the player, and yellow O standing for the treasure, and a magenta M standing for the monster. The buttons, which are supposed to move the player, don't do anything. The monster moves towards the player and gets it, and the game ends. The net result of what you are supposed to do in this project is that the buttons will work and one can play the game.

3. Finishing the Player class

The Player class's contract with the rest of the system is

Your task is to finish this class, according to this contract. You should think about

The grid will always be of size 30 columns (x) and 20 rows (y), with the positions numbered starting at zero. You are responsible to make sure that if the user clicks (for example) the left button when the player is already on the left edge of the board, then the player does not actually move any further left. Otherwise the program will crash.

4. Turn in

Use handin to turn in your Player.java as project5.

DUE: Friday,Oct 18, at 5:00 p.m.


Thomas VanDrunen, Cary Gray
Last modified: Mon Oct 7 14:02:11 CDT 2013