Пример #1
0
        public void FindPath_SmallMapWithTwoGoalsAndTwoObstacles_FindsBestPath()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #......#
                                      ########";
            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap map = Map.Create(mapCreationStrategy);

            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(1, 1, 0);
            goalMap.AddGoal(6, 1, 0);
            List <Point> obstacles = new List <Point> {
                new Point(1, 2), new Point(3, 2)
            };

            goalMap.AddObstacles(obstacles);
            Path path = goalMap.FindPath(3, 4);

            Assert.AreEqual(7, path.Length);
            ICell stepForward = path.StepForward();

            Assert.AreEqual(new Cell(4, 4, true, true, false), stepForward);
        }
Пример #2
0
        public void FindPath_SmallMapAfterAddingAndClearingGoals_PathHasOnePointAndAllCellsAreMax()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #......#
                                      ########";
            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap map = Map.Create(mapCreationStrategy);

            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(1, 1, 0);
            goalMap.AddGoal(6, 1, 0);
            goalMap.ClearGoals();
            List <Point> obstacles = new List <Point> {
                new Point(1, 2), new Point(3, 2)
            };
            List <Point> path = goalMap.FindPath(3, 4, obstacles);

            Assert.AreEqual(1, path.Count);
            Assert.AreEqual(new Point(3, 4), path[0]);
            string expectedGoalMapRepresentation = @"#    #    #    #    #    #    #    #
                                                  #   48   48   48   48    #   48    #
                                                  #   48    #   48   48    #   48    #
                                                  #   48    #   48   48    #   48    #
                                                  #   48   48   48   48   48   48    #
                                                  #    #    #    #    #    #    #    #";

            Assert.AreEqual(expectedGoalMapRepresentation.Replace(" ", string.Empty), goalMap.ToString().Replace(" ", string.Empty));
        }
Пример #3
0
        public void FindPath_BigMapFromGameAllowingDiagonalMovement_ExpectedPath()
        {
            string mapRepresentation = @"###########################################################
                                      #.....#...............#...#.....#.....#.s.....s...........#
                                      #.....#.#.###########.#...#.#.#.#.....#.#.....#.#.#######.#
                                      #.....#.#.#.....s...#.#...s.#.#.#.....#.#.....#.#.#.....#.#
                                      #.....#.#.#.....#...#####s#s#######s#####.....#.#.#.....#.#
                                      #.....s.#.s.....#...s.....#.......#.#...#.....#.#.#.....s.#
                                      #######s#.#######...#.....#.......#.#...#.....#.#.#######.#
                                      #.......#.#.....#...#.....#.......#.s...#.....#.#.......#.#
                                      #.......#.s.....#####s#.###.......#.#...#####s#.#.####s####
                                      #.......#.#.....s.#...#...#.......s.#...#.s...#.#.#.......#
                                      #.......###########...#.###############s###...#.#.#.......#
                                      #.......s...#.#...#...#.#.......#.#.......#...#.#.#.......#
                                      #######s#...#.#...##s##.s.......#.#.......#...#######s#####
                                      #.#.....#...#.#...#...#.#.......#.#.......s...#.....#...#.#
                                      #.#.....#...#.#...#####.#.......s.##s######...#.....#...#.#
                                      #.#.....#...#.#...#...#.#.......#.#.....#.#...s.....#...#.#
                                      #.#.....#...s.###s#...#.####s######.....#s#####s#####...s.#
                                      #.#.....#...#.....s...#...#...#...#.....s.....#.....#...#.#
                                      #.#.....#####.#.#.#...#.#.#...s...#s#####.....###s###...#.#
                                      #.#.....s...#.#.#.#...#.#.#...#...#.....#.....#.....#...#.#
                                      #.##s##############...###.######s##############.....#######
                                      #.........#.......#...#.......#...#.......#...#.....s.#...#
                                      #.#.#.#.#.#.......#####.......#####.......#.###########...#
                                      #.#.#.#.#.s.......#...#.......#...#.......s.#.....#...#...#
                                      ###s###############...#.......#...####s####.s.....s...#...#
                                      #...#.......#.....s...#.......s...#.......#.#.....#...#...#
                                      #...#.......#######s###.......##########s##.###s###...s...#
                                      #...#.......s...s...#.#.......#.s...#.....#.#...#.#...#...#
                                      #...#.......#...#...s.###########...#.....#.#...#.#########
                                      #...#.......#...#...#...........#...#.....#.#...#.........#
                                      #...#.......######s##.###s#####.#...#.....#.#...#.#######.#
                                      #...s.......#.......#.#.......#.s...#.....#.#...s.#.....#.#
                                      ########s####.#.#.#.#.#.......#.#...###s###.#######.....s.#
                                      #...........#.#.#.#.#.#.......#.#...s.....#.......#.....#.#
                                      ###########################################################";

            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap    map     = Map.Create(mapCreationStrategy);
            GoalMap goalMap = new GoalMap(map, true);

            goalMap.AddGoal(51, 31, 0);
            goalMap.AddGoal(51, 33, 0);
            string expectedPath = "...s...s.....s........s.....s.......s...s......s.......s...s...s...........s...s..s.....s.s...s.s..s....s..........s.....";

            Path path       = goalMap.FindPath(23, 7);
            var  actualPath = new StringBuilder();

            foreach (ICell cell in path.Steps)
            {
                actualPath.Append(cell.ToString());
            }

            Assert.AreEqual(expectedPath, actualPath.ToString());
        }
