CSCI 3701: Advanced Object-Oriented ProgrammingAssignment 6: Generalized ContainersDue Friday, April 30

Introduction

This assignment is meant to introduce you to generalized container classes in Java.

In this assignment you will re-implement the “pod chase” game from Assignment 2, adding a PodList class that stores a list of Pod object, as well as rewriting the Pod class to better take advantage of container methods.

Game Structure and User Interface Class

The basic structure of the game and visual application is mostly the same as in Project 1, in which the player (marked “#”) moves around a 2D map (currently 15x9) to collect “pods” (marked “*”).

·  The game provides buttons (“N”, “S”, “E”, and “W”) that moves the player one square in the direction north, south, east, or west respectively.

·  The pods themselves are always in motion, moving is a diagonal direction (either NE, NW, SE, or SW). When one reaches the edge of the board, it “bounces” off of the wall, changing its direction.

·  When the player reaches the current location of a pod, then that pod is considered “caught” and will no longer be displayed.

The main way that the game has changed is that every move there is a chance that a new pod will be generated. More specifically:

·  Each turn, a new pod will be generated with a 10% chance. This probability may be change in future versions of the game, so you will need to make it easy to change.

·  When a new pod is generated, it is placed at a random location on the board, and will be moving in a random direction (either NE, NW, SE, or SW).

I have provided on the course web page a user interface class called PodApp. While you will not be changing any of the code in this class, I do encourage you to take a look at the code in order to better understand the requirements for the classes that you will be creating.


Requirements for the PodList class

As you can see from the PodApp code, it now stores a single support object called PodList, rather than an array of Pod objects.

This PodList class is to act as a container for a list of Pod objects. Its purpose is to simplify the code in the application. Rather that the application having to loop through the list of pods to do things, this will be the responsibility of the PodList.

Internal Structure

Your PodList class is required to store its Pod objects in an ArrayList. To simplify your code, I suggest that you use generics to make it an ArrayList of type Pod.

Required Methods and Constructors

Your PodList class is to have the following methods (which you can see in action in the PodApp application):

·  public PodList(int width, int height)
This constructor takes the width and height of the board as parameters (and should probably store them in member variables).
It should construct four Pod objects and add them to its ArrayList. These pods are to be initialized to the following locations and directions:

o  x = 1, y = 5, direction = NE

o  x = 2, y = 1, direction = SW

o  x = 12, y = 2, direction = NW

o  x = 13, y = 6, direction = SE

·  public void moveAll()
This method should loop through the ArrayList, calling the move method of each pod in the list. This method is called by the application each turn.

·  public void generate()
This method is called by the application each turn. When it is called, it should generate a new Pod object with a 10% chance, and add that pod to the end of the ArrayList.

·  public boolean isPod(int x, int y)
This method should return true if there is a Pod in the ArrayList at location (x, y), false otherwise. This method is called by the application during the process of redrawing the board.

·  public void playerAt(int x, int y)
This method is called every turn, passing the current (x, y) location of the player. If there is a pod at that (x, y) location, it is considered to be “caught”, and should be removed from the ArrayList.

Requirements for the Pod class

The Pod class will also be greatly simplified from the first assignment. You should be able to reuse most of the code you already have, and either delete or comment out the parts that are no longer necessary. Specifically, you should no longer need:

·  isVisible: Since pods caught by the player are now removed from the list, there is no need to make them invisible when caught.

·  playerAt: Determining whether a pod is caught is now handled by the playerAt method of the PodList.

·  getX and getY: Previously, this was used by the app to draw the pod on the board, which is now handled by the isPod method of the PodList.

The only code that you should need to add is the overloaded .equals method described below.

Your new Pod class should now have the following constructors and methods:

·  public Pod(int x, int y, String direction, int width, int height)
This constructor takes as its parameters:

o  The initial x and y coordinates of the pod.

o  The initial direction it is moving in. This will be a string with value either “NE”, “NW”, “SE”, or “SW”.

o  The width and height of the game board (you will need to know this in order to “bounce” the pod off of the walls).

This constructor should pretty much be unchanged from your first assignment.

·  public void move()
This method moves the pod in the direction of its current motion. Specifically:

o  Going north (either NE or NW) – increment the Y location by 1.

o  Going south (either SE or SW) – decrement the Y location by 1.

o  Going east (either NE or SE) – increment the X location by 1.

o  Going west (either NW or SW) – decrement the X location by 1.

As before, if the pod hits a wall (which can be computed from the parameters passed to the constructor), it should “bounce” off of the wall. More specifically:

o  If it hits the top or bottom wall, the vertical direction changes. For example, if one hits the top wall going NE its direction changes to SE, and if it was going NW, its direction changes to SW.

o  If it hits the left or right wall, the horizontal direction changes. For example, if one hits the right wall going NE its direction changes to NW, and if it was going SE, its direction changes to SW.

This method will probably also be pretty much unchanged from the first assignment.

·  public boolean equals(Object obj)
This method should return true if the object obj is “equivalent” to this pod. We will define “equivalence” as having the same location (that is, the same x and y coordinates).
As described in class, this will involve:

·  Making sure that obj is a Pod, and then casting it to a Pod.

·  Comparing the x and y coordinates of this pod to the x and y coordinates of the pod you have cast obj to.


Note that creating this equals method will make it much easier to implement the isPod and playerAt methods of the PodList class, as described in the next section.

Using Pod Equality to Implement PodList Methods

Note that the the isPod and playerAt methods of the PodList class both take an (x, y) location as parameters, and then determine whether there is a pod at that location (to display it or delete it respectively.

One way to implement this would be to manually search the ArrayList in the PodList for a pod with those coordinates. However, the existence of the equals method in the Pod class allows us to use the built-in search methods of the ArrayList instead.

Given that method, a very simple way to search for a pod with a given (x, y) location is to:

  1. Construct a Pod object with that a given (x, y) location (the direction, etc. is not relevant).
  2. Search for that Pod object in the ArrayList.
  3. If that Pod object is found, either return true (for isPod) or remove that Pod object (for playerAt).

This will allow you to replace the manual search with a single method call!

Documentation

You are also required to document your Pod and PodList class. Specifically:

·  Give an overall description of the class itself at the beginning of the class.

·  Describe what each member variable represents.

·  Document each constructor and method in the class.

As always, the documentation is a significant portion of your grade!

What to turn in:

An electronic copy of your PodList.java and Pod.java classes posted to the Blackboard CMS.