Pop Quiz: Traffic Lights

As a follow up to yesterday's Power Calc article, which, in passing, mentioned state machines, I pose the following question to you:

How many states are there for a typical traffic signal controlling two perpendicular streets?

There's three light colors -- red, yellow, and green -- so that means there must be three states right?

Or, could it be that since there are twelve lights -- two streets, each street running in two directions, one set of three colored lights visible in each direction (2*2*3=12) -- that there are twelve states?

Or, noting, that opposite directions always have the same colors at the same time, perhaps that should be six states?

The correct answer is, in fact, none of the above....

It's actually four, though not because there are four controlled directions.

To see why this is so, allow me to list each of the possible states. First, though, let's name the two streets that are controlled by the lights in question as Street A and Street B. ( Kind of minimalist I know, and usually, in a city, "Streets" don't flow perpendicular to one another -- instead one major direction is labeled "St" and the other "Ave", or one has names and one has numbers, or some other such helpful system, but stick with me just the same. )

Here are the states:
  1. When the lights are green for cars on street A, by necessity the lights are red for cars on street B.
  2. When the lights are yellow for cars on street A, the lights are still red for cars on street B. ( In the state of Texas this actually means cars from both directions are starting to move through the intersection, some at high velocity, but that's a different story altogether, and doesn't, at any rate, affect the state of the lights. )
  3. When the lights on street A change from yellow to red, the lights on street B now change from red to green. ( In Texas, cautious drivers from both streets now come to a full stop because there is now a tangle of cars in the center of the intersection; again, largely beside the point. )
  4. Finally, the lights on street B change from green to yellow, by necessity the lights are still red for cars on street A. The very next state, when lights are green on street A, but red on street B, is actually state #1 again.
The most problematic state for people I've discussed this with seems to be the fourth, final, state. The combination of colors is the same as in the second state so shouldn't they be considered the same state? The important part here is how the traffic signal system as a whole looks, which actual lights are physically lit, how drivers perceive the lights, and how traffic should ( should being the key word for Texas drivers ) behave.

Here's a couple of tables that might help if you are confused. First a synopsis of the light colors, then the behavior of the drivers.

Light colors

Street A Street B
green red
yellow red
red green
red yellow

Legal / desired behavior

Street A Street B
green: drive. red: stop; wait.
yellow: slow down; don't enter the intersection. red: keep waiting.
red: stop driving. green: drive!
red: wait. yellow: slow down; don't enter the intersection.

Texas behavior

Finally, for reference sake, driving Texas style.
Street A Street B
green: drive red: proceed if there's no traffic.
yellow: speed up; achieve maximum velocity. red: start driving; cackle as pedestrians dive to safety.
red: coast. green: check cell for voice mail.
red: stop only if you have to; curse governmental control. yellow: adjust volume of radio.

Traffic Light Localization

Things actually get a whole lot more complicated if there are green light switching delays, pedestrian signals, turn arrows, or the like.

If you're making a driving game, best to write it all out long hand, or perhaps even, depending on the number of situations involved, you might consider writing a mini-dsl for controlling the lights, or maybe even create some sort of tag based rule system so you can tack on as many signal components as you want and make them all auto-magically work together.

For all my jabs at Texas drivers, drivers in Turkey are ***much*** crazier. One contributing factor: the traffic lights in Istanbul have a special flashing white strobe that, just before your light turns green, begins to flash. The instant the strobe goes off, the honking starts. "Not driving yet?" they ask.

The language of car horns, at least, is international.