Пример #4
0
        public void FindPath_BigMapFromGame_ExpectedPath()
        {
            string mapRepresentation = @"###########################################################
                                      #.....#...............#...#.....#.....#.s.....s...........#
                                      #.....#.#.###########.#...#.#.#.#.....#.#.....#.#.#######.#
                                      #.....#.#.#.....s...#.#...s.#.#.#.....#.#.....#.#.#.....#.#
                                      #.....#.#.#.....#...#####s#s#######s#####.....#.#.#.....#.#
                                      #.....s.#.s.....#...s.....#.......#.#...#.....#.#.#.....s.#
                                      #######s#.#######...#.....#.......#.#...#.....#.#.#######.#
                                      #.......#.#.....#...#.....#.......#.s...#.....#.#.......#.#
                                      #.......#.s.....#####s#.###.......#.#...#####s#.#.####s####
                                      #.......#.#.....s.#...#...#.......s.#...#.s...#.#.#.......#
                                      #.......###########...#.###############s###...#.#.#.......#
                                      #.......s...#.#...#...#.#.......#.#.......#...#.#.#.......#
                                      #######s#...#.#...##s##.s.......#.#.......#...#######s#####
                                      #.#.....#...#.#...#...#.#.......#.#.......s...#.....#...#.#
                                      #.#.....#...#.#...#####.#.......s.##s######...#.....#...#.#
                                      #.#.....#...#.#...#...#.#.......#.#.....#.#...s.....#...#.#
                                      #.#.....#...s.###s#...#.####s######.....#s#####s#####...s.#
                                      #.#.....#...#.....s...#...#...#...#.....s.....#.....#...#.#
                                      #.#.....#####.#.#.#...#.#.#...s...#s#####.....###s###...#.#
                                      #.#.....s...#.#.#.#...#.#.#...#...#.....#.....#.....#...#.#
                                      #.##s##############...###.######s##############.....#######
                                      #.........#.......#...#.......#...#.......#...#.....s.#...#
                                      #.#.#.#.#.#.......#####.......#####.......#.###########...#
                                      #.#.#.#.#.s.......#...#.......#...#.......s.#.....#...#...#
                                      ###s###############...#.......#...####s####.s.....s...#...#
                                      #...#.......#.....s...#.......s...#.......#.#.....#...#...#
                                      #...#.......#######s###.......##########s##.###s###...s...#
                                      #...#.......s...s...#.#.......#.s...#.....#.#...#.#...#...#
                                      #...#.......#...#...s.###########...#.....#.#...#.#########
                                      #...#.......#...#...#...........#...#.....#.#...#.........#
                                      #...#.......######s##.###s#####.#...#.....#.#...#.#######.#
                                      #...s.......#.......#.#.......#.s...#.....#.#...s.#.....#.#
                                      ########s####.#.#.#.#.#.......#.#...###s###.#######.....s.#
                                      #...........#.#.#.#.#.#.......#.#...s.....#.......#.....#.#
                                      ###########################################################";

            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap    map     = Map.Create(mapCreationStrategy);
            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(51, 31, 0);
            goalMap.AddGoal(51, 33, 0);
            string expectedPath = ".....s.....s.......s...........s.....s..........s....s.......s...........s...s....s..............s.....s...s......s...s....s..s....s.....s..............s......";

            List <Point> path       = goalMap.FindPath(23, 7, new List <Point>());
            var          actualPath = new StringBuilder();

            foreach (Point p in path)
            {
                actualPath.Append(map.GetCell(p.X, p.Y).ToString());
            }

            Assert.AreEqual(expectedPath, actualPath.ToString());
        }
