public TownMap GenerateTownMapByRoutes(List <Route> routes)
        {
            var townMap = new TownMap
            {
                Towns = new Dictionary <string, Town>()
            };

            if (!routes.Any())
            {
                throw new ArgumentException("Routes can not be empty");
            }

            foreach (var route in routes)
            {
                if (!townMap.Towns.ContainsKey(route.startTownId))
                {
                    townMap.Towns.Add(route.startTownId, new Town {
                        Id = route.startTownId, RouteMap = new Dictionary <string, double>()
                    });
                }

                if (!townMap.Towns.ContainsKey(route.destinationTownId))
                {
                    townMap.Towns.Add(route.destinationTownId, new Town {
                        Id = route.destinationTownId, RouteMap = new Dictionary <string, double>()
                    });
                }

                townMap.Towns[route.startTownId].RouteMap.Add(route.destinationTownId, route.distance);
            }

            return(townMap);
        }
Example #2
0
    static void Main(string[] args)
    {
        BenderBot bot;
        TownMap   townMap;
        string    movesHistory  = "";
        bool      gameIsRunning = true;

        string[] inputs   = Console.ReadLine().Split(' ');
        int      lines    = int.Parse(inputs[0]);
        int      collumns = int.Parse(inputs[1]);

        // populating map
        int startCoordX = 0;
        int startCoordY = 0;

        townMap = new TownMap(collumns, lines);
        for (int i = 0; i < lines; i++)
        {
            string row = Console.ReadLine();
            for (int j = 0; j < collumns; j++)
            {
                char tile = row[j];
                townMap.Tiles[j, i] = tile;
                if (tile == '@')
                {
                    startCoordX = j;
                    startCoordY = i;
                }
            }
        }

        //Console.Error.WriteLine("Bot started: " + startCoordX + " " + startCoordY);
        bot = new BenderBot(startCoordX, startCoordY, townMap);

        // main loop
        while (gameIsRunning)
        {
            string latestMove = bot.Move();
            if (latestMove == "LOOP")
            {
                movesHistory  = latestMove;
                gameIsRunning = false;
            }
            else
            {
                movesHistory += latestMove + System.Environment.NewLine;
            }
            gameIsRunning = gameIsRunning && !bot.Dead;
        }

        Console.WriteLine(movesHistory);
    }
Example #3
0
    public BenderBot(int coordX, int coordY, TownMap townMap)
    {
        this.coordX  = coordX;
        this.coordY  = coordY;
        this.townMap = townMap;
        direction    = Direction.SOUTH;

        invertedMode = false;
        breakerMode  = false;
        Dead         = false;

        botHistory = new HashSet <BenderBotState>();
    }
Example #4
0
        private bool RunTownMap(EncounterProg encounter, PlayerToken player, TownMap townMap, TownMapTile currentTownMapTile, ExitTile endTile)
        {
            endTile.Position[0] = 1;
            endTile.Position[1] = 1;

            player.Position[0] = 1;
            player.Position[1] = 2;

            townMap.SetEntityPosition(player);
            int[] exitTilePos = new int[2] {
                1, 1
            };
            townMap.SetTilePosition(exitTilePos, ExitTile.Value);

            townMap.BuildMapDisplay();

            List <ICharacter> townEntityPile = new List <ICharacter>();

            townEntityPile.Add(player);

            List <Tile> townSpecialTilePile = new List <Tile>()
            {
                endTile,
                new InnTile()
                {
                    Position = townMap.FindPosition(InnTile.Value)
                }
            };

            AddEntitiesAndTilesToMap(townMap, townSpecialTilePile, townEntityPile);

            var song = new Playable(new MusicPlayer(), Track.Town);

            song.Play();

            bool nextMap = RunMapGameLoop(townMap, townEntityPile, townSpecialTilePile, encounter);

            ClearMap(townMap, townEntityPile);

            song.Stop();
            player.Position[0] = currentTownMapTile.Position[0];
            player.Position[1] = currentTownMapTile.Position[1] + 1;



            return(nextMap);
        }
