using Microsoft.VisualBasic; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Diagnostics; public class Form1 { LogicMap Logic = new LogicMap(); private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if (e.KeyData == Keys.Space) { Console.WriteLine("------- New Step ---------"); Logic.DoLogic(); } } private void Form1_Load(System.Object sender, System.EventArgs e) { Logic.AddGate(new Point(100, 130), 0, 0, 0); Logic.AddGate(new Point(160, 40), 0, 0, 4); Logic.AddGate(new Point(205, 105), 0, 3, 1); Logic.AddGate(new Point(450, 35), 0, 4, 0); Logic.AddGate(new Point(595, 135), 0, 0, 1); Logic.AddPulse(Color.FromArgb(255, 0, 0), 1, 5); Logic.AddPulse(Color.FromArgb(255, 128, 162), 2, 7); } private void DrawLogicMap() { Graphics g = this.CreateGraphics(); } public class LogicMap { public List NextGates; public List ActiveGates; private List Gates; public void AddGate(Point Pos, LogicType Logic, int TrueGate, int FalseGate) { Gates.Add(new LogicGate { L = Logic, NT = TrueGate, NF = FalseGate, P = new List(), isActive = false, id = Gates.Count, Position = Pos }); Console.WriteLine("#Added Gate(" + Gates.Count - 1 + "): Logic(" + Logic + "), FalseGate(" + FalseGate + "), TrueGate(" + TrueGate + ")"); } public bool AddPulse(Color Color, int Gate, int Value) { if (Gate >= Gates.Count | Gate <= -1) return false; Gates[Gate].AddPulse(Color, ref this, ref Value); Console.WriteLine("#Added Pulse at Gate(" + Gate + ") with Value(" + Value + ")"); return true; } public LogicMap() { NextGates = new List(); ActiveGates = new List(); Gates = new List(); Console.WriteLine("#Created new Logic Map"); } public bool DoLogic() { ActiveGates.Clear(); //Replace and make room for new changes. ActiveGates.AddRange(NextGates); NextGates.Clear(); Form1.CreateGraphics().Clear(Color.Black); foreach (object Gate_loopVariable in Gates) { Gate = Gate_loopVariable; Gate.DrawDebugBase(); Gate.DebugDrawLines(this, Gates); } foreach (LogicGate Gate in ActiveGates) { Gate.DoLogic(ref this, ref Gates); Gate.DrawDebug(); } return true; } } public struct Pulse { public int Value; public Color Color; public object MoveTo(ref LogicMap LogMap, ref LogicGate OldGate, ref LogicGate NewGate, bool State) { OldGate.P.Remove(this); Graphics G = Form1.CreateGraphics(); Point Position = default(Point); if (LogicGate.IsVoid(ref NewGate)) { Console.WriteLine("Stopped at Gate:" + OldGate.id); if (State == false) Position = new Point(OldGate.Position.X + 20, OldGate.Position.Y + 10); else Position = new Point(OldGate.Position.X + 20, OldGate.Position.Y - 10); G.DrawEllipse(new Pen(this.Color), Position.X - 7, Position.Y - 7, 14, 14); return false; } NewGate.P.Add(this); if (State == false) Position = new Point(OldGate.Position.X + 20, OldGate.Position.Y + 10); else Position = new Point(OldGate.Position.X + 20, OldGate.Position.Y - 10); G.DrawLine(new Pen(this.Color, this.Value), Position, NewGate.Position); Console.WriteLine("Moved from to " + OldGate.id + " to Gate: " + NewGate.id); if (NewGate.isActive == false) { LogMap.NextGates.Add(NewGate); NewGate.isActive = true; } return true; } } public struct LogicGate { public int NT; public int NF; public List P; public LogicType L; public bool isActive; public int id; public Point Position; public bool AddPulse(Color Colore, ref LogicMap LogMap, ref int Value) { P.Add(new Pulse { Value = Value, Color = Colore }); this.isActive = true; LogMap.NextGates.Add(this); return true; } public static LogicGate VoidGate() { return new LogicGate { NT = -1, L = LogicType.None, NF = -1 }; } public static bool IsVoid(ref LogicGate Gate) { if (Gate.NF == -1 & Gate.NT == -1 & Gate.L == LogicType.None & (Gate.P == null)) return true; return false; } public void DebugDrawLines(ref LogicMap LogMap, ref List Gates) { LogicGate TrueGate = default(LogicGate); LogicGate FalseGate = default(LogicGate); Graphics g = Form1.CreateGraphics; if ((NT >= Gates.Count | NT <= -1)) { TrueGate = LogicGate.VoidGate(); Point PosTrue = new Point(Position.X + 20, Position.Y - 10); g.DrawEllipse(Pens.WhiteSmoke, PosTrue.X - 4, PosTrue.Y - 4, 8, 8); } else { TrueGate = Gates[NT]; Point PosTrue = new Point(Position.X + 20, Position.Y - 10); g.DrawLine(Pens.White, PosTrue, TrueGate.Position); } if ((NF >= Gates.Count | NF <= -1)) { Point PosFalse = new Point(Position.X + 20, Position.Y + 10); FalseGate = LogicGate.VoidGate(); g.DrawEllipse(Pens.WhiteSmoke, PosFalse.X - 4, PosFalse.Y - 4, 8, 8); } else { FalseGate = Gates[NF]; Point PosFalse = new Point(Position.X + 20, Position.Y + 10); g.DrawLine(Pens.White, PosFalse, FalseGate.Position); } } public bool DoLogic(ref LogicMap LogMap, ref List Gates) { LogicGate TrueGate = default(LogicGate); LogicGate FalseGate = default(LogicGate); this.isActive = false; if (L == LogicType.None) return false; if ((NT >= Gates.Count | NT <= -1)) TrueGate = LogicGate.VoidGate(); else TrueGate = Gates[NT]; if ((NF >= Gates.Count | NF <= -1)) FalseGate = LogicGate.VoidGate(); else FalseGate = Gates[NF]; return Form1.LFunc[L](LogMap, this, TrueGate, FalseGate); } public void DrawDebug() { dynamic G = Form1.CreateGraphics(); G.FillRectangle(Brushes.Red, Position.X - 20, Position.Y - 10, 40, 20); G.FillRectangle(Brushes.Green, Position.X + 18, Position.Y - 12, 4, 4); G.FillRectangle(Brushes.Blue, Position.X + 18, Position.Y + 8, 4, 4); } public void DrawDebugBase() { dynamic G = Form1.CreateGraphics(); G.FillRectangle(Brushes.Gray, Position.X - 20, Position.Y - 10, 40, 20); G.FillRectangle(Brushes.Green, Position.X + 18, Position.Y - 12, 4, 4); G.FillRectangle(Brushes.Blue, Position.X + 18, Position.Y + 8, 4, 4); G.DrawString(this.id.ToString(), Form1.Font, Brushes.Black, new Point(Position.X - 6, Position.Y - 7)); } } public enum LogicType { MoreThan = 0, LessThan = 1, MoreOrEqualTo = 2, LessOrEqualTo = 3, EqualTo = 4, NotEqualTo = 5, None = 255 } public static readonly System.Func[] LFunc = { (LogicMap LM, LogicGate L, LogicGate TL, LogicGate FL) => { List TrueList = new List(); List FalseList = new List(); foreach (Pulse P1 in L.P) { int SumF = 0; int SumT = 0; foreach (Pulse P2 in L.P) { if (!(Pulse.ReferenceEquals(P1, P2))) { if (P1.Value >= P2.Value) SumT += 1; else SumF += 1; } } if (SumT >= SumF) TrueList.Add(P1); else FalseList.Add(P1); } foreach (Pulse P in TrueList) { Console.WriteLine("Pulse at Gate(" + L.id + ") Returned True"); P.MoveTo(LM, L, TL, true); } foreach (Pulse P in FalseList) { Console.WriteLine("Pulse at Gate(" + L.id + ") Returned False"); P.MoveTo(LM, L, FL, false); } return true; } }; public Form1() { Load += Form1_Load; KeyDown += Form1_KeyDown; } }