Пример #5
0
        public void FindPath_DestinationNotWalkableAnd13StepsAway_ThrowsPathNotFoundException()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #......#
                                      ########";
            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap    map     = Map.Create(mapCreationStrategy);
            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(7, 1, 0);

            goalMap.FindPath(1, 1);
        }
Пример #6
0
        public void FindPath_SourceCellNotWalkable_ThrowsPathNotFoundException()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #......#
                                      ########";
            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap    map     = Map.Create(mapCreationStrategy);
            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(1, 1, 0);

            goalMap.FindPath(0, 1);
        }
Пример #7
0
        public void FindPath_SmallMapAfterAddingAndClearingGoals_ThrowsPathNotFoundException()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #......#
                                      ########";
            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap map = Map.Create(mapCreationStrategy);

            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(1, 1, 0);
            goalMap.AddGoal(6, 1, 0);
            goalMap.ClearGoals();

            goalMap.FindPath(3, 4);
        }
Пример #8
0
        public void FindPath_SmallMapWithTwoGoalsAndTwoObstacles_FindsBestPath()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #......#
                                      ########";
            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap map = Map.Create(mapCreationStrategy);

            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(1, 1, 0);
            goalMap.AddGoal(6, 1, 0);
            List <Point> obstacles = new List <Point> {
                new Point(1, 2), new Point(3, 2)
            };
            List <Point> path = goalMap.FindPath(3, 4, obstacles);

            Assert.AreEqual(6, path.Count);
            Assert.AreEqual(new Point(2, 4), path[1]);
        }
