Jobbet litt med trafikklys

This commit is contained in:
Geir Okkenhaug Jerstad 2024-12-10 14:42:11 +01:00
parent d3db60a6e3
commit 2549653f2c
14 changed files with 296 additions and 1 deletions

View file

@ -0,0 +1,17 @@
namespace Trafikklys.Demo2;
internal class Demo2
{
public static void Run()
{
var trafikklys = new Trafikklys();
while (true)
{
Console.Clear();
trafikklys.Show();
Console.ReadKey(true);
trafikklys.GoToNextPhase();
}
}
}

View file

@ -0,0 +1,40 @@
namespace Trafikklys.Demo2;
internal class Trafikklys
{
bool red;
bool yellow;
bool green;
public Trafikklys()
{
red = true;
}
public void Show()
{
TrafikklysKonsoll.Show(red, yellow, green);
}
public void GoToNextPhase()
{
if (red && !yellow)
{
yellow = true;
}
else if (red)
{
red = false;
yellow = false;
green = true;
}else if (green)
{
green = false;
yellow = true;
}else if (yellow)
{
yellow = false;
red = true;
}
}
}

View file

@ -0,0 +1,16 @@
namespace Trafikklys.Demo3;
public class Demo3
{
public static void Run()
{
var trafikklys = new Trafikklys();
while (true)
{
Console.Clear();
trafikklys.Show();
Console.ReadKey(true);
trafikklys.GoToNextPhase();
}
}
}

View file

@ -0,0 +1,38 @@
namespace Trafikklys.Demo3;
public class Trafikklys
{
private bool Red { get; set; }
public bool Yellow { get; private set; }
public bool Green { get; private set; }
public Trafikklys()
{
Red = true;
}
public void Show()
{
TrafikklysKonsoll.Show(Red, Yellow, Green);
}
public void GoToNextPhase()
{
if (Red && !Yellow)
{
Yellow = true;
}else if (Red)
{
Red = false;
Yellow = false;
Green = true;
}else if (Green)
{
Green = false;
Yellow = true;
}else if (Yellow)
{
Yellow = false;
Red = true;
}
}
}

View file

@ -0,0 +1,16 @@
namespace Trafikklys.Demo4;
public class Demo4
{
public static void Run()
{
var trafikklys = new Trafikklys();
while (true)
{
Console.Clear();
trafikklys.Show();
Console.ReadKey(true);
trafikklys.GoToNextPhase();
}
}
}

View file

@ -0,0 +1,27 @@
namespace Trafikklys.Demo4;
public class Trafikklys
{
private int _phase;
public void SetPhase(int phase)
{
if (phase >= 0 && phase <= 3)
{
_phase = phase;
}
}
public void Show()
{
var red = _phase < 2;
var yellow = _phase is 1 or 3;
var green = _phase == 2;
TrafikklysKonsoll.Show(red, yellow, green);
}
public void GoToNextPhase()
{
_phase = _phase < 3 ? _phase + 1 : 0;
}
}

View file

@ -1,3 +1,6 @@
using Trafikklys.Demo1;
using Trafikklys.Demo2;
using Trafikklys.Demo3;
using Trafikklys.Demo4;
Demo1.Run();
Trafikklys.Demo4.Demo4.Run();