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); }
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 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(); }