Class TrafficJam

java.lang.Object
  |
  +--TrafficJam

public class TrafficJam
extends java.lang.Object

This is a very crude simulation of traffic. Cars and Trucks move about on a "traffic grid," avoiding collisions and each trying to reach its destination.

Version:
1.0
Author:
David Matuszek

Field Summary
private  int columns
           
private  java.lang.String[][] destinations
           
(package private)  int MAX_COLUMNS
           
(package private)  int MAX_ROWS
           
(package private)  int MAX_VEHICLES
           
private  int numberOfVehicles
           
private static java.util.Random random
           
private  int rows
           
private  Vehicle[][] town
           
private  Vehicle[] vehicles
           
 
Constructor Summary
(package private) TrafficJam(int rows, int columns, int numberOfCars, int numberOfTrucks)
           
 
Method Summary
(package private)  boolean hasRoomForTruck(int row, int column)
          Returns true if the given location is null AND an adjacent location is also null (because trucks require two spaces).
static void main(java.lang.String[] args)
          Parameters: Number of rows, number of columns, number of cars, number of trucks.
(package private)  void runSimulation()
          Simulates traffic flow.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

random

private static java.util.Random random

town

private Vehicle[][] town

destinations

private java.lang.String[][] destinations

rows

private int rows

columns

private int columns

vehicles

private Vehicle[] vehicles

numberOfVehicles

private int numberOfVehicles

MAX_ROWS

final int MAX_ROWS
See Also:
Constant Field Values

MAX_COLUMNS

final int MAX_COLUMNS
See Also:
Constant Field Values

MAX_VEHICLES

final int MAX_VEHICLES
See Also:
Constant Field Values
Constructor Detail

TrafficJam

TrafficJam(int rows,
           int columns,
           int numberOfCars,
           int numberOfTrucks)
Method Detail

hasRoomForTruck

boolean hasRoomForTruck(int row,
                        int column)
Returns true if the given location is null AND an adjacent location is also null (because trucks require two spaces).


main

public static void main(java.lang.String[] args)
Parameters: Number of rows, number of columns, number of cars, number of trucks.


runSimulation

void runSimulation()
Simulates traffic flow. Each Vehicle in turn has a chance to move, and will move one step closer to its goal unless blocked by another Vehicle. To reduce congestion, Vehicles which have reached their destinations are removed from the traffic grid. The simulation continues until either all Vehicles have reached their destinations, or until all remaining Vehicles are blocked.