CIT 590 Assignment 5: Traffic
Spring 2009, David Matuszek
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.
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.
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().
Create a two-dimensional array of
Vehicles. Create anArrayDisplaywith your Vehicle array as an argument to the constructor.Initially, every location in the array will have the value
null. Create someCars and a coupleAmbulances and place them in random locations in the array; for each vehicle, give it a random destination along some edge of the array.
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:
- If a vehicle is already at its destination, remove it from the array.
- At each step a vehicle can:
- Move one square ahead, if that square is empty and the vehicle has the right-of-way, or
- Turn left or right (but it cannot make a U-turn), or
- Stay where it is.
- A car has the right-of-way if:
- There is no car to its right that also wants to move into the same square, and
- No ambulance wants to move into the same square.
- An ambulance has the right of way if:
- 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.)
Here's a quick summary of how to use the ArrayDisplay class:
Paintable; each
such class must define a
public void paint(Graphics g, int x, int y,
int width, int height)Paintable, or of some type
that implements Paintable.ArrayDisplay object, using your array as an argument
to the constructor.null.ArrayDisplay object to repaint().repaint() again.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();
TrafficarrayDisplay, traffic class ArrayDisplay (provided)interface Paintable (provided)class Town
Paintable objectsArrayDisplaypublic
static void main(String[] args)
Town and tells it to run the
simulationpublic abstract class Vehicle implements Paintable
public class Car extends Vehicle
public void paint(Graphics g, int x, int y, int width, int
height)
public void move()
public class Ambulance extends Vehicle
public void paint(Graphics g, int x, int y, int width, int
height)
public void move()
Traffic_yourName 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.)
Your program is due before midnight, Thursday, February 19. Zip up all files and submit via Blackboard.