Advertisement
daixso

Traffic Light Sim

May 29th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace TrafficLightSim
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         protected Boolean norSouGrpActive = false;
  16.         protected Boolean simulationRunning = false;
  17.  
  18.  
  19.         protected int greenTime = 10; // in seconds
  20.         protected int yellowTime = 3; // in seconds
  21.  
  22.         public Form1()
  23.         {
  24.             InitializeComponent();
  25.         }
  26.  
  27.         private void Form1_Load(object sender, EventArgs e)
  28.         {
  29.             // INITIALIZE DEFAULT STATE
  30.  
  31.             // Set start button text
  32.             startStopBtn.Text = "Start Simulation";
  33.  
  34.             // Set all lights to red
  35.             northLight.BackColor = Color.Red;
  36.             southLight.BackColor = Color.Red;
  37.             eastLight.BackColor = Color.Red;
  38.             westLight.BackColor = Color.Red;
  39.         }
  40.  
  41.         private void consoleOutput(int type, string message)
  42.         {
  43.             switch (type)
  44.             {
  45.                 case 0:
  46.                     consoleLog.AppendText("DEBUG: " + message + "\n");
  47.                     break;
  48.                 case 1:
  49.                     consoleLog.AppendText("WARNING: " + message + "\n");
  50.                     break;
  51.                 default:
  52.                     consoleLog.AppendText("INFO: " + message + "\n");
  53.                     break;
  54.             }
  55.         }
  56.  
  57.         private void startStopBtn_Click(object sender, EventArgs e)
  58.         {
  59.             if (!simulationRunning)
  60.             {
  61.                 simulationRunning = !simulationRunning; // Invert simulationRunning
  62.                 norSouGrpActive = !norSouGrpActive;
  63.  
  64.                 // Update startStopBtn text
  65.                 startStopBtn.Text = "Stop Simulation";
  66.  
  67.                 // Set North South group to green
  68.  
  69.                 changeState('g');
  70.  
  71.                 consoleOutput(0, "Initialized. Starting the simulation loop.");
  72.                 simulationLoop();
  73.             }
  74.             else
  75.             {
  76.                 simulationRunning = !simulationRunning; // Invert simulationRunning
  77.  
  78.                 // Update startStopBtn text
  79.                 startStopBtn.Text = "Start Simulation";
  80.  
  81.                 // Stop all traffic
  82.                 allTrafficStop();
  83.             }
  84.  
  85.         }
  86.  
  87.         private async void simulationLoop()
  88.         {
  89.             while (simulationRunning)
  90.             {
  91.                 //consoleOutput(0, "Lights changing to yellow in 10 seconds.");
  92.                 await Task.Delay(greenTime * 1000).ContinueWith(t => preLightChange());
  93.             }
  94.         }
  95.  
  96.         private async void preLightChange()
  97.         {
  98.             changeState('y');
  99.             //consoleOutput(0, "Lights changing switching in 3 seconds.");
  100.             await Task.Delay(yellowTime * 1000).ContinueWith(t => lightChange());
  101.         }
  102.  
  103.         private void lightChange()
  104.         {
  105.             // Set current group to red
  106.             changeState('r');
  107.             // Invert current group
  108.             norSouGrpActive = !norSouGrpActive;
  109.             // Set new group to green
  110.             changeState('g');
  111.         }
  112.  
  113.         private void allTrafficStop()
  114.         {
  115.             northLight.BackColor = Color.Red;
  116.             southLight.BackColor = Color.Red;
  117.             eastLight.BackColor = Color.Red;
  118.             westLight.BackColor = Color.Red;
  119.         }
  120.  
  121.         private void changeState(char state)
  122.         {
  123.             if (norSouGrpActive)
  124.             {
  125.                 switch (state)
  126.                 {
  127.                     case 'g':
  128.                         northLight.BackColor = Color.Green;
  129.                         southLight.BackColor = Color.Green;
  130.                         break;
  131.                     case 'y':
  132.                         northLight.BackColor = Color.Yellow;
  133.                         southLight.BackColor = Color.Yellow;
  134.                         break;
  135.                     case 'r':
  136.                         northLight.BackColor = Color.Red;
  137.                         southLight.BackColor = Color.Red;
  138.                         break;
  139.                 }
  140.             }
  141.             else if (!norSouGrpActive)
  142.             {
  143.                 switch (state)
  144.                 {
  145.                     case 'g':
  146.                         eastLight.BackColor = Color.Green;
  147.                         westLight.BackColor = Color.Green;
  148.                         break;
  149.                     case 'y':
  150.                         eastLight.BackColor = Color.Yellow;
  151.                         westLight.BackColor = Color.Yellow;
  152.                         break;
  153.                     case 'r':
  154.                         eastLight.BackColor = Color.Red;
  155.                         westLight.BackColor = Color.Red;
  156.                         break;
  157.                 }
  158.             }
  159.         }
  160.  
  161.         private void resetBtn_Click(object sender, EventArgs e)
  162.         {
  163.             simulationRunning = !simulationRunning; // Invert simulationRunning
  164.  
  165.             // Update startStopBtn text
  166.             startStopBtn.Text = "Start Simulation";
  167.  
  168.             // Stop all traffic
  169.  
  170.             allTrafficStop();
  171.         }
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement