CMSC 15100 - Fall 2006 - Lab Assignment 2

Traffic Simulatr 1.0 beta

In this lab, you will design a simulator for an intersection of two one-way streets (one running west-to-east, the other running north-to-south) with a single stop-light. For the purposes of the simulation, there is always exactly one car on each road. It will look like this when you're done:

where hitting a key makes the light change.

Part 1 Define an image bg that looks like this:

Also define images for a car pointed east and a car pointed south, and circles that represent red, green, and yellow stop lights.

Part 2 A light-color is 'red, 'yellow, or 'green. Define functions add-n/s-light : light-color image -> image, add-e/w-light : light-color image -> image that take an image (presumably a background) and add a stop light of the appropriate color in the appropriate position.

Part 3 A car-loc is either 'before, 'in, or 'after, meaning that the car is either before the light, in the intersection, or past the intersection. Define functions add-n/s-car and add-e/w-car with contract car-loc image -> image.

Part 4

An Intersection is either:
   - (make-rg car-loc car-loc)
   - (make-ry car-loc car-loc)
   - (make-gr car-loc car-loc)
   - (make-yr car-loc car-loc)

where in each case the first car-loc indicates the position of the east-bound
car and the second indicates the position of the west-bound car, and the
letters rg, ry, gr, and yr correspond to the colors of the west-facing and 
north-facing traffic lights, respectively.

Define the function intersection->scene that turns an intersection into an image.

Part 5 Define the function next-intersection : Intersection -> Intersection that advances any appropriate cars in the given intersection. Assume that if a car leaves the street to the east or south, another will come along to take its place from the west or north. Assume that the light never changes.

Part 6 Define the function next-light : Intersection -> Intersection that advances the state of the traffic light, but leaves any cars where they are. For example, an rg traffic light becomes an ry traffic light.

Part 7 Now you are ready to animate the world. Copy in this code into your program:

(define (handle-keypress i key)
  (cond
    [(symbol? key) i]
    [(char? key)
     (cond
       [(char=? key #\space) (next-light i)]
       [else i])]))

(big-bang 175 175 1 (make-rg 'before 'before))
(on-tick-event next-intersection)
(on-key-event handle-keypress) 
(on-redraw intersection->scene)
Now run your program! Hitting space will advance the state of the light.

You are done with the lab. Submit it by sending it to jacobm+lab@cs.uchicago.edu. Include your and your partner's names.


Jacob Matthews
jacobm at cs.uchicago.edu
Hinds 026