Skip to main content

more options

Assignment 2, Alternative 1. J*Man

Introduction

In this assignment, you will create a simple video game called J*Man. In it, the player uses buttons to steer the star-like J*Man around a board, capturing other pieces. The assignment will give you more experience with the structure of object-oriented programs (e.g. subclasses, casting, and abstract classes). You will develop a fair number of complicated interactive methods. You will need to read and understand the specification of the given code.

To see the game J*Man in action, first download this jar file (which is our solution to the assignment) onto your desktop and then double-click it to start it going. A window will open, with buttons, the game board, and the game rules. Below, we give more extensive rules of the game.

Rules of the Game

Four kinds items appear on the game board.

  1. J*Man. J*Man is vaguely asterisk shaped and will be red, yellow, or green. His color determines what other pieces he can capture. There is only one J*Man.
  2. White blocks. They just sit there, getting in the way.
  3. Walkers. They are circular. They are red, yellow, or green. As J*Man moves about the board, the walkers wander about slowly.
  4. Pillars, which appear as red, yellow, or green triangles. Pillars don't move. They change color randomly.

The goal is to use the four navigation buttons to move J*Man about the board until he has captured all the walkers and pillars. J*Man cannot capture a walker or a pillar unless their colors are appropriate. J*Man can capture:

  • a red piece only if he, himself, is green;
  • a yellow piece only if he is red; and
  • a green piece only if he is yellow.

Whenever J*Man captures a piece, he takes on that piece's color.

J*Man and the walkers follow the following rule: If they try to move into a square that is occupied by something they cannot capture (for a walker, any occupied space) or try to move off the map, they don't move.

After each attempt by J*Man to move, all the Walkers and Pillars also get to perform one action, as follows:

  • Approximately 2/3 of the time (this is random), their action consists of doing nothing.
  • The other 1/3 of the time, a pillar randomly chooses a color: red, green, or yellow.
  • The other 1/3 of the time, a walker attempts to move up, down, left, or right. The choice is random. If the square to move to is off the map or already occupied, the walker doesn't move.

Program structure

The program has two parts:

  1. The graphical user interface (GUI), which draws the board in a JFrame window, responds to clicks of buttons, and so forth. GUIs are beyond the scope of this course, and we provide all the GUI code. At the end of this document, we provide some explanation and point you to reading material to learn more about building a GUI in Java. We urge you to study this material, but you don't have to.
  2. Code that performs the action of the various pieces. You will write virtually all of this part.

Graphic User Interface (GUI)

We provide the GUI. The button on the top left displays a dialogue that asks the user if they want to start a new game; the button on the right displays the rules of J*Man. The middle of the screen is the game board, and the four buttons on the bottom are used to move J*Man around the board.

Classes

In file a2jmanstart.zip, we provide parts of three classes: Map, Piece, and JMan. Abstract class Piece describes the common behavior of the various pieces on the board. You have to complete the bodies of some of its methods and decide what fields it needs. You will write subclasses of Piece, whose instances will maintain the pieces —J*Man, blocks, walkers, and pillars. How you do this is up to you, with a few exceptions, one of which is that subclass JMan of Piece should be used for the instance of J*Man in the game. We have given just enough of JMan so that the three classes compile.

Take a look at class Map. Much of it has to do with the GUI, and you should focus on

  1. Field grid, which is declared near line 30. This array contains the pieces that are on the board. An array element is null if that position is empty. The upper left hand corner of the grid is (0,0), i.e. grid[0][0]. The x-value increases to the right; the y-value increases down.
  2. Field jman, which contains an object that describes the J*Man.
  3. Function isEmpty, near line 186. Look at its spec. Then, spending some time understanding the code will help you get a better feel for how field grid is used. You will probably call this function.
  4. Procedure putNew, near line 162. Class Map is complete except for the body of this procedure, and you have to complete it. You can't do this until you have at least started the necessary subclasses of class Piece.
  5. Look carefully at procedure actionPerformed, near line 245. This procedure is called when a button is pressed. It determines which button was pressed and acts accordingly. If the button was a request to move J*Man in some direction, it calls jMan.step with the appropriate argument. This method, in class JMan, then makes the move.

Completing class Piece

Your first task should be to complete abstract class Piece. It is the behavior of the class that is important to the use, of the class, and we have defined that behavior by declaring and specifying the methods. You now have to determine what fields are needed in order for the methods to work properly.

Note carefully the four static fields BLOCK, JMAN, WALKER, and PILLAR in class Piece. They have been declared with attribute final, which means that they cannot be changed and thus can be considered to be constants. They are used to communicate values to methods.

Note also function rand at the end of class Piece; call this function to generate random numbers.

Completing class JMan

Consider completing class JMan next. We have helped you out by providing skeletons for the four methods that have to be written. After you write the constructors, you should be able to write part of procedure Map.putNew, so that at least this one piece is placed properly. that will help you see progress.

Writing other subclasses of Piece and completing Map.putNew

It is up to you to determine what other subclasses of Piece are needed and to write them. As you finish one, you should be able to write more of the code in procedure Map.putNew —and check it out.

Note that class JMan has a function toString. You should write a similar function in every subclass of Piece; it is invaluable for debugging, because you can find out what a piece p is simply by executing System.out.println(p);.

Summary

We summarize what you need to do in this assignment.

1. Do not make any changes to class Map except the body of method putNew.

2. Fix method Map.putNew, fix classes Piece and JMan, and write whatever other subclasses of Piece are needed to complete the program.

3. Before submitting your assignment, click the Javadoc button and check the specification that results. If your specifications on the new classes that you write are not suitable, you will be asked to fix them and resubmit.

4. Implement anything you need in order to have the game work properly.

5. Test the program any way you want. If your program does not work properly, you will be asked to fix it.

How to submit your assignment

Please put all your .java files for this project in a folder. Then produce a .zip file of this folder, named a2jman.zip. Submit file a2jman.zip on the CMS.

About building GUIs in Java

The best place to learn about building GUIs in Java is to read/listen to pages 17-1 to 17-4 of the CD ProgramLive. You already know that a JFrame is a window on your monitor. Page 17-1 tells you how you can add components to the JFrame, like buttons and text fields.

Page 17-2 then shows you the basic components that one can add to a JFrame, and for each one, you can download the code for the demo.

Page 17-3 then tells you about layout managers, which are used to put components in various positions of a JFrame.

Finally, Page 17-4 shows you the basics of "listening" to actions, like a click of a button.

Probably, in less than 1.5 hours, you can have a good idea about how GUIs are used. It will then be fairly simple to look at class Map and see how the GUI is constructed. The GUI is constructed in the constructor. First, the "preferred sizes" of the buttons are defined. Second, the directional buttons are added to a Box. Third, a "listener" is registered for each button, so that the system know which method to call when a button is pressed. Fourth, all the components are added to the JFrame. Fifth, the grid is constructed. Sixth, the JFrame is "packed", placed, made unresizable, and shown.

If you understood pages 17-1 to 17-4, you can understand how this GUI is constructed.