CS 60, Fall 2001

Assignment 6, Due Fri. 19 October

Assignment 7, Due Fri. 2 November

Traffic Jam, Phase 1

 

Construct a Java applet for phase 1, the construction phase, of the Traffic Jam game, shown below. Note that appropriate use of objects should be regarded as a requirement, since this is one of the topics that this assignment tries to exercise.

The red rectangle and the gap at the right end of that row have a significance that will be described below. This image was captured from Internet Explorer 5 running on a Mac on web page where such an applet resides:

 

http://www.cs.hmc.edu/courses/2001/fall/cs60/examples/java/TrafficJam/

 

In phase 1, your applet allows you to construct a traffic jam. A traffic jam consists of a set of cars (dominoes of length 2) and trucks (dominoes of length 3) laid out on a grid. Cars and trucks are “drawn” by dragging the mouse over cells in the grid. The applet must constrain things so that at most one car or truck can cover a given cell. Cars and trucks cannot be split in two, be laid out on diagonals, have wrap-around, etc.

 

Below is a picture of a completed puzzle. The applet has arbitrarily assigned different colors to each car and truck.

The object of the game is for a user to maneuver the cars and trucks by sliding them up and down or right and left on the grid so that the distinguished red car (as shown in the first picture) can move to the exit position at the right. Sliding can only be done along the axis of a car/truck; it cannot slide perpendicular to its axis. For example, the red car can only move left and right. The picture below shows the state where the red car has moved to the exit position. This follows after 16 steps of moving various cars/trucks. See if you can mentally trace the steps that would be used.

 

In phase 2 (the next assignment), you will write code that can determine whether a solution is possible and, if so, will steer the cars and trucks so that the red car gets to the exit position. In other words, the phase 2 applet will solve the puzzle, if there is a solution. If not, it will say so. This is a simple form of “AI” (artificial intelligence) problem based on searching a graph.

 

Obviously for phase 1 you will need to learn how to make your applet react to the mouse. The textbook and lecture notes will contain some information on this. You will also need to know how to sense a control-click of the mouse for purposes of erasure. We will provide you with the slider component for stepping the puzzle through its solution.

 

Here is a checklist of specific things your phase 1 applet will need to be able to do:

Here are some design suggestions: