Esempio n. 1
0
        public Command GetCommand()
        {
            // Variable updates
            meTile = game.Board[me.Position];

            // Pretend every other player places a bomb.
            foreach (var player in game.Players.Values.Where(x => !x.Dead && x != me))
            {
                Bomb bomb = new Bomb(player, 6);
                game.Bombs.Add(bomb);
                game.Board[bomb.Position].Bombs.Add(bomb);
                bomb.AddTheatToBoard(game.Board);
            }

            // Now calculate bomb chain reactions
            game.CalculateBombChainReactions();

            // If this tile is safe, stay.
            if (meTile.ThreatheningBombs.Count == 0)
            {
                return(Command.Pass);
            }

            // Find safe tiles and order them by distance
            var safeTiles = areafinder
                            .FindTiles(meTile, int.MaxValue, x => x == meTile || x.Walkable)
                            .Where(x => x.ThreatheningBombs.Count == 0)
                            .OrderBy(x => Position.Distance(x.Position, meTile.Position));

            // See if the path towards that tile is safe
            foreach (var safeTile in safeTiles)
            {
                var path = pathfinder.FindPath(meTile, safeTile);
                if (path != null && path.Count > 1 && IsPathSafe(path))
                {
                    return(MoveTowardsTile(path[1]));
                }
            }

            return(Command.Pass);
        }
Esempio n. 2
0
        /// <summary>
        /// Determines if it's considered safe to place a bomb
        /// at the current tile.
        /// </summary>
        /// <returns>
        /// Whether it is safe to place a bomb here.
        /// </returns>
        private bool ItIsSafeToPlaceABombHere()
        {
            // If we place a bomb here, can we find a safe spot then?
            bool safety = false;

            // We gotta calculate bomb chain reactions, so save every previous tick.
            Dictionary <Bomb, int> actuallBombTicks = new Dictionary <Bomb, int>();

            foreach (Bomb b in game.Bombs)
            {
                actuallBombTicks.Add(b, b.Ticks);
            }

            // Simulate a bomb placement of all players
            List <Bomb> simulatingBombs = new List <Bomb>();

            foreach (Player player in game.Players.Values.Where(x => !x.Dead))
            {
                Bomb bomb = new Bomb(player, 5);
                game.Bombs.Add(bomb);
                currentTile.Bombs.Add(bomb);
                bomb.AddTheatToBoard(game.Board);
                simulatingBombs.Add(bomb);
            }

            // Now we can perform the calculation
            CalculateBombChainReactions();

            // Now find a safe spot!
            var tiles = areafinder.FindTiles(
                currentTile, 4,
                x => x == currentTile || x.Walkable
                ).Where(x => x.ThreatheningBombs.Count == 0 && x != currentTile).ToList();

            // Is there a safe spot?
            if (tiles.Any())
            {
                // Is the path to any of that tiles safe? (test only four)
                foreach (var tile in tiles.Take(4))
                {
                    IList <Tile> path = pathfinder.FindPath(currentTile, tile);
                    if (IsPathSafe(path, -1))
                    {
                        safety = true;
                        break;
                    }
                }
            }

            // Reverse the changes done by our simulation
            foreach (Bomb bomb in simulatingBombs)
            {
                currentTile.Bombs.Remove(bomb);
                game.Bombs.Remove(bomb);
            }

            foreach (Tile t in game.Board.Tiles())
            {
                t.ThreatheningBombs.RemoveAll(x => simulatingBombs.Contains(x));
            }
            foreach (var kvp in actuallBombTicks)
            {
                kvp.Key.Ticks = kvp.Value;
            }

            // No safe spot, it's not safe to place a bomb here.
            return(safety);
        }