The goal of this project is to practice writing a simple class.
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.
To make a directory for this project, clone the repository from the class directory:
hg clone /cslab/class/csci235/projects/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.
Player
classThe Player
class's contract with the rest of
the system is
int
x and y
coordinates in the grid) and the size of the grid in its constructor.
(Currently, the constructor does nothing with this information).
getXPos()
and getYPos()
.
Currently these methods always say the player is in
position (0,0).
up()
, down()
, left()
,
right()
, and disOrReappear()
are
invoked on the object.
getIcon()
is called, it
must return a character. If the player is visible, the
character returned will represent the player on the screen;
if the player is invisible, it should return a space character.
Currently, it always returns the letter 'T'.
You may choose a character of your choice for this icon, though the program
may not work correctly if you use capital 'M' or capital 'O'.
Your task is to finish this class, according to this contract. You should think about
getXPos()
, getYPos()
,
and getIcon()
should return, based on the
instance variables.
up()
, down()
,
left()
, right()
, and disOrReappear()
should affect the instance variables.
Grid positions are numbered starting at zero (of course), increasing toward the right and toward the top. 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 farther left. Otherwise the program will crash.
Use handin
to turn in your Player.java
as
project5
.
DUE: Friday, October 16, at 5:00 p.m.