Exemplo n.º 1
0
        public void CreateRobot()
        {
            var point = new Point(0, 0);
            var robot = new SimpleRobot(point, 0, _holder, 5, 5);

            Assert.IsNotNull(robot);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var grid = new GridMap {
                MaxRows = 10, MaxCols = 10
            };

            //Create Obstructions map
            grid.CreateGridTopographyManually();
            grid.Display();

            IRobot rob1 = new SimpleRobot(1, 0, 0, 'N', grid);
            IRobot rob2 = new SimpleRobot(2, 2, 3, 'N', grid);

            grid.RobotsInGrid = new List <SimpleRobot> {
                (SimpleRobot)rob1, (SimpleRobot)rob2
            };
            rob1.Display();
            // Series of Input commands
            string command = "LFFRRRFFLR";

            rob1.Navigate(command);

            rob2.Display();
            rob2.Navigate(command);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 获取起始点和终止点
        /// </summary>
        /// <param name="pngPath"></param>
        /// <returns></returns>
        private bool TryAutoExcute(string pngPath)
        {
            if (!isAuto)
            {
                return(false);
            }

            using (var bmp = new Bitmap(pngPath))
            {
                try
                {
                    AddLogAsync("尝试解析小人起点和目标落点...");
                    var result = new SimpleRobot().GetNextTap(bmp);
                    AddLogAsync("解析成功...");
                    ExcuteSwipe(result.Duration);
                    this.Invoke(new Action(() =>
                    {
                        pictureBox1.Image = new Bitmap(bmp);
                    }));
                    return(true);
                }
                catch (Exception ex)
                {
                    AddLogAsync("自动捕获出现异常:" + ex.ToString());
                    AddLogAsync("请人工辅助操作。");
                    AddLogAsync("左键单击小人底部,再右键单击目标落点即可。");
                    isAuto = false;
                    return(false);
                }
            }
        }
Exemplo n.º 4
0
        public void Status_ByDefault_IsNormal()
        {
            // Arrange
            SimpleRobot sut = CreateSut();

            // Assert
            sut.Status.Should().Be(Status.Normal);
        }
Exemplo n.º 5
0
        public void SetLastPosition_IfMovesOverEdge_SetsStatusToLost()
        {
            // Arrange
            SimpleRobot sut = CreateSut();

            // Act
            sut.LastPosition = outerPosition;

            // Assert
            sut.Status.Should().Be(Status.Lost);
        }
Exemplo n.º 6
0
        public void SetLastPosition_IfMovesOverEdge_AddsScentPosition()
        {
            // Arrange
            SimpleRobot sut = CreateSut();

            // Act
            sut.LastPosition = outerPosition;

            // Assert
            sut.Area.ScentPosition.Should().Equal(outerPosition);
        }
Exemplo n.º 7
0
        public void MoveRight()
        {
            var point = new Point(0, 0);
            var robot = new SimpleRobot(point, 0, _holder, 5, 5);

            robot.Move('R');
            var newPoint = robot.GetLocation();

            Assert.AreEqual(1, newPoint.XPoint);
            Assert.AreEqual(0, newPoint.YPoint);
        }
Exemplo n.º 8
0
        public void SetLastPosition_WithArea_DoesNotThrow()
        {
            // Arrange
            SimpleRobot sut = CreateSut();

            // Act
            Action act = () => sut.LastPosition = defaultPosition;

            // Assert
            act.Should().NotThrow();
        }
        public void RobotDoNotMove_InvalidInstruction()
        {
            //robot at (9,8) 'N'
            rob = new SimpleRobot(1, 9, 8, 'N', grid);
            rob.Move('s');
            var newPoint = rob.GetLocation();

            Assert.AreEqual(newPoint[0], 9);
            Assert.AreEqual(newPoint[1], 8);
            Assert.AreEqual(rob.GetDirection(), 'N');
        }
Exemplo n.º 10
0
        public void SetLastPosition_IfDoesNotMoveOverEdge_DoesNotAddScentPosition()
        {
            // Arrange
            SimpleRobot sut = CreateSut();

            // Act
            sut.LastPosition = innerPosition;

            // Assert
            sut.Area.ScentPosition.Should().BeEmpty();
        }
Exemplo n.º 11
0
        public void TestXBoundry0()
        {
            var point = new Point(0, 0);
            var robot = new SimpleRobot(point, 0, _holder, 5, 5);

            //try to move left from 0,0: make sure we stay there
            robot.Move('L');
            var newPoint = robot.GetLocation();

            Assert.AreEqual(0, newPoint.XPoint);
            Assert.AreEqual(0, newPoint.YPoint);
        }
Exemplo n.º 12
0
        public void TestHole()
        {
            var point = new Point(4, 0);
            var robot = new SimpleRobot(point, 0, _holder, 5, 5);

            _holder.SetObstacle(4, 2, new Hole(3, 2));
            robot.Move('F');
            robot.Move('F');
            var newPoint = robot.GetLocation();

            Assert.AreEqual(3, newPoint.XPoint);
            Assert.AreEqual(2, newPoint.YPoint);
        }
Exemplo n.º 13
0
        public void TestSpinner()
        {
            var point = new Point(2, 0);
            var robot = new SimpleRobot(point, 0, _holder, 5, 5);

            _holder.SetObstacle(2, 1, new Spinner(true, 1));
            robot.Move('F');
            robot.Move('F');
            var newPoint = robot.GetLocation();

            Assert.AreEqual(3, newPoint.XPoint);
            Assert.AreEqual(1, newPoint.YPoint);
        }
Exemplo n.º 14
0
        public void TestYBoundry0()
        {
            var point = new Point(0, 0);
            //set init direction to south (2)
            var robot = new SimpleRobot(point, 2, _holder, 5, 5);

            //move F, make sure we stay @ 00
            robot.Move('F');
            var newPoint = robot.GetLocation();

            Assert.AreEqual(0, newPoint.XPoint);
            Assert.AreEqual(0, newPoint.YPoint);
        }
Exemplo n.º 15
0
        public void RobotDoNotMove_RockOrUnkownOrBorder()
        {
            //Test rock in Front
            //robot at (9,8) 'N'
            rob = new SimpleRobot(1, 9, 8, 'N', grid);
            //rock at (9,9) , Do not move Front
            grid.Topography[9, 9] = new Rock()
            {
                ObsPosition = new int[] { 9, 9 }
            };
            rob.Move('F');
            var newPoint = rob.GetLocation();

            Assert.AreEqual(newPoint[0], 9);
            Assert.AreEqual(newPoint[1], 8);
            Assert.AreEqual(rob.GetDirection(), 'N');

            //Test Unknown on Left
            //robot at (9,8) 'N'
            rob = new SimpleRobot(1, 9, 8, 'N', grid);
            //Unknown at (8,8) , Do not move to Left
            grid.Topography[8, 8] = new Unknown()
            {
                ObsPosition = new int[] { 8, 8 }
            };
            rob.Move('L');
            newPoint = rob.GetLocation();
            Assert.AreEqual(newPoint[0], 9);
            Assert.AreEqual(newPoint[1], 8);
            Assert.AreEqual(rob.GetDirection(), 'N');


            //Test On the border
            //robot at (19,18) 'N'
            //Out of bouds on Move to Right : Do Not Move
            rob = new SimpleRobot(1, 19, 18, 'N', grid);
            rob.Move('R');
            newPoint = rob.GetLocation();
            Assert.AreEqual(newPoint[0], 19);
            Assert.AreEqual(newPoint[1], 18);
            Assert.AreEqual(rob.GetDirection(), 'N');

            //robot at (19,19) 'N'
            //Move Front / Left / Right Do not move
            rob = new SimpleRobot(1, 19, 19, 'N', grid);
            rob.Move('F');
            newPoint = rob.GetLocation();
            Assert.AreEqual(newPoint[0], 19);
            Assert.AreEqual(newPoint[1], 19);
            Assert.AreEqual(rob.GetDirection(), 'N');
        }
Exemplo n.º 16
0
 public RobotMovesTest()
 {
     grid = new GridMap()
     {
         MaxRows = 20, MaxCols = 20
     };
     Assert.IsNotNull(grid);
     rob = new SimpleRobot(1, 9, 8, 'N', grid);
     Assert.IsNotNull(rob);
     grid.RobotsInGrid = new List <SimpleRobot> {
         rob
     };
     grid.CreateGridTopographyManually();
 }
Exemplo n.º 17
0
        public void SetLastPosition_WithoutArea_Throws()
        {
            // Arrange
            SimpleRobot sut = CreateSut();

            sut.Area = null;

            // Act
            Action act = () => sut.LastPosition = defaultPosition;

            // Assert
            act.Should().Throw <InvalidOperationException>()
            .WithMessage($"Can not set position without area.");
        }
Exemplo n.º 18
0
        public void SetLastPosition_IfDoesNotMoveOverEdge_ChangesLastPosition()
        {
            // Arrange
            SimpleRobot sut = CreateSut();

            // Act
            Position start = sut.LastPosition;

            sut.LastPosition = innerPosition;

            // Assert
            sut.LastPosition.Should().Be(innerPosition);
            sut.LastPosition.Should().NotBe(start);
        }
Exemplo n.º 19
0
        public void SetLastPosition_IfMovesOverEdge_DoesNotChangeLastPosition()
        {
            // Arrange
            SimpleRobot sut = CreateSut();

            // Act
            Position start = sut.LastPosition;

            sut.LastPosition = outerPosition;

            // Assert
            sut.LastPosition.Should().Be(start);
            sut.LastPosition.Should().NotBe(outerPosition);
        }
Exemplo n.º 20
0
        public void _SetLastPosition_IfLost_DoesNotChangeLastPosition()
        {
            // Arrange
            SimpleRobot sut = CreateSut();

            // Act
            Orientation start = sut.Orientation;

            sut.LastPosition = outerPosition;
            sut.Orientation  = Orientation.E;

            // Assert
            sut.Orientation.Should().Be(start);
            sut.Orientation.Should().NotBe(Orientation.E);
        }
Exemplo n.º 21
0
        public void TestRock()
        {
            var point = new Point(0, 0);
            var robot = new SimpleRobot(point, 0, _holder, 5, 5);

            //put a rock at 0,2, try to move forward 3,  make sure we stay at 0,1
            _holder.SetObstacle(0, 2, _rock);
            robot.Move('F');
            robot.Move('F');
            robot.Move('F');
            var newPoint = robot.GetLocation();

            Assert.AreEqual(0, newPoint.XPoint);
            Assert.AreEqual(1, newPoint.YPoint);
        }
Exemplo n.º 22
0
        public void TestYBoundryMax()
        {
            var point = new Point(0, 0);
            var robot = new SimpleRobot(point, 0, _holder, 5, 5);

            //move F 5 times, make sure we stay at 0,4
            robot.Move('F');
            robot.Move('F');
            robot.Move('F');
            robot.Move('F');
            robot.Move('F');
            var newPoint = robot.GetLocation();

            Assert.AreEqual(0, newPoint.XPoint);
            Assert.AreEqual(4, newPoint.YPoint);
        }
Exemplo n.º 23
0
        public void TestXBountryMax()
        {
            var point = new Point(0, 0);
            var robot = new SimpleRobot(point, 0, _holder, 5, 5);

            //try to move right from 0,0 5 times: make sure we stay at 4,0
            robot.Move('R');
            robot.Move('F');
            robot.Move('F');
            robot.Move('F');
            robot.Move('F');
            var newPoint = robot.GetLocation();

            Assert.AreEqual(4, newPoint.XPoint);
            Assert.AreEqual(0, newPoint.YPoint);
        }
Exemplo n.º 24
0
        public void RobotMoveTo_Hole()
        {
            //robot at (9,8) 'N'
            rob = new SimpleRobot(1, 9, 8, 'N', grid);
            //hole at (9,9) connected to (7,7)
            grid.Topography[9, 9] = new Hole()
            {
                ObsPosition = new int[] { 9, 9 },
                endPosition = new int[] { 7, 7 }
            };
            rob.Move('F');
            var newPoint = rob.GetLocation();

            Assert.AreEqual(newPoint[0], 7);
            Assert.AreEqual(newPoint[1], 7);
            Assert.AreEqual(rob.GetDirection(), 'N');
        }
Exemplo n.º 25
0
        public void RobotMoveTo_Path()
        {
            //Test Path in Front
            //robot at (9,8) 'N'
            rob = new SimpleRobot(1, 9, 8, 'N', grid);
            //Path at (9,9) , Move Front to (9,9)
            grid.Topography[9, 9] = new Path()
            {
                ObsPosition = new int[] { 9, 9 }
            };
            rob.Move('F');
            var newPoint = rob.GetLocation();

            Assert.AreEqual(newPoint[0], 9);
            Assert.AreEqual(newPoint[1], 9);
            Assert.AreEqual(rob.GetDirection(), 'N');
        }
Exemplo n.º 26
0
        static void Main(string[] args)
        {
            string filepath = "";

            //get the file to parse
            if (args.Length == 0)
            {
                //if there's no argument, assume that it's in the same directory as the EXE
                filepath = "RobotSetup.json";
            }
            else
            {
                filepath = args[0];
            }
            //parse the file and store the values
            FileParser parser    = new FileParser();
            var        setValues = parser.ParseFile(filepath);

            _obstacleHolder = setValues.ObstacleHolder;
            _steps          = setValues.Steps;
            _maxX           = setValues.MaxX;
            _maxY           = setValues.MaxY;

            //make a bot
            Point  startingPoint = new Point(0, 0);
            IRobot robot         = new SimpleRobot(startingPoint, 0, _obstacleHolder, _maxX, _maxY);

            //start running the bot
            foreach (var step in _steps)
            {
                robot.Move(step);
            }
            //print the path
            int counter = 0;

            foreach (var step in robot.GetPath())
            {
                Console.WriteLine(String.Format("{0}: ({1},{2})", counter, step.XPoint, step.YPoint));
                counter++;
            }
            Console.WriteLine("\n\nEnter to exit...");
            Console.ReadLine();
        }
Exemplo n.º 27
0
        public void RobotDoNotMove_AnotherRobotPosition()
        {
            //robot1 at (9,8) 'N'
            //Robot1 can not move Right since Robot2 is present there
            rob1 = new SimpleRobot(1, 9, 8, 'N', grid);
            rob2 = new SimpleRobot(2, 9, 9, 'N', grid);
            grid.RobotsInGrid = new List <SimpleRobot>
            {
                rob1,
                rob2
            };

            rob1.Move('F');
            var newPoint = rob.GetLocation();

            Assert.AreEqual(newPoint[0], 9);
            Assert.AreEqual(newPoint[1], 8);
            Assert.AreEqual(rob.GetDirection(), 'N');
        }
Exemplo n.º 28
0
        public void SetLastPosition_IfMovesToScentPosition_IgnoresMove()
        {
            // Arrange
            SimpleRobot robot = CreateSut();

            robot.LastPosition = outerPosition;

            SimpleRobot sut = CreateSut();

            // Act
            Position start = sut.LastPosition;

            sut.LastPosition = outerPosition;
            sut.LastPosition = innerPosition;

            // Assert
            sut.LastPosition.Should().Be(innerPosition);
            sut.LastPosition.Should().NotBe(start);
        }
Exemplo n.º 29
0
        private void Run()
        {
            var area   = new RectangularAreaParser().Parse(Console.ReadLine());
            var parser = new SimpleStateParser();

            foreach (var item in GetInputs().ToArray())
            {
                var state = parser.Parse(item.State);
                var robot = new SimpleRobot {
                    Area = area, LastPosition = state.Position, Orientation = state.Orientation
                };
                var commands = new SimpleCommandParser(robot).Parse(item.Commands);
                foreach (var command in commands)
                {
                    command.Execute();
                }
                Console.WriteLine($"{robot.LastPosition.X} {robot.LastPosition.Y} {robot.Orientation}" +
                                  $"{(robot.Status == Status.Lost ? ' ' + robot.Status.ToString() : string.Empty)}");
            }
        }
Exemplo n.º 30
0
        public void RobotMoveTo_Spinner()
        {
            //robot at (9,8) 'N'
            rob = new SimpleRobot(1, 9, 8, 'N', grid);
            //move to spinner at (9,9) with spinAngle 90 , changes Direction to 'E'
            grid.Topography[9, 9] = new Spinner()
            {
                ObsPosition = new int[] { 9, 9 },
                spinAngle   = 90
            };
            rob.Move('F');
            var newPoint = rob.GetLocation();

            Assert.AreEqual(newPoint[0], 9);
            Assert.AreEqual(newPoint[1], 9);
            Assert.AreEqual(rob.GetDirection(), 'E');
            //move Right to spinner at (9,8) with spinAngle 90 , changes Direction From 'E' to 'S'
            grid.Topography[9, 8] = new Spinner()
            {
                ObsPosition = new int[] { 9, 8 },
                spinAngle   = 90
            };
            rob.Move('R');
            newPoint = rob.GetLocation();
            Assert.AreEqual(newPoint[0], 9);
            Assert.AreEqual(newPoint[1], 8);
            Assert.AreEqual(rob.GetDirection(), 'S');
            //move Right to spinner at (8,8) with spinAngle 90 , changes Direction From 'S' to 'W'
            grid.Topography[8, 8] = new Spinner()
            {
                ObsPosition = new int[] { 8, 8 },
                spinAngle   = 90
            };
            rob.Move('R');
            newPoint = rob.GetLocation();
            Assert.AreEqual(newPoint[0], 8);
            Assert.AreEqual(newPoint[1], 8);
            Assert.AreEqual(rob.GetDirection(), 'W');
            //move Right to spinner at (8,9) with spinAngle 90 , changes Direction From 'W' to 'N'
            grid.Topography[8, 9] = new Spinner()
            {
                ObsPosition = new int[] { 8, 9 },
                spinAngle   = 90
            };
            rob.Move('R');
            newPoint = rob.GetLocation();
            Assert.AreEqual(newPoint[0], 8);
            Assert.AreEqual(newPoint[1], 9);
            Assert.AreEqual(rob.GetDirection(), 'N');
            //move Left to spinner at (7,9) with spinAngle 270 , changes Direction From 'N' to 'W'
            grid.Topography[7, 9] = new Spinner()
            {
                ObsPosition = new int[] { 7, 9 },
                spinAngle   = 270
            };
            rob.Move('L');
            newPoint = rob.GetLocation();
            Assert.AreEqual(newPoint[0], 7);
            Assert.AreEqual(newPoint[1], 9);
            Assert.AreEqual(rob.GetDirection(), 'W');
        }