Example #1
0
        public void TryFindPaths_SmallMapWithTwoGoalsOfEqualWeightAndDistance_Finds2PathsWith6Points()
        {
            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);
            ReadOnlyCollection <Path> paths = goalMap.TryFindPaths(3, 4);

            string expectedGoalMapRepresentation = @"#    #    #    #    #    #    #    #
                                                  #    0    1    2    3    #    0    #
                                                  #    1    #    3    4    #    1    #
                                                  #    2    #    4    5    #    2    #
                                                  #    3    4    5    5    4    3    #
                                                  #    #    #    #    #    #    #    #";

            Assert.AreEqual(RemoveWhiteSpace(expectedGoalMapRepresentation), RemoveWhiteSpace(goalMap.ToString()));
            Assert.AreEqual(2, paths.Count);
            Assert.AreEqual(6, paths[0].Length);
            Assert.AreEqual(6, paths[1].Length);
        }
Example #2
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);
        }
Example #3
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));
        }
Example #4
0
        public void FindAllPathsToAllGoals_SmallMapWithTwoGoals_Finds2PathsWith6Points()
        {
            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 <List <Point> > paths = goalMap.FindAllPathsToAllGoals(3, 4);

            string expectedGoalMapRepresentation = @"#    #    #    #    #    #    #    #
                                                  #    0    1    2    3    #    0    #
                                                  #    1    #    3    4    #    1    #
                                                  #    2    #    4    5    #    2    #
                                                  #    3    4    5    5    4    3    #
                                                  #    #    #    #    #    #    #    #";

            Assert.AreEqual(expectedGoalMapRepresentation.Replace(" ", string.Empty), goalMap.ToString().Replace(" ", string.Empty));
            Assert.AreEqual(2, paths.Count);
            Assert.AreEqual(6, paths[0].Count);
            Assert.AreEqual(6, paths[1].Count);
        }
Example #5
0
        public void FindAllPathsToAllGoals_SmallMapWithTwoGoals_Finds2PathsWith6Points()
        {
            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<List<Point>> paths = goalMap.FindAllPathsToAllGoals( 3, 4 );

             string expectedGoalMapRepresentation = @"#    #    #    #    #    #    #    #
                                                  #    0    1    2    3    #    0    #
                                                  #    1    #    3    4    #    1    #
                                                  #    2    #    4    5    #    2    #
                                                  #    3    4    5    5    4    3    #
                                                  #    #    #    #    #    #    #    #";
             Assert.AreEqual( expectedGoalMapRepresentation.Replace( " ", string.Empty ), goalMap.ToString().Replace( " ", string.Empty ) );
             Assert.AreEqual( 2, paths.Count );
             Assert.AreEqual( 6, paths[0].Count );
             Assert.AreEqual( 6, paths[1].Count );
        }
Example #6
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());
        }
Example #7
0
        public void TryFindPath_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.TryFindPath(23, 7);
            var  actualPath = new StringBuilder();

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

            Assert.AreEqual(expectedPath, actualPath.ToString());
        }
Example #8
0
        public void FindPathAvoiding_BoxedInCornerWithObstacle_ExpectedPath()
        {
            string mapRepresentation = @"###########
                                      #.....#...#
                                      #.....#.#.#
                                      #.....#.#.#
                                      #.....#.#.#
                                      #.....s.#.#
                                      ###########";

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

            goalMap.AddGoal(2, 2, 0);
            Point  obstacle     = new Point(2, 2);
            string expectedPath = "........s...........";

            List <Point> path = goalMap.FindPathAvoidingGoals(1, 2, new List <Point> {
                obstacle
            });
            var actualPath = new StringBuilder();

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

            Assert.AreEqual(expectedPath, actualPath.ToString());
        }
Example #9
0
        public void FindPathAvoiding_BoxedInCornerWithObstacle_ExpectedPath()
        {
            string mapRepresentation = @"###########
                                      #.....#...#
                                      #.....#.#.#
                                      #.....#.#.#
                                      #.....#.#.#
                                      #.....s.#.#
                                      ###########";

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

             List<Point> path = goalMap.FindPathAvoidingGoals( 1, 2, new List<Point> { obstacle } );
             var actualPath = new StringBuilder();
             foreach( Point p in path )
             {
            actualPath.Append( map.GetCell( p.X, p.Y ).ToString() );
             }

             Assert.AreEqual( expectedPath, actualPath.ToString() );
        }
