CIT 590 Assignment 5: Traffic
Spring 2009, David Matuszek

Purposes of this assignment:

General idea of the assignment:

This is a simulation of traffic in a small Town. There will be two kinds of Vehicle: Car and Ambulance. A Vehicle starts at a random location along the edge of the town, and proceeds to some random location on a different edge of the town. As it moves, it gives right-of-way to other vehicles as necessary, and avoids colliding with other vehicles.

Instead of "streets" and "intersections," we will have a two-dimensional array of "locations;" each location contains either a vehicle or null. Vehicles progress by moving to an adjacent location in the array, either horizontally or vertically.

Details:

First of all, all numbers may vary. Accordingly, don't put magic numbers in your code. Define all necessary numbers (number of rows, number of columns, initial number of cars, initial number of ambulances, etc.) as final int variables, declared at the top of your Town class.

Vehicles:

Every Vehicle has certain properties (instance variables): The row and column that it is currently in, the destinationRow and destinationColumn that it is trying to get to, and the direction that it is facing (use 0 for north, 1 for east, 2 for south, and 3 for west). Vehicles can move().

To begin:

Create a two-dimensional array of Vehicles. Create an ArrayDisplay with your Vehicle array as an argument to the constructor.

Initially, every location in the array will have the value null. Create some Cars and a couple Ambulances and place them in random locations in the array; for each vehicle, give it a random destination along some edge of the array.

To run the simulation:

The simulation proceeds in a series of steps.

At each step, from 1 to 5 additional vehicles will be created. Each vehicle will initially be placed somewhere along a randomly chosen edge (but not in a corner), and given a destination along some other edge (but again, not a corner).

At each step give every Vehicle a chance to move(). Here are the rules for movement:

  1. If a vehicle is already at its destination, remove it from the array.
  2. At each step a vehicle can:
  3. A car has the right-of-way if:
    1. There is no car to its right that also wants to move into the same square, and
    2. No ambulance wants to move into the same square.
  4. An ambulance has the right of way if:
    1. There is no ambulance to its right that also wants to move into the same square.

No vehicle should move more than one square on a turn! (This is a particularly easy mistake to make when you are moving a vehicle down or to the right.)

ArrayDisplay:

Here's a quick summary of how to use the ArrayDisplay class:

  1. Define any number of classes that implement Paintable; each such class must define a public void paint(Graphics g, int x, int y, int width, int height) method.
  2. Create a two-dimensional array of Paintable, or of some type that implements Paintable.
  3. Create an ArrayDisplay object, using your array as an argument to the constructor.
  4. Put some objects (ones that implement Paintable) into your array. "Empty" locations in the array should be null.
  5. Tell your ArrayDisplay object to repaint().
  6. Whenever you make changes to the array, and you want it painted again, call repaint() again.
Example:
class Monster implements Paintable {
    public void paint(Graphics g, int x, int y, int width, int height) { ... }
 ...
 }
 class SomeOtherClass {
     Monster[][] monsterLand = new Monster[10][10];
     ArrayDisplay view = new ArrayDisplay(monsterLand);
     ...
     monsterLand[i][j] = new Monster(Color.GREEN);
     view.repaint();

Structure of the assignment:

The above are requirements. You may have additional classes and methods as needed, and they should be documented.

You are to write this program by yourself. You may not copy code from anyone else, nor may you lend your code to anyone else. However, you are encouraged to discuss the program with others, and to help others as appropriate.

You should have a Vehicle class which does the work common to both cars and trucks, and you extend it with Car and Ambulance classes. (The differences between a car and an ambulance are how they are painted, and when they have the right-of-way.)

Due date:

Your program is due before midnight, Thursday, February 19. Zip up all files and submit via Blackboard.