Ejemplo n.º 1
0
        public void SampleRockTest_RockClass()
        {
            var grid = Program.InitializeGrid(5, 5);
            var rock = new Rock(new [] { 0, 0 }, grid);
            var robot = new Robot()
            {
                Position = new[] {3, 3}
            };

            robot = rock.TakeAction(robot);

            Assert.IsTrue(robot.Position.SequenceEqual(new[] {3, 3}));
        }
Ejemplo n.º 2
0
        public void SampleSpinnerTest()
        {
            var grid = Program.InitializeGrid(5, 5);
            var spinner = new Obstacle<int>()
            {
                Type = "Spinner",
                ActionData = 270,
                RobotAction = (r, angle) => r.UpdateDirection(angle),
                Grid = grid
            };
            var robot = new Robot();

            robot = spinner.TakeAction(robot);

            Assert.IsTrue(robot.Direction == 3);
        }
Ejemplo n.º 3
0
        public void SampleRockTest_ObstacleClass()
        {
            var grid = Program.InitializeGrid(5, 5);
            var rock = new Obstacle()
            {
                Type = "Rock",
                RobotAction = r => r,
                Grid = grid
            };
            var robot = new Robot()
            {
                Position = new[] { 3, 3 }
            };

            robot = rock.TakeAction(robot);

            Assert.IsTrue(robot.Position.SequenceEqual(new[] {3, 3}));
        }