Example #10
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);
        }
Example #11
0
        public void TryFindPath_SmallMapAfterAddingAndClearingGoals_ReturnsNull()
        {
            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();

            Path path = goalMap.TryFindPath(3, 4);

            Assert.AreEqual(null, path);
        }
Example #12
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]);
        }
Example #13
0
        public bool Act()
        {
            var   ac        = parent.GetComponent <Actor>(ComponentType.Actor);
            var   pos       = parent.GetComponent <SpriteAnimation>(ComponentType.SpriteAnimation).Position;
            IMap  map       = ac.Map.MapData.Map;
            Point playerPos = ac.Map.CameraFollow.GetComponent <SpriteAnimation>(ComponentType.SpriteAnimation).Position;

            ac.Map.MapData.SetIsWalkable(pos.X, pos.Y, true);
            ac.Map.MapData.SetIsWalkable(playerPos.X, playerPos.Y, true);

            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(playerPos.X, playerPos.Y, 0);

            Path path = null;

            try
            {
                path = goalMap.FindPathAvoidingGoals(pos.X, pos.Y);
            }
            catch (PathNotFoundException)
            {
                System.Console.WriteLine("RunAway - PathNotFoundException");
                if (parent.logger != null)
                {
                    parent.logger.Write(string.Format("{0}, Finds itself cornered.", parent.NAME));
                }
            }
            ac.Map.MapData.SetIsWalkable(pos.X, pos.Y, false);
            ac.Map.MapData.SetIsWalkable(playerPos.X, playerPos.Y, false);

            if (path != null)
            {
                try
                {
                    ac.Sprite.Move(path.StepForward());
                }
                catch (NoMoreStepsException)
                {
                    System.Console.WriteLine("RunAway - NoMoreStepsException");
                    if (parent.logger != null)
                    {
                        parent.logger.Write(string.Format("{0}, Has nowhere to run.", parent.NAME));
                    }
                }
            }
            return(true);
        }
Example #14
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);
        }
Example #15
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);
        }
Example #16
0
        private GoalMap CreateGoalMap()
        {
            var goalMap = new GoalMap(map.GetIMap());

            // Aim for our random goal
            goalMap.AddGoal(this.goal.X, this.goal.Y, 100);
            // Avoid ANYTHING that's solid
            foreach (var e in map.Objects)
            {
                if (e.IsSolid && e != this)
                {
                    goalMap.AddObstacle(e.X, e.Y);
                }
            }

            return(goalMap);
        }
Example #17
0
        public void TryFindPath_DestinationNotWalkableAnd13StepsAway_ReturnsNull()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #......#
                                      ########";
            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap    map     = Map.Create(mapCreationStrategy);
            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(7, 1, 0);

            Path path = goalMap.TryFindPath(1, 1);

            Assert.AreEqual(null, path);
        }
Example #18
0
        public void TryFindPath_DestinationUnreachable_ReturnsNull()
        {
            string mapRepresentation = @"########
                                      #....#.#
                                      #.#..#.#
                                      #.#..#.#
                                      #....#.#
                                      ########";
            IMapCreationStrategy <Map> mapCreationStrategy = new StringDeserializeMapCreationStrategy <Map>(mapRepresentation);
            IMap    map     = Map.Create(mapCreationStrategy);
            GoalMap goalMap = new GoalMap(map);

            goalMap.AddGoal(6, 1, 0);

            var paths = goalMap.TryFindPath(1, 1);

            Assert.AreEqual(null, paths);
        }
