public static void AddNodes(this Graph graph, State currentState)
 {
     while (currentState.Next != null)
     {
         if (graph.FindEdge(currentState, currentState.Next) != null)
         {
             currentState = currentState.Next;
             continue;
         }
         var edge = graph.AddEdge(currentState.GetHashCode().ToString(), currentState.Next.GetHashCode().ToString());
         edge.Attr.LineWidth = 2;
         var currentNode = graph.FindNode(currentState.GetHashCode().ToString());
         currentNode.LabelText = currentState.ToString();
         if (currentState.Alternative != null)
         {
             var altEdge = graph.AddEdge(currentState.GetHashCode().ToString(), currentState.Alternative.First.GetHashCode().ToString());
             altEdge.Attr.Color = Color.Gray;
             altEdge.Attr.LineWidth = 2;
             graph.AddNodes(currentState.Alternative.First);
         }
         currentNode.Attr.Shape = Shape.Box;
         currentState = currentState.Next;
     }
     var lastNode = graph.FindNode(currentState.GetHashCode().ToString());
     lastNode.LabelText = currentState.ToString();
 }
示例#2
0
 public Strategy(ITranslator translator)
 {
     First = null;
     current = null;
     last = null;
     history = new List<State>();
     this.translator = translator;
 }
示例#3
0
 public void GoToPreviousState(int number)
 {
     for (var i = 0; i < number; i++)
     {
         if (history.Count == 0) return;
         current = history[history.Count - 1];
         history.RemoveAt(history.Count - 1);
     }
 }
示例#4
0
 public GraphPanel(State start)
 {
     viewer = new GViewer();
     Graph = new Graph("graph");
     Graph.AddNodes(start);
     viewer.ToolBarIsVisible = false;
     viewer.Graph = Graph;
     viewer.Dock = DockStyle.Fill;
     var magnificationCoeff = 560.0 / viewer.Height;
     Height = 560;
     Width = (int)(viewer.Width * magnificationCoeff);
     Controls.Add(viewer);
 }
示例#5
0
 public void UnselectNode(State state)
 {
     var node = Graph.FindNode(state.GetHashCode().ToString());
     node.Attr.FillColor = Color.White;
 }
示例#6
0
 public void UnselectEdge(State from, State to)
 {
     var edge = Graph.FindEdge(from, to);
     edge.Attr.Color = memoryColor;
 }
示例#7
0
 public void SelectNode(State state)
 {
     var node = Graph.FindNode(state.GetHashCode().ToString());
     node.Attr.FillColor = selectColor;
 }
示例#8
0
 private State MakeDecision(Report report)
 {
     if (report.Success)
     {
         if (current is EndOfStrategy)
             return current;
         current = current.Next;
         return current.Previous;
     }
     GoToPreviousState(1);
     var pointer = current.Previous;
     while (!(current is EndOfStrategy))
         if (pointer.Alternative == null) pointer = pointer.Next;
         else
         {
             current = pointer.Alternative.First.Next;
             return current.Previous;
         }
     return new EndOfStrategy();
 }
示例#9
0
 private void Connect(State newItem)
 {
     if (First == null)
     {
         First = current = last = newItem;
     }
     else
     {
         last.Next = newItem;
         newItem.Previous = last;
         last = newItem;
     }
 }
 public static Edge FindEdge(this Graph graph, State source, State target)
 {
     var nodeFrom = graph.FindNode(source.GetHashCode().ToString());
     var nodeTo = graph.FindNode(target.GetHashCode().ToString());
     return graph.Edges.FirstOrDefault(x => x.SourceNode == nodeFrom && x.TargetNode == nodeTo);
 }
示例#11
0
 void history_ItemActivate(object sender, EventArgs e)
 {
     if (environment.Moving) return;
     graphPanel.UnselectNode(currentState);
     var selectedItemIndex = history.SelectedIndices[0];
     var stepsBack = history.Items.Count - selectedItemIndex - 1;
     for (var i = history.Items.Count - 1; i > selectedItemIndex; i--)
         history.Items.RemoveAt(i);
     moveHistory = moveHistory.Take(selectedItemIndex).ToList();
     reportHistory = reportHistory.Take(selectedItemIndex + 1).ToList();
     stateHistory = stateHistory.Take(selectedItemIndex + 1).ToList();
     strategy.GoToPreviousState(stepsBack);
     currentState = stateHistory.Last();
     currentReport = reportHistory.Last();
     environment.SetState(reportHistory.Last());
     graphPanel.SelectNode(currentState);
 }