Ejemplo n.º 4
0
        public void SampleHoleTest()
        {
            var grid = Program.InitializeGrid(5, 5);
            var hole = new Obstacle<int[]>()
            {
                Type = "Hole",
                ActionData = new[] { 4, 4 },
                RobotAction = (r, endpoint) => r.UpdatePosition(new [] { endpoint[0] - r.Position[0], endpoint[1] - r.Position[1] }, grid),
                Grid = grid
            };
            var robot = new Robot()
            {
                Position = new[] {3, 3}
            };

            robot = hole.TakeAction(robot);

            Assert.IsTrue(robot.Position.SequenceEqual(new[] { 4, 4 }));
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            #region Test Data
            var testGrid = InitializeGrid(5, 5);
            var testObstacles = new List<IObstacle>
            {
                new Rock(new [] { 2, 2 }, testGrid),
                new Rock(new [] { 2, 3 }, testGrid),
                new Hole(new [] { 1, 1 }, new [] { 1, 0 }, testGrid),
                new Hole(new [] { 4, 3 }, new [] { 2, 4 }, testGrid),
                new Spinner(270, new [] { 1, 2 }, testGrid),
                new Spinner(90, new [] { 3, 0 }, testGrid)
            };
            PopulateGrid(testObstacles, ref testGrid);

            var robot = new Robot()
            {
                Direction = 2
            };

            var instructions = "LFFLFRFRFLRLRFRFLFL"; // should end at [1, 2] with direction 1 (90)
            #endregion
            Console.WriteLine("Output result will be in the following format:");
            Console.WriteLine("instruction: [x, y] (direction)");
            Console.WriteLine();

            Console.WriteLine("Starting parameters:");
            Console.WriteLine(" : [{0},{1}] ({2})", robot.Position[0], robot.Position[1], robot.Direction * 90);

            foreach (var i in instructions)
            {
                robot.Move(i.ToString(), testGrid);
                Console.WriteLine("{0}: [{1},{2}] ({3})"
                    ,i , robot.Position[0], robot.Position[1], robot.Direction * 90);
            }

            Console.WriteLine();
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
Ejemplo n.º 6
0
        public void UpdatePosition_MoveToPositionWithSpinner()
        {
            var grid = Program.InitializeGrid(5, 5);
            var spinner = new Spinner(270, new [] { 1, 0 }, grid);
            grid[grid.FirstOrDefault(g => g.Key.SequenceEqual(spinner.Position)).Key] = spinner;
            var robot = new Robot();

            robot = robot.UpdatePosition(spinner.Position, grid);

            Assert.IsTrue(robot.Position.SequenceEqual(new[] { 1, 0 }));
            Assert.AreEqual(3, robot.Direction);
        }
Ejemplo n.º 7
0
        public void UpdatePosition_MoveToPositionWithRock()
        {
            var grid = Program.InitializeGrid(5, 5);
            var rock = new Rock(new [] { 0, 0 }, grid);
            grid[grid.FirstOrDefault(g => g.Key.SequenceEqual(rock.Position)).Key] = rock;
            var robot = new Robot();

            robot = robot.UpdatePosition(rock.Position, grid);

            Assert.IsTrue(robot.Position.SequenceEqual(new[] { 0, 0 }));
        }
Ejemplo n.º 8
0
        public void UpdatePosition_MoveToPositionWithHole()
        {
            var grid = Program.InitializeGrid(5, 5);
            var hole = new Hole(new[] {3, 3}, new[] {1, 0}, grid);
            grid[grid.FirstOrDefault(g => g.Key.SequenceEqual(hole.Position)).Key] = hole;
            var robot = new Robot();

            robot = robot.UpdatePosition(hole.Position, grid);

            Assert.IsTrue(robot.Position.SequenceEqual(new[] { 3, 3 }));
        }
Ejemplo n.º 9
0
        public void UpdatePosition_MoveToEmptyPosition()
        {
            var grid = Program.InitializeGrid(5, 5);
            var robot = new Robot();

            robot = robot.UpdatePosition(new[] {1, 0}, grid);

            Assert.IsTrue(robot.Position.SequenceEqual(new[] {1, 0}));
        }
Ejemplo n.º 10
0
        public void UpdatePosition_MoveOutOfBounds()
        {
            var grid = Program.InitializeGrid(5, 5);
            var robot = new Robot();

            robot = robot.UpdatePosition(new[] { -1, -1 }, grid);

            Assert.IsTrue(robot.Position.SequenceEqual(new[] { 0, 0 }));
        }
Ejemplo n.º 11
0
        public void UpdateDirection_NotMultiplesOf90()
        {
            var robot = new Robot();

            Assert.IsTrue(robot.UpdateDirection(1).Direction == 0);
            Assert.IsTrue(robot.UpdateDirection(45).Direction == 0);
            Assert.IsTrue(robot.UpdateDirection(91).Direction == 1);
            Assert.IsTrue(robot.UpdateDirection(179).Direction == 2);
            Assert.IsTrue(robot.UpdateDirection(181).Direction == 0);
            Assert.IsTrue(robot.UpdateDirection(361).Direction == 0);
            Assert.IsTrue(robot.UpdateDirection(271).Direction == 3);
            Assert.IsTrue(robot.UpdateDirection(316).Direction == 2);
        }
Ejemplo n.º 12
0
        public void UpdateDirection_MultiplesOf90()
        {
            var robot = new Robot();

            Assert.IsTrue(robot.UpdateDirection(0).Direction == 0);
            Assert.IsTrue(robot.UpdateDirection(90).Direction == 1);
            Assert.IsTrue(robot.UpdateDirection(90).Direction == 2);
            Assert.IsTrue(robot.UpdateDirection(180).Direction == 0);
            Assert.IsTrue(robot.UpdateDirection(360).Direction == 0);
            Assert.IsTrue(robot.UpdateDirection(180).Direction == 2);
            Assert.IsTrue(robot.UpdateDirection(270).Direction == 1);
        }
Ejemplo n.º 13
0
        public void Move_PositiveCase()
        {
            var robot = new Robot()
            {
                Position = new [] {3, 3},
                Direction = 0
            };
            var instructions = "FRRR"; // ends up back at [3, 3] with direction 3
            var grid = Program.InitializeGrid(5, 5);

            foreach (var instruction in instructions)
                robot.Move(instruction.ToString(), grid);

            Assert.IsTrue(robot.Position.SequenceEqual(new [] { 3, 3 }));
            Assert.IsTrue(robot.Direction == 3);
        }
Ejemplo n.º 14
0
        public void Move_OutOfBounds()
        {
            var robot = new Robot()
            {
                Position = new[] {3, 3},
                Direction = 0
            };
            var instructions = "FFFF"; // will attempt to move to [3, -1] with direction 0
            var grid = Program.InitializeGrid(5, 5);

            foreach (var instruction in instructions)
                robot.Move(instruction.ToString(), grid);

            Assert.IsTrue(robot.Position.SequenceEqual(new [] { 3, 0 }));
            Assert.IsTrue(robot.Direction == 0);
        }