Example #19
0
        public bool Act(Monster monster, CommandSystem commandSystem)
        {
            DungeonMap dungeonMap = Game.DungeonMap;
            Player     player     = Game.Player;

            // Set the cells the monster and player are on to walkable so the pathfinder doesn't bail early
            dungeonMap.SetIsWalkable(monster.X, monster.Y, true);
            dungeonMap.SetIsWalkable(player.X, player.Y, true);

            GoalMap goalMap = new GoalMap(dungeonMap);

            goalMap.AddGoal(player.X, player.Y, 0);

            Path path = null;

            try
            {
                path = goalMap.FindPathAvoidingGoals(monster.X, monster.Y);
            }
            catch (PathNotFoundException)
            {
                Game.MessageLog.Add($"{monster.Name} cowers in fear");
            }


            // Reset the cell the monster and player are on  back to not walkable
            dungeonMap.SetIsWalkable(monster.X, monster.Y, false);
            dungeonMap.SetIsWalkable(player.X, player.Y, false);

            if (path != null)
            {
                try
                {
                    commandSystem.MoveMonster(monster, path.StepForward());
                }
                catch (NoMoreStepsException)
                {
                    Game.MessageLog.Add($"{monster.Name} cowers in fear");
                }
            }

            return(true);
        }
Example #20
0
        public void FindPathAvoiding_NoPathExists_ThrowsPathNotFoundException()
        {
            string mapRepresentation = @"###############################################
                                      #..........#......................##..........#
                                      #..........#..........##..........##..........#
                                      #..........#..........##..........##..........#
                                      #..........#..........##..........##..........#
                                      #.....................##......................#
                                      ###############################################";

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

            goalMap.AddGoal(2, 2, 0);
            goalMap.AddObstacle(2, 1);
            goalMap.AddObstacle(1, 2);

            Path path = goalMap.FindPathAvoidingGoals(1, 1);
        }
Example #21
0
        public bool Act(Monster monster, CommandSystem commandSystem)
        {
            DungeonMap dungeonMap = Game.DungeonMap;
            Player     player     = Game.Player;

            dungeonMap.SetIsWalkable(monster.X, monster.Y, true);
            dungeonMap.SetIsWalkable(player.X, player.Y, true);

            GoalMap goalMap = new GoalMap(dungeonMap);

            goalMap.AddGoal(player.X, player.Y, 0);

            Path path = null;

            try
            {
                path = goalMap.FindPathAvoidingGoals(monster.X, monster.Y);
            }
            catch (PathNotFoundException)
            {
                Game.MessageLog.Add($"{monster.Name} cowers in fear");
            }

            dungeonMap.SetIsWalkable(monster.X, monster.Y, false);
            dungeonMap.SetIsWalkable(player.X, player.Y, false);

            if (path != null)
            {
                try
                {
                    commandSystem.MoveMonster(monster, path.StepForward());
                }
                catch (NoMoreStepsException)
                {
                    Game.MessageLog.Add($"{monster.Name} cowers in fear");
                }
            }

            return(true);
        }
Example #22
0
        public void TryFindPathAvoiding_NoPathExists_ReturnsNull()
        {
            string mapRepresentation = @"###############################################
                                      #..........#......................##..........#
                                      #..........#..........##..........##..........#
                                      #..........#..........##..........##..........#
                                      #..........#..........##..........##..........#
                                      #.....................##......................#
                                      ###############################################";

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

            goalMap.AddGoal(2, 2, 0);
            goalMap.AddObstacle(2, 1);
            goalMap.AddObstacle(1, 1);

            Path path = goalMap.TryFindPathAvoidingGoals(1, 1);

            Assert.AreEqual(null, path);
        }
Example #23
0
        public void TryFindPathAvoiding_BoxedInCornerWithObstacle_ExpectedPath()
        {
            string mapRepresentation = @"###############################################
                                      #..........#......................##..........#
                                      #..........#..........##..........##..........#
                                      #..........#..........##..........##..........#
                                      #..........#..........##..........##..........#
                                      #.....................##......................#
                                      ###############################################";

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

            goalMap.AddGoal(2, 2, 0);
            goalMap.AddObstacle(2, 1);

            Path path = goalMap.TryFindPathAvoidingGoals(1, 1);

            Assert.AreEqual(61, path.Length);
            Assert.AreEqual(map.GetCell(1, 2), path.StepForward());
            Assert.AreEqual(map.GetCell(1, 1), path.Start);
            Assert.AreEqual(map.GetCell(45, 1), path.End);
        }
Example #24
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 ) );
        }
Example #25
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] );
        }
Example #26
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() );
        }