示例#12
0
 public SimulationForm(IStrategy strategy, PointD startingPoint)
 {
     this.strategy = strategy;
     var startReport = new StrategyTesterReport(0, startingPoint, true);
     environment = new World(startReport.Coords, startReport.AngleInRadians);
     currentReport = startReport;
     moveHistory = new List<Move>();
     reportHistory = new List<StrategyTesterReport> { startReport };
     var startState = new Start(startingPoint) { Next = strategy.First };
     currentState = startState;
     stateHistory = new List<State> { startState };
     graphPanel = new GraphPanel(startState);
     graphPanel.SelectNode(startState);
     Width = Config.FieldWidth + graphPanel.Width + 150;
     Height = Config.FieldHeight+68;
     graphPanel.Location = new Point(Config.FieldWidth + 150, 0);
     DoubleBuffered = true;
     var sw = new Timer();
     var loadMap = new Button
     {
         Width = 100,
         Height = 30,
         Text = @"Load Map",
         Location = new Point(400, Height - 68)
     };
     picturePanel = new Canvas
     {
         Height = this.Height - 68,
         Width = this.Width - 150 - graphPanel.Width
     };
     var nextBut = new Button
     {
         Width = 100,
         Height = 30,
         Text = @"Next State",
         Location = new Point(0, Height - 68)
     };
     var fastNextBut = new Button
     {
         Width = 100,
         Height = 30,
         Text = @"Next Fast",
         Location = new Point(100, Height - 68)
     };
     var toEndBut = new Button
     {
         Width = 100,
         Height = 30,
         Text = @"To End Fast",
         Location = new Point(300, Height - 68)
     };
     var prevBut = new Button
     {
         Width = 100,
         Height = 30,
         Text = @"Previous State",
         Location = new Point(200, Height - 68)
     };
     history = new ListView
     {
         Height = this.Height - 40,
         Width = 150,
         Location = new Point(Width - 150 - graphPanel.Width, 0),
         View = View.List,
         MultiSelect = false
     };
     sw.Interval = 100;
     sw.Tick += (sender, e) => picturePanel.Invalidate();
     loadMap.Click += loadMap_Click;
     picturePanel.Paint += picturePanel_Paint;
     nextBut.Click += (sender, args) => MakeMoves(false, true);
     fastNextBut.Click += (sender, args) => MakeMoves(true, true);
     prevBut.Click += PrevBut_Click;
     toEndBut.Click +=toEndBut_Click;
     picturePanel.MouseDown += PicturePanel_MouseDown;
     picturePanel.MouseUp += PicturePanel_MouseUp;
     picturePanel.MouseMove += PicturePanel_MouseMove;
     picturePanel.MouseClick += picturePanel_MouseClick;
     history.ItemActivate += history_ItemActivate;
     history.Items.Add(startState.ToString());
     Controls.Add(picturePanel);
     Controls.Add(history);
     Controls.Add(nextBut);
     Controls.Add(fastNextBut);
     Controls.Add(toEndBut);
     Controls.Add(prevBut);
     Controls.Add(loadMap);
     Controls.Add(graphPanel);
     sw.Start();
 }
示例#13
0
 async void MakeMoves(bool doFast, bool async)
 {
     if (environment.Moving) return;
     var newAction = strategy.GetNextState(currentReport);
     if (currentState == newAction.Item2) return;
     graphPanel.UnselectNode(currentState);
     var movesList = newAction.Item1;
     var actionName = newAction.Item2.ToString();
     history.Items.Add(actionName);
     graphPanel.SelectEdge(currentState, newAction.Item2);
     StrategyTesterReport newState;
     if (async)
     {
         var task = Task<StrategyTesterReport>.Factory.StartNew(() => environment.TryMove(currentReport, movesList, doFast));
         newState = await task;
     }
     else newState = environment.TryMove(currentReport, movesList, doFast);
     graphPanel.UnselectEdge(currentState, newAction.Item2);
     if (newState.Success)
     {
         var lastMove = new Move {From = currentReport.Coords, To = newState.Coords};
         moveHistory.Add(lastMove);
         currentState = newAction.Item2;
         stateHistory.Add(newAction.Item2);
         reportHistory.Add(newState);
         currentReport = newState;
     }
     else
     {
         history.Items.RemoveAt(history.Items.Count - 1);
         currentReport.Success = false;
     }
     graphPanel.SelectNode(currentState);
 }
示例#14
0
 void PrevBut_Click(object sender, EventArgs e)
 {
     if (environment.Moving) return;
     if (history.Items.Count == 1) return;
     graphPanel.UnselectNode(currentState);
     history.Items.RemoveAt(history.Items.Count - 1);
     moveHistory.RemoveAt(moveHistory.Count - 1);
     reportHistory.RemoveAt(reportHistory.Count - 1);
     stateHistory.RemoveAt(stateHistory.Count - 1);
     strategy.GoToPreviousState(1);
     currentReport = reportHistory.Last();
     currentState = stateHistory.Last();
     environment.SetState(reportHistory.Last());
     graphPanel.SelectNode(currentState);
 }