Example #5
0
        public static void Main()
        {
            // This must be the exact name of the bitmap font file we are using or it will error.
            string fontFileName = "terminal8x8.png";
            // The title will appear at the top of the console window
            string consoleTitle = "Hunter v0.0";


            // Tell RLNet to use the bitmap font that we specified and that each tile is 8 x 8 pixels
            _rootConsole = new RLRootConsole(fontFileName, _screenWidth, _screenHeight,
                                             8, 8, 1f, consoleTitle);

            // Initialize the sub consoles that we will Blit to the root console
            _mapConsole     = new RLConsole(_mapWidth, _mapHeight);
            _messageConsole = new RLConsole(_messageWidth, _messageHeight);
            _statConsole    = new RLConsole(_statWidth, _statHeight);

            Player           = new Player();
            CommandSystem    = new CommandSystem();
            QuestMenu        = new QuestMenu(_menuWidth, _menuHeight);
            DeathScreen      = new DeathScreen(_menuWidth, _menuHeight);
            WinMenu          = new WinMenu(_menuWidth, _menuHeight);
            Menu             = new Menu(_menuWidth, _menuHeight);
            SchedulingSystem = new SchedulingSystem();

            // Create a new MessageLog and print the random seed used to generate the level
            MessageLog = new MessageLog();
            MessageLog.Add("The rogue arrives on level 1");
            MessageLog.Add("Prepare to fight for your life");

            //Generate the map
            TownMap mapCreation = new TownMap(_mapWidth, _mapHeight);

            //SimpleBsp mapCreation = new SimpleBsp(_mapWidth, _mapHeight);
            //FullRoomBsp mapCreation = new FullRoomBsp(_mapWidth, _mapHeight);

            DungeonMap = mapCreation.CreateMap();
            DungeonMap.UpdatePlayerFieldOfView();


            // Set up a handler for RLNET's Update event
            _rootConsole.Update += OnRootConsoleUpdate;
            // Set up a handler for RLNET's Render event
            _rootConsole.Render += OnRootConsoleRender;
            // Begin RLNET's game loop
            _rootConsole.Run();
        }
        public double GetRouteDistance(string route, TownMap townMap)
        {
            if (townMap.Towns == null)
            {
                throw new ArgumentException("No such route");
            }

            if (string.IsNullOrWhiteSpace(route) || townMap.Towns == null)
            {
                throw new ArgumentException("No such route");
            }

            var stops = route.Split('-');

            if (stops.Count() <= 1)
            {
                throw new ArgumentException("No such route");
            }

            var distance = 0D;
            var count    = 0;

            foreach (var stop in stops)
            {
                // return distance on second to last stop because you add the distance for the last stop in the in the previous loop
                if (count == stops.Count() - 1)
                {
                    return(distance);
                }

                try
                {
                    // get the current town, then get the distance to the next town from current town and add it to the total
                    var town = townMap.Towns[stop];
                    distance += town.RouteMap[stops[count + 1]];
                    count++;
                }
                catch (Exception ex)
                {
                    //Log ex
                    throw new ArgumentException("No such route");
                }
            }

            throw new ArgumentException("No such route");
        }
        public int GetNumberOfRoutesBetweenTownsByStop(string startTownId, string destinationTownId, TownMap townMap, int stopCount, LimitType limitType)
        {
            if (string.IsNullOrWhiteSpace(startTownId) || string.IsNullOrWhiteSpace(destinationTownId) || townMap.Towns == null || stopCount <= 0)
            {
                throw new ArgumentException("No such route");
            }

            var routeCount   = 0;
            var currentTowns = new List <Town>();

            // catch if method is passed a bad starting id
            try {
                currentTowns.Add(townMap.Towns[startTownId]);
            }
            catch (Exception ex)
            {
                // Log ex
                throw new ArgumentException("No such route");
            }

            for (int i = 0; i <= stopCount; i++)
            {
                var connectingTowns = new List <Town>();
                foreach (var town in currentTowns)
                {
                    if (town.Id == destinationTownId && i != 0)
                    {
                        // depending on the limit typed passed count if current town in the destination town
                        if (limitType == LimitType.MaxOrEqual)
                        {
                            routeCount++;
                        }
                        else if (limitType == LimitType.Exact && stopCount == i)
                        {
                            routeCount++;
                        }
                        else if (limitType == LimitType.LessThen && i < stopCount)
                        {
                            routeCount++;
                        }
                    }

                    foreach (var route in town.RouteMap)
                    {
                        connectingTowns.Add(townMap.Towns[route.Key]);
                    }
                }
                currentTowns = connectingTowns;
            }

            return(routeCount);
        }
        public double GetShortestDistanceBetweenTownsById(string startTownId, string destinationTownId, TownMap townMap)
        {
            if (string.IsNullOrWhiteSpace(startTownId) || string.IsNullOrWhiteSpace(destinationTownId) || townMap.Towns == null)
            {
                throw new ArgumentException("No such route");
            }

            if (!townMap.Towns.ContainsKey(destinationTownId))
            {
                throw new ArgumentException("No such route");
            }

            var currentRoutes = new Dictionary <string, double> {
                { startTownId, 0 }
            };
            var potentialRoutes = new Dictionary <string, double> {
                { startTownId, 0 }
            };
            var destinationRoutes = new Dictionary <string, double>();

            while (true)
            {
                foreach (var route in currentRoutes)
                {
                    var town = new Town();
                    try
                    {
                        town = townMap.Towns[route.Key.Split('-').Last()];
                    }
                    catch (Exception ex)
                    {
                        // Log ex
                        throw new ArgumentException("No such route");
                    }

                    if (town.Id != destinationTownId)
                    {
                        foreach (var potentialTown in town.RouteMap)
                        {
                            var potentialRoute         = string.Join('-', route.Key, potentialTown.Key);
                            var potentialRouteDistance = GetRouteDistance(potentialRoute, townMap);

                            if (!currentRoutes.Keys.Contains(potentialRoute))
                            {
                                if (destinationRoutes.Any())
                                {
                                    if (potentialRouteDistance < destinationRoutes.Values.Min())
                                    {
                                        potentialRoutes.Add(potentialRoute, potentialRouteDistance);
                                    }
                                }
                                else
                                {
                                    potentialRoutes.Add(potentialRoute, potentialRouteDistance);
                                }
                            }

                            if (potentialTown.Key == destinationTownId && !destinationRoutes.Keys.Contains(potentialRoute))
                            {
                                destinationRoutes.Add(potentialRoute, potentialRouteDistance);
                            }
                        }
                    }
                }

                if (potentialRoutes.Count() == currentRoutes.Count())
                {
                    if (destinationRoutes.Any())
                    {
                        return(destinationRoutes.Values.Min());
                    }

                    return(0);
                }

                currentRoutes = new Dictionary <string, double>();
                foreach (var potentialRoute in potentialRoutes)
                {
                    currentRoutes.Add(potentialRoute.Key, potentialRoute.Value);
                }
            }
        }
        public int GetNumberOfRoutesBetweenTownsByDistance(string startTownId, string destinationTownId, TownMap townMap, double distance, LimitType limitType)
        {
            if (string.IsNullOrWhiteSpace(startTownId) || string.IsNullOrWhiteSpace(destinationTownId) || townMap.Towns == null || distance <= 0)
            {
                throw new ArgumentException("No such route");
            }

            var routeCount = 0;

            // create 2 lists, 1 to loop over and 1 to edit while in the loop
            var currentRoutes = new List <string> {
                startTownId
            };
            var potentialRoutes = new List <string> {
                startTownId
            };

            while (true)
            {
                foreach (var route in currentRoutes)
                {
                    var town = new Town();
                    try
                    {
                        town = townMap.Towns[route.Split('-').Last()];
                    }
                    catch (Exception ex)
                    {
                        // Log ex
                        throw new ArgumentException("No such route");
                    }

                    foreach (var potentialTown in town.RouteMap)
                    {
                        var potentialRoute         = string.Join('-', route, potentialTown.Key);
                        var potentialRouteDistance = GetRouteDistance(potentialRoute, townMap);

                        // if the potential route is within the distance limit and hasn't already been added, add it to the list of valid routes and count if it end at the destination town
                        if (potentialRouteDistance <= distance && !currentRoutes.Contains(potentialRoute))
                        {
                            potentialRoutes.Add(potentialRoute);

                            if (potentialTown.Key == destinationTownId)
                            {
                                if (limitType == LimitType.MaxOrEqual)
                                {
                                    routeCount++;
                                }
                                else if (limitType == LimitType.Exact && distance == potentialRouteDistance)
                                {
                                    routeCount++;
                                }
                                else if (limitType == LimitType.LessThen && potentialRouteDistance < distance)
                                {
                                    routeCount++;
                                }
                            }
                        }
                    }
                }

                if (potentialRoutes.Count() == currentRoutes.Count())
                {
                    return(routeCount);
                }

                // clear the current routes list and copy over the potential routes list that was modified during the last loop with the new routes and start the loop again with the new routes
                currentRoutes = new List <string>();
                currentRoutes.AddRange(potentialRoutes);
            }
        }