Пример #9
0
        public void FindPath_BigMapFromGame_ExpectedPath()
        {
            string mapRepresentation = @"###########################################################
                                      #.....#...............#...#.....#.....#.s.....s...........#
                                      #.....#.#.###########.#...#.#.#.#.....#.#.....#.#.#######.#
                                      #.....#.#.#.....s...#.#...s.#.#.#.....#.#.....#.#.#.....#.#
                                      #.....#.#.#.....#...#####s#s#######s#####.....#.#.#.....#.#
                                      #.....s.#.s.....#...s.....#.......#.#...#.....#.#.#.....s.#
                                      #######s#.#######...#.....#.......#.#...#.....#.#.#######.#
                                      #.......#.#.....#...#.....#.......#.s...#.....#.#.......#.#
                                      #.......#.s.....#####s#.###.......#.#...#####s#.#.####s####
                                      #.......#.#.....s.#...#...#.......s.#...#.s...#.#.#.......#
                                      #.......###########...#.###############s###...#.#.#.......#
                                      #.......s...#.#...#...#.#.......#.#.......#...#.#.#.......#
                                      #######s#...#.#...##s##.s.......#.#.......#...#######s#####
                                      #.#.....#...#.#...#...#.#.......#.#.......s...#.....#...#.#
                                      #.#.....#...#.#...#####.#.......s.##s######...#.....#...#.#
                                      #.#.....#...#.#...#...#.#.......#.#.....#.#...s.....#...#.#
                                      #.#.....#...s.###s#...#.####s######.....#s#####s#####...s.#
                                      #.#.....#...#.....s...#...#...#...#.....s.....#.....#...#.#
                                      #.#.....#####.#.#.#...#.#.#...s...#s#####.....###s###...#.#
                                      #.#.....s...#.#.#.#...#.#.#...#...#.....#.....#.....#...#.#
                                      #.##s##############...###.######s##############.....#######
                                      #.........#.......#...#.......#...#.......#...#.....s.#...#
                                      #.#.#.#.#.#.......#####.......#####.......#.###########...#
                                      #.#.#.#.#.s.......#...#.......#...#.......s.#.....#...#...#
                                      ###s###############...#.......#...####s####.s.....s...#...#
                                      #...#.......#.....s...#.......s...#.......#.#.....#...#...#
                                      #...#.......#######s###.......##########s##.###s###...s...#
                                      #...#.......s...s...#.#.......#.s...#.....#.#...#.#...#...#
                                      #...#.......#...#...s.###########...#.....#.#...#.#########
                                      #...#.......#...#...#...........#...#.....#.#...#.........#
                                      #...#.......######s##.###s#####.#...#.....#.#...#.#######.#
                                      #...s.......#.......#.#.......#.s...#.....#.#...s.#.....#.#
                                      ########s####.#.#.#.#.#.......#.#...###s###.#######.....s.#
                                      #...........#.#.#.#.#.#.......#.#...s.....#.......#.....#.#
                                      ###########################################################";

             IMapCreationStrategy<Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy<Map>( mapRepresentation );
             IMap map = Map.Create( mapCreationStrategy );
             GoalMap goalMap = new GoalMap( map );
             goalMap.AddGoal( 51, 31, 0 );
             goalMap.AddGoal( 51, 33, 0 );
             string expectedPath = ".....s.....s.......s...........s.....s..........s....s.......s...........s...s....s..............s.....s...s......s...s....s..s....s.....s..............s......";

             List<Point> path = goalMap.FindPath( 23, 7, new List<Point>() );
             var actualPath = new StringBuilder();
             foreach( Point p in path )
             {
            actualPath.Append( map.GetCell( p.X, p.Y ).ToString() );
             }

             Assert.AreEqual( expectedPath, actualPath.ToString() );
        }
Пример #10
0
        public void FindPath_SmallMapWithTwoGoalsAndTwoObstacles_FindsBestPath()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #......#
                                      ########";
             IMapCreationStrategy<Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy<Map>( mapRepresentation );
             IMap map = Map.Create( mapCreationStrategy );

             GoalMap goalMap = new GoalMap( map );
             goalMap.AddGoal( 1, 1, 0 );
             goalMap.AddGoal( 6, 1, 0 );
             List<Point> obstacles = new List<Point> { new Point( 1, 2 ), new Point( 3, 2 ) };
             List<Point> path = goalMap.FindPath( 3, 4, obstacles );

             Assert.AreEqual( 6, path.Count );
             Assert.AreEqual( new Point( 2, 4 ), path[1] );
        }
Пример #11
0
        public void FindPath_SmallMapAfterAddingAndClearingGoals_PathHasOnePointAndAllCellsAreMax()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #......#
                                      ########";
             IMapCreationStrategy<Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy<Map>( mapRepresentation );
             IMap map = Map.Create( mapCreationStrategy );

             GoalMap goalMap = new GoalMap( map );
             goalMap.AddGoal( 1, 1, 0 );
             goalMap.AddGoal( 6, 1, 0 );
             goalMap.ClearGoals();
             List<Point> obstacles = new List<Point> { new Point( 1, 2 ), new Point( 3, 2 ) };
             List<Point> path = goalMap.FindPath( 3, 4, obstacles );

             Assert.AreEqual( 1, path.Count );
             Assert.AreEqual( new Point( 3, 4 ), path[0] );
             string expectedGoalMapRepresentation = @"#    #    #    #    #    #    #    #
                                                  #   48   48   48   48    #   48    #
                                                  #   48    #   48   48    #   48    #
                                                  #   48    #   48   48    #   48    #
                                                  #   48   48   48   48   48   48    #
                                                  #    #    #    #    #    #    #    #";
             Assert.AreEqual( expectedGoalMapRepresentation.Replace( " ", string.Empty ), goalMap.ToString().Replace( " ", string.Empty ) );
        }