public void TestStrategy(Strategy strategy, World environment, StrategyTesterReport start, List<PointD> expectedPoints, List<double> expectedAngles, List<bool> expectedResults)
 {
     var curReport = start;
     var reports = new List<StrategyTesterReport>();
     while (true)
     {
         var newAction = strategy.GetNextState(curReport);
         var movesList = newAction.Item1;
         if (newAction.Item2 is EndOfStrategy) break;
         curReport = environment.TryMove(curReport, movesList, true);
         reports.Add(curReport);
     }
     CollectionAssert.AreEqual(reports.Select(x => x.Coords).ToList(), expectedPoints);
     CollectionAssert.AreEqual(reports.Select(x => x.AngleInRadians).ToList(), expectedAngles, new DoubleComaperer());
     CollectionAssert.AreEqual(reports.Select(x => x.Success).ToList(), expectedResults);
 }
Beispiel #2
0
 public StrategyTesterReport TryMove(StrategyTesterReport originalState, List<LowLevelCommand> movesList, bool doFast)
 {
     Moving = true;
     var isSuccess = true;
     foreach (var move in movesList)
     {
         var type = move.GetType().Name;
         isSuccess = (bool)OurRobot
             .GetType()
             .GetMethod(type, BindingFlags.Instance | BindingFlags.Public)
             .Invoke(OurRobot, new object[] { originalState, move, Objects, doFast });
         if (isSuccess) continue;
         OurRobot.Angle = originalState.AngleInRadians;
         OurRobot.Coords = originalState.Coords;
         break;
     }
     var resultingState = new StrategyTesterReport(OurRobot.Angle, OurRobot.Coords, isSuccess);
     Moving = false;
     return resultingState;
 }
Beispiel #3
0
 public bool Rotate(StrategyTesterReport originalState, Rotate command, List<Polygon> obstacles, bool doFast)
 {
     bool isMoveSuccessfull = true;
     var targetAngle = Angle + command.AngleSpeed * command.Time;
     var timeDelta = command.Time / 100;
     for (var i = 0.0; i < command.Time - timeDelta; i += timeDelta)
     {
         Angle += timeDelta * command.AngleSpeed;
         if (obstacles.Any(x => x.IsPointIn(Coords)))
         {
             Angle = originalState.AngleInRadians;
             Coords = originalState.Coords;
             isMoveSuccessfull = false;
             break;
         }
         if (!doFast) Thread.Sleep((int)(timeDelta * 1000));
     }
     Angle = targetAngle;
     return isMoveSuccessfull;
 }
Beispiel #4
0
 public bool Forward(StrategyTesterReport originalState, Forward command, List<Polygon> obstacles, bool doFast)
 {
     var isMoveSuccessfull = true;
     var timeDelta = command.Time / 100;
     var targetCoords = new PointD(Coords.X + command.Time * command.Speed * Math.Cos(Angle),
                                   Coords.Y + command.Time * command.Speed * -Math.Sin(Angle));
     for (var i = 0.0; i < command.Time - timeDelta; i += timeDelta)
     {
         Coords.X += timeDelta * command.Speed * Math.Cos(Angle);
         Coords.Y += timeDelta * command.Speed * -Math.Sin(Angle);
         if (obstacles.Any(x => x.IsPointIn(Coords)))
         {
             Angle = originalState.AngleInRadians;
             Coords = originalState.Coords;
             isMoveSuccessfull = false;
             break;
         }
         if (!doFast) Thread.Sleep((int)(timeDelta * 1000));
     }
     if (isMoveSuccessfull) Coords = targetCoords;
     return isMoveSuccessfull;
 }
Beispiel #5
0
 public void SetState(StrategyTesterReport state)
 {
     OurRobot.Angle = state.AngleInRadians;
     OurRobot.Coords = state.Coords;
 }
Beispiel #6
0
 public bool Nothing(StrategyTesterReport originalState, Nothing command, List<Polygon> obstacles, bool doFast)
 {
     return true;
 }
 public void TestStrategyWithObstacles()
 {
     var planB = new Strategy()
         .MoveTo(300, 300)
         .MoveTo(400, 400)
         .StopAt(500, 500, 90)
         .End();
     var strategy = new Strategy(new StrategyTesterTranslator())
         .MoveTo(100, 100)
         .MoveTo(200, 100)
         .Else(planB)
         .MoveTo(200, 200)
         .StopAt(100, 200, 0)
         .End();
     var start = new StrategyTesterReport(0, new PointD(20, 20), true);
     var environment = new World(start.Coords, 0);
     environment.Objects.Add(new Polygon(new Point(150, 90), new Size(10, 20)));
     TestStrategy(
         strategy,
         environment,
         start,
         new List<PointD>
         {
             new PointD(100, 100),
             new PointD(100, 100),
             new PointD(300, 300),
             new PointD(400, 400),
             new PointD(500, 500)
         },
         new List<double>
         {
             Math.PI * 2 - Math.PI / 4,
             Math.PI * 2 - Math.PI / 4,
             Math.PI * 2 - Math.PI / 4,
             Math.PI * 2 - Math.PI / 4,
             Math.PI / 2
         },
         new List<bool> { true, false, true, true, true }
     );
     environment = new World(start.Coords, 0);
     environment.Objects.Add(new Polygon(new Point(182, 140), new Size(30, 10)));
     strategy.GoToPreviousState(6);
     TestStrategy(
         strategy,
         environment,
         start,
         new List<PointD>
         {
             new PointD(100, 100),
             new PointD(200, 100),
             new PointD(200, 100),
             new PointD(300, 300),
             new PointD(400, 400),
             new PointD(500, 500)
         },
         new List<double>
         {
             Math.PI * 2 - Math.PI / 4,
             0,
             0,
             AngleCaculator.CalculateAngle(new PointD(200, 100), new PointD(300, 300)),
             Math.PI * 2 - Math.PI / 4,
             Math.PI / 2
         },
         new List<bool> { true, true, false, true, true, true }
     );
 }
 public void TestStrategyWithoutObstacles()
 {
     var strategy = new Strategy(new StrategyTesterTranslator())
         .MoveTo(100, 100)
         .MoveTo(200, 100)
         .MoveTo(200, 200)
         .StopAt(100, 200, 0)
         .End();
     var start = new StrategyTesterReport(0, new PointD(20, 20), true);
     TestStrategy(
         strategy,
         new World(start.Coords, 0),
         start,
         new List<PointD>
         {
             new PointD(100, 100),
             new PointD(200, 100),
             new PointD(200, 200),
             new PointD(100, 200)
         },
         new List<double> { Math.PI * 2 - Math.PI / 4, 0, Math.PI * 2 - Math.PI / 2, 0 },
         new List<bool> { true, true, true, true }
         );
 }
 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);
 }
 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();
 }
 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);
 }
 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);
 }