Example #10
0
        /// <summary>
        /// Runs the logic around the game loop
        /// </summary>
        /// <param name="difficulty">Difficulty of the game</param>
        /// <param name="newPlayer">The current player</param>
        /// <returns>Whether or not the game will continue</returns>
        public void RunMapGame()
        {
            EncounterProg encounter = new EncounterProg();

            encounter.RunCharacterCreation();

            Random randomNumberSeed = new Random();

            bool newMap = true;

            int height = 8, width = 20;

            while (newMap)
            {
                PlayerToken player = new PlayerToken(encounter);

                Console.WriteLine("Loading New Map...");
                Console.WriteLine("This may take up to 60 seconds...");

                Map     map     = new Map(width, height);
                TownMap townMap = new TownMap(width, height);
                Dictionary <int, CaveMap> caveMapStorage = new Dictionary <int, CaveMap>();
                List <Tile> specialTilePile = new List <Tile>();

                int randomSeed = randomNumberSeed.Next(_currentLevel - 1, _currentLevel + 2);
                if (_currentLevel == 1)
                {
                    randomSeed = randomNumberSeed.Next(0, 3);
                }
                else if (_currentLevel == 2)
                {
                    randomSeed = randomNumberSeed.Next(1, 3);
                }
                for (int i = 0; i < randomSeed; i++)
                {
                    double   randomWidthSeed  = randomNumberSeed.Next(2, 4);
                    double   randomHeightSeed = randomNumberSeed.Next(1, 1);
                    CaveMap  caveMap          = new CaveMap((int)(width / (randomWidthSeed)), (int)(height / (randomHeightSeed)));
                    CaveTile caveTile         = new CaveTile();
                    caveTile.AssociationNum = i + 1;
                    specialTilePile.Add(caveTile);
                    caveMapStorage.Add(i + 1, caveMap);
                }
                TownMapTile currentTownMapTile = new TownMapTile();
                ExitTile    endTile            = new ExitTile();
                endTile.Position            = new int[2];
                currentTownMapTile.Position = FindTileOrEntitySpawn.StartingPostion(map, currentTownMapTile);
                specialTilePile.Add(currentTownMapTile);
                specialTilePile.Add(endTile);


                List <ICharacter> entityPile = new List <ICharacter>();
                randomSeed = randomNumberSeed.Next(_currentLevel, _currentLevel * 5);

                bool isTownMap = true;
                bool isCaveMap = true;
                while ((isTownMap || isCaveMap) && newMap)
                {
                    newMap = RunWorldMap(encounter, player, map, specialTilePile, randomSeed, endTile, entityPile);

                    isTownMap = false;
                    isCaveMap = false;
                    foreach (Tile tile in specialTilePile)
                    {
                        if (tile.GetType() == typeof(CaveTile) && IsColliding.IsCurrentlyColliding(tile, player))
                        {
                            newMap    = RunCaveMap(encounter, player, caveMapStorage, endTile, tile);
                            isCaveMap = true;
                        }
                        else if (tile.GetType() == typeof(TownMapTile) && IsColliding.IsCurrentlyColliding(currentTownMapTile, player))
                        {
                            encounter.TownReplenish();
                            newMap    = RunTownMap(encounter, player, townMap, currentTownMapTile, endTile);
                            isTownMap = true;
                        }
                    }
                }

                width  += 10;
                height += 2;
                if (width == 100)
                {
                    newMap = false;
                }

                _currentLevel++;
            }

            Console.WriteLine("Game Over");
        }
