Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace TrafficLightSim
- {
- public partial class Form1 : Form
- {
- protected Boolean norSouGrpActive = false;
- protected Boolean simulationRunning = false;
- protected int greenTime = 10; // in seconds
- protected int yellowTime = 3; // in seconds
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- // INITIALIZE DEFAULT STATE
- // Set start button text
- startStopBtn.Text = "Start Simulation";
- // Set all lights to red
- northLight.BackColor = Color.Red;
- southLight.BackColor = Color.Red;
- eastLight.BackColor = Color.Red;
- westLight.BackColor = Color.Red;
- }
- private void consoleOutput(int type, string message)
- {
- switch (type)
- {
- case 0:
- consoleLog.AppendText("DEBUG: " + message + "\n");
- break;
- case 1:
- consoleLog.AppendText("WARNING: " + message + "\n");
- break;
- default:
- consoleLog.AppendText("INFO: " + message + "\n");
- break;
- }
- }
- private void startStopBtn_Click(object sender, EventArgs e)
- {
- if (!simulationRunning)
- {
- simulationRunning = !simulationRunning; // Invert simulationRunning
- norSouGrpActive = !norSouGrpActive;
- // Update startStopBtn text
- startStopBtn.Text = "Stop Simulation";
- // Set North South group to green
- changeState('g');
- consoleOutput(0, "Initialized. Starting the simulation loop.");
- simulationLoop();
- }
- else
- {
- simulationRunning = !simulationRunning; // Invert simulationRunning
- // Update startStopBtn text
- startStopBtn.Text = "Start Simulation";
- // Stop all traffic
- allTrafficStop();
- }
- }
- private async void simulationLoop()
- {
- while (simulationRunning)
- {
- //consoleOutput(0, "Lights changing to yellow in 10 seconds.");
- await Task.Delay(greenTime * 1000).ContinueWith(t => preLightChange());
- }
- }
- private async void preLightChange()
- {
- changeState('y');
- //consoleOutput(0, "Lights changing switching in 3 seconds.");
- await Task.Delay(yellowTime * 1000).ContinueWith(t => lightChange());
- }
- private void lightChange()
- {
- // Set current group to red
- changeState('r');
- // Invert current group
- norSouGrpActive = !norSouGrpActive;
- // Set new group to green
- changeState('g');
- }
- private void allTrafficStop()
- {
- northLight.BackColor = Color.Red;
- southLight.BackColor = Color.Red;
- eastLight.BackColor = Color.Red;
- westLight.BackColor = Color.Red;
- }
- private void changeState(char state)
- {
- if (norSouGrpActive)
- {
- switch (state)
- {
- case 'g':
- northLight.BackColor = Color.Green;
- southLight.BackColor = Color.Green;
- break;
- case 'y':
- northLight.BackColor = Color.Yellow;
- southLight.BackColor = Color.Yellow;
- break;
- case 'r':
- northLight.BackColor = Color.Red;
- southLight.BackColor = Color.Red;
- break;
- }
- }
- else if (!norSouGrpActive)
- {
- switch (state)
- {
- case 'g':
- eastLight.BackColor = Color.Green;
- westLight.BackColor = Color.Green;
- break;
- case 'y':
- eastLight.BackColor = Color.Yellow;
- westLight.BackColor = Color.Yellow;
- break;
- case 'r':
- eastLight.BackColor = Color.Red;
- westLight.BackColor = Color.Red;
- break;
- }
- }
- }
- private void resetBtn_Click(object sender, EventArgs e)
- {
- simulationRunning = !simulationRunning; // Invert simulationRunning
- // Update startStopBtn text
- startStopBtn.Text = "Start Simulation";
- // Stop all traffic
- allTrafficStop();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement