
state = 1

state = 2

state = 3
One program. Three pictures. Nothing about the drawing code changes between them — the lamps are in the same places, the housing is the same size, and every circle is drawn every single time.
What changes is one number, and an if. Lab 2 let one variable move every lamp. Lab 3 lets one variable decide which lamp is on, and by the end you will be changing it with the spacebar while the program runs.
'F' → Fullscreen
Start from your finished Lab 2 program, not an empty file. Save a copy named 1-14-stoplight so Lab 2 stays intact — you will want it again.
How to read the code blocks. Each task's code block shows your whole program, from line 1, as it should look when that task is finished.
The green lines marked with a + are the ones you add or change for that task — everything else is already in your file. Do not type the + sign.
state variable, the three Color variables, and one if.DrawCircle calls so their last argument is the matching variable — redLamp, yellowLamp, greenLamp — instead of Color.Red and friends. Miss this and nothing you do to the variables will show up.state to 2 and run again. All three lamps go dark. That is correct for now — there is only one if, and its condition is false.#:package Raylib-cs@8.0.0
using Raylib_cs;
int screenWidth = 400;int screenHeight = 600;
int lampRadius = 40;int padding = 20;int gap = 100;
int lampX = screenWidth / 2;int firstLampY = 120;
int housingWidth = 2 * (lampRadius + padding);int housingHeight = 2 * gap + 2 * lampRadius + 2 * padding;int housingX = lampX - housingWidth / 2;int housingY = firstLampY - lampRadius - padding;
int state = 1;
Raylib.InitWindow(screenWidth, screenHeight, "Stoplight");Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose()){ Color redLamp = Color.DarkGray; Color yellowLamp = Color.DarkGray; Color greenLamp = Color.DarkGray;
if (state == 1) { redLamp = Color.Red; }
Raylib.BeginDrawing(); Raylib.ClearBackground(Color.DarkGray);
Raylib.DrawRectangle(housingX, housingY, housingWidth, housingHeight, Color.Black); Raylib.DrawCircle(lampX, firstLampY, lampRadius, redLamp); Raylib.DrawCircle(lampX, firstLampY + gap, lampRadius, yellowLamp); Raylib.DrawCircle(lampX, firstLampY + 2 * gap, lampRadius, greenLamp);
Raylib.EndDrawing();}
Raylib.CloseWindow();if into a three-branch chain.state: 1, then 2, then 3. Each should light exactly one lamp, and your screen should match the three pictures at the top of this page.state = 7. Write down which lamp lights, and why. It is not a bug, and it is not nothing.#:package Raylib-cs@8.0.0
using Raylib_cs;
int screenWidth = 400;int screenHeight = 600;
int lampRadius = 40;int padding = 20;int gap = 100;
int lampX = screenWidth / 2;int firstLampY = 120;
int housingWidth = 2 * (lampRadius + padding);int housingHeight = 2 * gap + 2 * lampRadius + 2 * padding;int housingX = lampX - housingWidth / 2;int housingY = firstLampY - lampRadius - padding;
int state = 1;
Raylib.InitWindow(screenWidth, screenHeight, "Stoplight");Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose()){ Color redLamp = Color.DarkGray; Color yellowLamp = Color.DarkGray; Color greenLamp = Color.DarkGray;
if (state == 1) { redLamp = Color.Red; } else if (state == 2) { yellowLamp = Color.Yellow; } else { greenLamp = Color.Green; }
Raylib.BeginDrawing(); Raylib.ClearBackground(Color.DarkGray);
Raylib.DrawRectangle(housingX, housingY, housingWidth, housingHeight, Color.Black); Raylib.DrawCircle(lampX, firstLampY, lampRadius, redLamp); Raylib.DrawCircle(lampX, firstLampY + gap, lampRadius, yellowLamp); Raylib.DrawCircle(lampX, firstLampY + 2 * gap, lampRadius, greenLamp);
Raylib.EndDrawing();}
Raylib.CloseWindow();if statements that advance state and wrap it back around.Console.WriteLine($"state = {state}"); just below the wrap, so you can watch the number as well as the light.else if, run it, and press space past green a few times.else if back to an if.#:package Raylib-cs@8.0.0
using Raylib_cs;
int screenWidth = 400;int screenHeight = 600;
int lampRadius = 40;int padding = 20;int gap = 100;
int lampX = screenWidth / 2;int firstLampY = 120;
int housingWidth = 2 * (lampRadius + padding);int housingHeight = 2 * gap + 2 * lampRadius + 2 * padding;int housingX = lampX - housingWidth / 2;int housingY = firstLampY - lampRadius - padding;
int state = 1;
Raylib.InitWindow(screenWidth, screenHeight, "Stoplight");Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose()){ if (Raylib.IsKeyPressed(KeyboardKey.Space)) { state = state + 1; }
if (state > 3) { state = 1; }
Color redLamp = Color.DarkGray; Color yellowLamp = Color.DarkGray; Color greenLamp = Color.DarkGray;
if (state == 1) { redLamp = Color.Red; } else if (state == 2) { yellowLamp = Color.Yellow; } else { greenLamp = Color.Green; }
Raylib.BeginDrawing(); Raylib.ClearBackground(Color.DarkGray);
Raylib.DrawRectangle(housingX, housingY, housingWidth, housingHeight, Color.Black); Raylib.DrawCircle(lampX, firstLampY, lampRadius, redLamp); Raylib.DrawCircle(lampX, firstLampY + gap, lampRadius, yellowLamp); Raylib.DrawCircle(lampX, firstLampY + 2 * gap, lampRadius, greenLamp);
Raylib.EndDrawing();}
Raylib.CloseWindow();Two changes, one at a time. Screenshot or describe what you see each time, and put it back before the next.
else from the color chain, so it becomes three separate if statements. Then change the second condition to state == 1 as well, and run it with state = 1.else block only — keep the first two branches as if and else if — and run it with state = 3.For each one, say what a driver at this intersection would do if a real light looked like that. One of the two is dangerous in a way the other is not.
state = 4 as a fourth case that lights the red lamp, and change the wrap so 4 is reachable.Color variable. Two different states can brighten the same lamp.Your two broken versions from Task 4 should look like this. If yours looked different, you changed something else as well.

Task 4, change 1 — separate ifs

Task 4, change 2 — no final else
Look hard at the one on the left, because you have seen it before. It is not a redrawing of Lab 1's stoplight — it is the same image file. The picture you were proud of in Activity 1.7 is what a broken Lab 3 looks like, and the only thing that changed in between is that a stoplight is now supposed to decide.
state = 7 lit, and whyelse if wrap, and can say why the light still looked rightAnswer these on your own before submitting your work.
if actually changing?if statements gave the same picture for every valid state. Explain why the chain is still the better choice here.state is an ordinary int, but the program behaves differently depending on what it holds. What does that let this program do that Lab 2's could not?Submit four things to the dropbox:
state = 7 answer, and the number the console showed when the wrap became an else if.