Example #11
0
        // Event handler for RLNET's Update event
        private static void OnRootConsoleUpdate(object sender, UpdateEventArgs e)
        {
            bool       didPlayerAct = false;
            RLKeyPress keyPress     = _rootConsole.Keyboard.GetKeyPress();

            if (CommandSystem.IsPlayerTurn)
            {
                if (Globals.BuildingEntranceIsTriggered)
                {
                    if (keyPress != null)
                    {
                        if (keyPress.Key == RLKey.E)
                        {
                            CommandSystem.CloseMenu();
                            Globals.SheriffTriggered     = false;
                            Globals.GenericMenuTriggered = false;
                            didPlayerAct = true;
                        }
                        else if (keyPress.Key == RLKey.Enter)
                        {
                            //SimpleBsp mapGenerator = new SimpleBsp(_mapWidth, _mapHeight);
                            FullRoomBsp mapGenerator = new FullRoomBsp(_mapWidth, _mapHeight);
                            DungeonMap    = mapGenerator.CreateMap();
                            MessageLog    = new MessageLog();
                            CommandSystem = new CommandSystem();
                            CommandSystem.CloseMenu();
                            Globals.SheriffTriggered = false;
                            didPlayerAct             = true;
                        }
                        else if (keyPress.Key == RLKey.Escape)
                        {
                            _rootConsole.Close();
                        }
                    }
                }
                else if (Globals.IsPlayerDead)
                {
                    if (keyPress != null)
                    {
                        if (keyPress.Key == RLKey.Enter)
                        {
                            TownMap mapGenerator = new TownMap(_mapWidth, _mapHeight);
                            DungeonMap    = mapGenerator.CreateMap();
                            MessageLog    = new MessageLog();
                            CommandSystem = new CommandSystem();
                            CommandSystem.CloseMenu();
                            Player.Health        = Player.MaxHealth;
                            Globals.IsPlayerDead = false;
                            didPlayerAct         = true;
                        }
                        else if (keyPress.Key == RLKey.Escape)
                        {
                            _rootConsole.Close();
                        }
                    }
                }
                else if (Globals.IsBossDead)
                {
                    if (keyPress != null)
                    {
                        if (keyPress.Key == RLKey.Enter)
                        {
                            TownMap mapGenerator = new TownMap(_mapWidth, _mapHeight);
                            DungeonMap    = mapGenerator.CreateMap();
                            MessageLog    = new MessageLog();
                            CommandSystem = new CommandSystem();
                            CommandSystem.CloseMenu();
                            Player.Health      = Player.MaxHealth;
                            Globals.IsBossDead = false;
                            didPlayerAct       = true;
                        }
                        else if (keyPress.Key == RLKey.E)
                        {
                            CommandSystem.CloseMenu();
                            Globals.IsBossDead = false;
                            didPlayerAct       = true;
                        }
                        else if (keyPress.Key == RLKey.Escape)
                        {
                            _rootConsole.Close();
                        }
                    }
                }
                else
                {
                    if (keyPress != null)
                    {
                        if (keyPress.Key == RLKey.Up)
                        {
                            didPlayerAct = CommandSystem.MovePlayer(Direction.Up);
                        }
                        else if (keyPress.Key == RLKey.Down)
                        {
                            didPlayerAct = CommandSystem.MovePlayer(Direction.Down);
                        }
                        else if (keyPress.Key == RLKey.Left)
                        {
                            didPlayerAct = CommandSystem.MovePlayer(Direction.Left);
                        }
                        else if (keyPress.Key == RLKey.Right)
                        {
                            didPlayerAct = CommandSystem.MovePlayer(Direction.Right);
                        }
                        else if (keyPress.Key == RLKey.Comma)
                        {
                            //if (DungeonMap.CanMoveDownToNextLevel())
                            //{
                            TownMap mapGenerator = new TownMap(_mapWidth, _mapHeight);
                            DungeonMap    = mapGenerator.CreateMap();
                            MessageLog    = new MessageLog();
                            CommandSystem = new CommandSystem();
                            Player.Health = Player.MaxHealth;
                            didPlayerAct  = true;
                            //}
                        }
                        else if (keyPress.Key == RLKey.Escape)
                        {
                            _rootConsole.Close();
                        }
                    }
                }
                if (didPlayerAct)
                {
                    _renderRequired = true;
                    CommandSystem.EndPlayerTurn();
                }
            }
            else
            {
                CommandSystem.ActivateMonsters();
                _renderRequired = true;
            }
        }