Beispiel #1
0
        private static void BuildBuilding(Match game, Player player, Building building)
        {
            var deltaY = building.PlayerNumber == 1 ? 1 : -1;
            var ship = player.Ship;

            if (building.LivesCost > player.Lives)
            {
                throw new NotEnoughLivesException();
            }

            building.X = ship.X;
            building.Y = ship.Y + deltaY;
            game.Map.AddEntity(building);
            player.Lives -= building.LivesCost;
        }
Beispiel #2
0
        private static void BuildAtX(Match game, Player player, int x)
        {
            var ship = player.Ship;
            var deltaY = player.PlayerNumber == 1 ? -1 : 1;
            List<Alien> explosions = new List<Alien>();

            for (var shieldX = x; shieldX < x + ship.Width; shieldX++)
            {
                var shieldY = ship.Y + deltaY;
                for (var counter = 0; counter < 3; counter++)
                {
                    var entity = game.Map.GetEntity(shieldX, shieldY);
                    if (entity == null)
                    {
                        var shield = new Shield(player.PlayerNumber) { X = shieldX, Y = shieldY };

                        game.Map.AddEntity(shield);

                        //test
                        Match.GetInstance().Map.AddTempEntity(new NodeEntity(Match.GetInstance().Depth,shield ));
                    }
                    else if (entity.Type == EntityType.Alien)
                    {
                        explosions.Add((Alien)entity);
                    }
                    else if ((entity.Type == EntityType.Bullet) ||
                             (entity.Type == EntityType.Missile))
                    {
                        Match.GetInstance().Map.AddKillEntity(new NodeEntity(Match.GetInstance().Depth, entity, hasMoved: false, isExplosion: true));
                        entity.Destroy();
                    }

                    shieldY += deltaY;
                }
            }

            foreach (var alien in explosions)
            {
                Alien.Explode(alien.X, alien.Y);
            }
        }
Beispiel #3
0
        private static bool ShipKanBuildFactory(Match game, Ship ship, int playerNumber)
        {
            var player = game.GetPlayer(playerNumber);

            if (player.Lives < Settings.Default.AlienFactoryCost || player.AlienFactory != null) return false;

            var targetX = ship.X;
            var targetY = (playerNumber == 1) ? ship.Y + 1 : ship.Y - 1;

            for (var x = targetX; x < targetX + ship.Width; x++)
            {
                var mapEntity = Match.GetInstance().Map.GetEntity(x, targetY);
                if (mapEntity != null)
                {
                    return false;
                }
            }
            return true;
        }
Beispiel #4
0
 private static int PlayerMissileCount(Match game, int playerNumber)
 {
     return game.GetPlayer(playerNumber).Missiles.Count;
 }
Beispiel #5
0
        public static Match GetInstance()
        {
            if (_instance != null) return _instance;

            _instance = new Match();
            _instance.StartNewGame();
            return _instance;
        }
Beispiel #6
0
        //kankill
        public static bool ShipKanKill(Match game, Ship ship, int playerNumber)
        {
            var player = game.GetPlayer(playerNumber);
            if (player.Missiles.Count >= player.MissileLimit) return false;

            var alienManager = game.GetPlayer((playerNumber == 1) ? 2 : 1).AlienManager;
            var waves = alienManager.Waves;

            if (waves.Count <= 0) return false;

            var shieldAX = ship.X + 1;
            var shieldAY = (playerNumber == 1) ? ship.Y - 1 : ship.Y + 1;

            var shieldA = game.Map.GetEntity(shieldAX, shieldAY);

            if (shieldA != null && shieldA.GetType() == typeof(Shield))
            {
                var bulletAY = (playerNumber == 1) ? ship.Y - 2 : ship.Y + 2;

                var bulletA = game.Map.GetEntity(shieldAX, bulletAY);

                if (bulletA == null
                    || !((bulletA.PlayerNumber != playerNumber)
                    && (bulletA.GetType() == typeof(Bullet)
                    || bulletA.GetType() == typeof(Missile))))
                {
                    return false;
                }
            }

            var shieldBY = (playerNumber == 1) ? ship.Y - 2 : ship.Y + 2;

            var shieldB = game.Map.GetEntity(shieldAX, shieldBY);

            var deltaX = alienManager.DeltaX;

            if (shieldB != null && shieldB.GetType() == typeof(Shield))
            {
                var bulletBY = (playerNumber == 1) ? ship.Y - 3 : ship.Y + 3;

                var bulletB = game.Map.GetEntity(shieldAX, bulletBY);

                if (bulletB == null
                    || !((bulletB.PlayerNumber != playerNumber)
                    && (bulletB.GetType() == typeof(Bullet)
                    || bulletB.GetType() == typeof(Missile))))
                {
                    if (bulletB == null)
                    {
                        //stat
                        if (!AlienAreMovingForward(waves, deltaX))
                        {
                            if (deltaX == 1)
                            {
                                var alienX = ship.X;
                                var alien = game.Map.GetEntity(alienX, shieldBY);

                                if (alien != null && alien.GetType() != typeof(Alien))
                                {
                                    return false;
                                }
                            }
                            else
                            {
                                var alienX = ship.X + 2;
                                var alien = game.Map.GetEntity(alienX, shieldBY);

                                if (alien != null && alien.GetType() != typeof(Alien))
                                {
                                    return false;
                                }
                            }
                        }
                        //end
                    }

                    if (bulletB != null && bulletB.GetType() == typeof(Alien))
                    {
                        if (!AlienAreMovingForward(waves, deltaX)) return false;
                    }
                }
            }

            //shield @ Y-3 or Y+3
            var shieldCY = (playerNumber == 1) ? ship.Y - 3 : ship.Y + 3;

            var shieldC = game.Map.GetEntity(shieldAX, shieldCY);

            if (shieldC != null && shieldC.GetType() == typeof(Shield))
            {
                var bulletCY = (playerNumber == 1) ? ship.Y - 4 : ship.Y + 4;

                var bulletC = game.Map.GetEntity(shieldAX, bulletCY);

                if (bulletC == null
                  || !((bulletC.PlayerNumber != playerNumber)
                  && (bulletC.GetType() == typeof(Bullet)
                  || bulletC.GetType() == typeof(Missile))))
                {
                    var bulletDY = (playerNumber == 1) ? ship.Y - 5 : ship.Y + 5;
                    var bulletD = game.Map.GetEntity(shieldAX, bulletDY);

                    if (bulletD == null
                       || !((bulletD.PlayerNumber != playerNumber)
                       && (bulletC.GetType() == typeof(Bullet)
                       || bulletC.GetType() == typeof(Missile))))
                    {
                        if (!AlienAreMovingForward(waves, deltaX))
                        {
                            if (deltaX == 1)
                            {
                                var alienAX = ship.X;

                                var alienA = game.Map.GetEntity(alienAX, shieldCY);

                                if (alienA == null || alienA.GetType() != typeof(Alien))
                                {
                                    if (ship.X <= 1) return false;

                                    var alienBX = ship.X - 1;

                                    var alienB = game.Map.GetEntity(alienBX, shieldCY);

                                    if (alienB == null || alienB.GetType() != typeof(Alien))
                                    {
                                        return false;
                                    }

                                    var dis = shieldAX - alienBX;

                                    var rightMostAlienX = FindRightMostAlienX(waves);
                                    var wally = game.Map.Width - 2;
                                    var maxDis = wally - rightMostAlienX;

                                    if (dis > maxDis) return false;
                                }
                            }
                            else
                            {
                                var alienAX = ship.X + 2;

                                var alienA = game.Map.GetEntity(alienAX, shieldCY);

                                if (alienA == null || alienA.GetType() != typeof(Alien))
                                {
                                    if (ship.X > (game.Map.Width - 5)) return false;

                                    var alienBX = ship.X + 3;
                                    var alienB = game.Map.GetEntity(alienBX, shieldCY);
                                    if (alienB == null || alienB.GetType() != typeof(Alien))
                                    {
                                        return false;
                                    }
                                    var dis = alienBX - shieldAX;
                                    var leftMostAlienX = FindLeftMostAlienX(waves);
                                    var maxDis = leftMostAlienX - 1;

                                    if (dis > maxDis) return false;
                                }
                            }
                        }
                    }
                    if (bulletC != null && bulletC.GetType() == typeof(Alien))
                    {
                        if (!AlienAreMovingForward(waves, deltaX)) return false;
                    }
                }
            }

            var adjustY = (playerNumber == 1) ? 1 : -1;

            var missileX = ship.X + 1;
            var missileY = (playerNumber == 1) ? ship.Y - 1 : ship.Y + 1;

            int disX;
            int disY;

            if (AlienAreMovingForward(waves, deltaX))
            {
                if (deltaX == -1)
                {
                   foreach (var wave in waves)
                    {
                        foreach (var alien in wave)
                        {
                            if (alien.Alive)
                            {
                                disX = missileX - alien.X;
                                disY = adjustY * (missileY - alien.Y);
                                if (disY == (disX + 2))
                                {
                                    var maxX = game.Map.Width - 2;
                                    var rightMostAlienX = FindRightMostAlienX(waves);
                                    var maxDisX = maxX - rightMostAlienX;
                                    if (disX <= maxDisX) return true;
                                }
                            }
                        }
                    }
                }
                else
                {
                    foreach (var wave in waves)
                    {
                        foreach (var alien in wave)
                        {
                            if (alien.Alive)
                            {
                                disX = alien.X - missileX;
                                disY = adjustY * (missileY - alien.Y);
                                if (disY == (disX + 2))
                                {
                                    var minX = 1;
                                    var leftMostAlienX = FindLeftMostAlienX(waves);
                                    var maxDisX = leftMostAlienX - minX;
                                    if (disX <= maxDisX) return true;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                if (deltaX == 1)
                {
                    //1-way
                    foreach (var wave in waves)
                    {
                        foreach (var alien in wave)
                        {
                            if (alien.Alive)
                            {
                                disX = missileX - alien.X;
                                disY = adjustY * (missileY - alien.Y);
                                if (disX == disY)
                                {
                                    var maxX = game.Map.Width - 2;
                                    var righMostAlienX = FindRightMostAlienX(waves);
                                    var maxDisX = maxX - righMostAlienX;
                                    if (disX <= maxDisX) return true;
                                }
                            }
                        }
                    }
                    //return trip
                    foreach (var wave in waves)
                    {
                        foreach (var alien in wave)
                        {
                            if (alien.Alive)
                            {
                                var maxX = game.Map.Width - 2;
                                var rightMostAlienX = FindRightMostAlienX(waves);
                                var maxDisX = maxX - rightMostAlienX;
                                var newAlienX = alien.X + maxDisX;

                                if (newAlienX >= missileX)
                                {
                                    disX = maxDisX + (newAlienX - missileX);
                                    disY = adjustY * (missileY - alien.Y);
                                    if (disY == (disX + 2))
                                    {
                                       var minX = 1;
                                        var newDisX = newAlienX - missileX;
                                        var leftMostAlienX = FindLeftMostAlienX(waves);
                                        var newLeftMostAlienX = leftMostAlienX + maxDisX;
                                        var newMaxDisX = newLeftMostAlienX - minX;
                                        if (newDisX <= newMaxDisX) return true;
                                    }
                                }

                            }
                        }
                    }
                }
                else
                {
                    //1-way
                    foreach (var wave in waves)
                    {
                        foreach (var alien in wave)
                        {
                            if (alien.Alive)
                            {
                                disX = alien.X - missileX;
                                disY = adjustY * (missileY - alien.Y);
                                if (disX == disY)
                                {
                                    var minX = 1;
                                    var leftMostAlienX = FindLeftMostAlienX(waves);
                                    var maxDisX = leftMostAlienX - minX;
                                    if (disX <= maxDisX) return true;
                                }
                            }
                        }
                    }
                    //return-trip
                    foreach (var wave in waves)
                    {
                        foreach (var alien in wave)
                        {
                            if (alien.Alive)
                            {
                                var minX = 1;
                                var leftMostAlienX = FindLeftMostAlienX(waves);
                                var maxDisX = leftMostAlienX - minX;
                                var newAlienX = alien.X - maxDisX;

                                if (missileX >= newAlienX)
                                {
                                    disX = maxDisX + (missileX - newAlienX);
                                    disY = adjustY * (missileY - alien.Y);
                                    if (disY == (disX + 2))
                                    {
                                        var maxX = game.Map.Width - 2;
                                        var newDisX = missileX - newAlienX;
                                        var rightMostAlienX = FindRightMostAlienX(waves);
                                        var newRightMostAlienX = rightMostAlienX - maxDisX;
                                        var newMaxDisX = maxX - newRightMostAlienX;
                                        if (newDisX <= newMaxDisX) return true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return false;
        }
Beispiel #7
0
        //shield
        public static bool ShipMustShieldBuildingOnRight(Match game, Ship ship, int playerNumber)
        {
            var wall = (playerNumber == 1) ? game.Map.Width - 4 : 1;

            //@ Y+5/Y-5

            var newWall = (playerNumber == 1) ? wall - 2 : wall + 2;

            switch (playerNumber)
            {
                case 1:
                    if (ship.X < newWall)
                    {
                        var buildingX = ship.X + 4;
                        var buildingY = ship.Y + 1;

                        var building = game.Map.GetEntity(buildingX, buildingY);

                        if (building != null
                            && (building.GetType() == typeof(MissileController)
                            || building.GetType() == typeof(AlienFactory)))
                        {
                            var bulletEY = ship.Y - 5;
                            var bulletE = game.Map.GetEntity(buildingX, bulletEY);

                            //test bullet/missile
                            if (bulletE != null)
                            {
                                if (bulletE.PlayerNumber != playerNumber
                                    && (bulletE.GetType() == typeof(Bullet)
                                    || bulletE.GetType() == typeof(Missile)))
                                {
                                    return true;
                                }
                            }
                        }
                    }
                    break;
                case 2:
                    if (ship.X > newWall)
                    {
                        var buildingX = ship.X - 2;
                        var buildingY = ship.Y - 1;

                        var building = game.Map.GetEntity(buildingX, buildingY);

                        if (building != null
                            && (building.GetType() == typeof(MissileController)
                            || building.GetType() == typeof(AlienFactory)))
                        {
                            var bulletEY = ship.Y + 5;
                            var bulletE = game.Map.GetEntity(buildingX, bulletEY);

                            //test bullet/missile
                            if (bulletE != null)
                            {
                                if (bulletE.PlayerNumber != playerNumber
                                    && (bulletE.GetType() == typeof(Bullet)
                                    || bulletE.GetType() == typeof(Missile)))
                                {
                                    return true;
                                }
                            }

                        }
                    }
                    break;
            }

            //@ Y+6/Y-6

            newWall = (playerNumber == 1) ? wall - 3 : wall + 3;

            switch (playerNumber)
            {
                case 1:
                    if (ship.X < newWall)
                    {
                        var buildingX = ship.X + 5;
                        var buildingY = ship.Y + 1;

                        var building = game.Map.GetEntity(buildingX, buildingY);

                        if (building != null
                            && (building.GetType() == typeof(MissileController)
                            || building.GetType() == typeof(AlienFactory)))
                        {
                            var bulletEY = ship.Y - 6;
                            var bulletE = game.Map.GetEntity(buildingX, bulletEY);

                            //test bullet/missile
                            if (bulletE != null)
                            {
                                if (bulletE.PlayerNumber != playerNumber
                                   && (bulletE.GetType() == typeof(Bullet)
                                    || bulletE.GetType() == typeof(Missile)))
                                {
                                    return true;
                                }
                            }
                        }
                    }
                    break;
                case 2:
                    if (ship.X > newWall)
                    {
                        var buildingX = ship.X - 3;
                        var buildingY = ship.Y - 1;

                        var building = game.Map.GetEntity(buildingX, buildingY);

                        if (building != null
                            && (building.GetType() == typeof(MissileController)
                            || building.GetType() == typeof(AlienFactory)))
                        {
                            var bulletEY = ship.Y + 6;
                            var bulletE = game.Map.GetEntity(buildingX, bulletEY);

                            //test bullet/missile
                            if (bulletE != null)
                            {
                                if (bulletE.PlayerNumber != playerNumber
                                    && (bulletE.GetType() == typeof(Bullet)
                                    || bulletE.GetType() == typeof(Missile)))
                                {
                                    return true;
                                }
                            }

                        }
                    }
                    break;
            }

            //@ Y+7/Y-7

            newWall = (playerNumber == 1) ? wall - 4 : wall + 4;

            switch (playerNumber)
            {
                case 1:
                    if (ship.X < newWall)
                    {
                        var buildingX = ship.X + 6;
                        var buildingY = ship.Y + 1;

                        var building = game.Map.GetEntity(buildingX, buildingY);

                        if (building != null
                            && (building.GetType() == typeof(MissileController)
                            || building.GetType() == typeof(AlienFactory)))
                        {
                            var bulletEY = ship.Y - 7;
                            var bulletE = game.Map.GetEntity(buildingX, bulletEY);

                            //test bullet/missile
                            if (bulletE != null)
                            {
                                if (bulletE.PlayerNumber != playerNumber
                                    && (bulletE.GetType() == typeof(Bullet)
                                    || bulletE.GetType() == typeof(Missile)))
                                {
                                    return true;
                                }
                            }
                        }
                    }
                    break;
                case 2:
                    if (ship.X > newWall)
                    {
                        var buildingX = ship.X - 4;
                        var buildingY = ship.Y - 1;

                        var building = game.Map.GetEntity(buildingX, buildingY);

                        if (building != null
                            && (building.GetType() == typeof(MissileController)
                            || building.GetType() == typeof(AlienFactory)))
                        {
                            var bulletEY = ship.Y + 7;
                            var bulletE = game.Map.GetEntity(buildingX, bulletEY);

                            //test bullet/missile
                            if (bulletE != null)
                            {
                                if (bulletE.PlayerNumber != playerNumber
                                    && (bulletE.GetType() == typeof(Bullet)
                                    || bulletE.GetType() == typeof(Missile)))
                                {
                                    return true;
                                }
                            }

                        }
                    }
                    break;
            }
            return false;
        }
Beispiel #8
0
        private static void UndoBotMove(Match game, PlayerType playerType, int playerNumber, BotMove move)
        {
            var command = move.Command.ToString();

            if (move.Command == ShipCommand.BuildShield)
            {
                var botShip = game.GetPlayer(playerNumber);
                if (botShip.Ship == null)
                {
                    botShip.Lives += Settings.Default.ShieldCost;
                    game.Undo(playerType);
                    game.Depth--;
                    return;
                }
            }

            game.SetPlayerMove(playerNumber, command);

            game.Undo(playerType); //should use LastCommand or else restore command before

            game.Depth--;
        }
Beispiel #9
0
        private static void UndoAlienLevelOneMove(Match game, PlayerType playerType, int playerNumber, AlienLevelOneMove move)
        {
            var alienManager = game.GetPlayer(playerNumber).AlienManager;

            alienManager.UndoSpawnIfPossible();
            alienManager.RestoreDeadAliensAndWaves();

            alienManager.UndoIssueMovementOrders(move);

            if (game.GetRoundNumber() == Settings.Default.AlienWaveSizeBumpRound)
            {
                var player = game.GetPlayer(playerNumber);
                player.AlienWaveSize -= Settings.Default.AlienWaveSizeBump;
            }
            if (playerType == PlayerType.OpponentAliens)
            {
                game.DecrementRounderNumber();
            }
        }
Beispiel #10
0
        private static void DoAlienLevelTwoMove(Match game, PlayerType playerType, int playerNumber, AlienLevelTwoMove move)
        {
            //TODO:
            game.Depth++;

            var alienManager = game.GetPlayer(playerNumber).AlienManager;

            var shotAlien = FindShotAlien(alienManager, move.ShotAlienId);

            alienManager.IssueShootOrdersIfPossible(shotAlien);

            game.Update(playerType);
        }
Beispiel #11
0
        private static void DoAlienLevelOneMove(Match game, PlayerType playerType, int playerNumber, int depth)
        {
            if (playerType == PlayerType.OpponentAliens)
            {
                game.IncrementRoundNumber();
            }

            if (game.GetRoundNumber() == Settings.Default.AlienWaveSizeBumpRound)
            {
                var player = game.GetPlayer(playerNumber);
                player.AlienWaveSize += Settings.Default.AlienWaveSizeBump;
            }

            var alienManager = game.GetPlayer(playerNumber).AlienManager;

            alienManager.ClearDeadAliensAndWaves();
            alienManager.SpawnIfPossible();//till here

            alienManager.IssueMovementOrders();
        }
Beispiel #12
0
 private static int CalculateShotTargetX(Match game, Entity ship)
 {
     var targetX = game.Map.Width / 2;
     var opponentShip = ship;
     if (opponentShip != null)
     {
         targetX = opponentShip.X + 1;
     }
     return targetX;
 }
Beispiel #13
0
        private static Alien AlienThatCanShootUsingStrategyA(Match game, int playerNumber)
        {
            var target = game.GetPlayer(playerNumber == 1 ? 2 : 1).Ship;

            var waves = game.GetPlayer(playerNumber).AlienManager.Waves;

            var deltaX = game.GetPlayer(playerNumber).AlienManager.DeltaX;

            var mapWidth = game.Map.Width;

            var targetX = CalculateShotTargetX(game, target);
            return FindAlienClosestToX(waves, targetX, deltaX, mapWidth);
        }
Beispiel #14
0
        private static List<Alien> AliensThatCanShootUsingSTrategyB(Match game, int playerNumber)
        {
            var target = game.GetPlayer(playerNumber == 1 ? 2 : 1).Ship;

            var atPlayerAlien = AlienThatCanShootUsingStrategyA(game, playerNumber);

            var Waves = game.GetPlayer(playerNumber).AlienManager.Waves;

            var map = game.Map;

            return SelectShootingAliensExcludingAliens(map, Waves, target, new List<Alien>(1) { atPlayerAlien });
        }
Beispiel #15
0
 public static void SetInstance(Match instance)
 {
     _instance = instance;
 }
Beispiel #16
0
        private static bool ShipKanBuildShield(Match game, Ship ship, int playerNumber)
        {
            var lives = game.GetPlayer(playerNumber).Lives;
            if (lives < Settings.Default.ShieldCost) return false;

            return true;
        }
Beispiel #17
0
        private static bool ShipKanMoveRight(Match game, Ship ship, int playerNumber)
        {
            var wall = (playerNumber == 1) ? game.Map.Width - 4 : 1;

            switch (playerNumber)
            {
                case 1:
                    if (ship.X >= wall) return false;
                    var controllerX = ship.X + 3;
                    var bulletA = game.Map.GetEntity(controllerX, ship.Y);

                    if (bulletA != null)
                    {
                        if (bulletA.PlayerNumber != playerNumber
                            && (bulletA.GetType() == typeof(Bullet) || bulletA.GetType() == typeof(Missile)))
                        {
                            var controllerY = ship.Y + 1;

                            var controller = game.Map.GetEntity(controllerX, controllerY);
                            if (controller == null || controller.GetType() != typeof(MissileController))
                            {
                                return false;
                            }
                        }
                    }

                    var bulletBY = ship.Y - 1;
                    var bulletB = game.Map.GetEntity(controllerX, bulletBY);

                    if (bulletB != null)
                    {
                        if (bulletB.PlayerNumber != playerNumber
                            && (bulletB.GetType() == typeof(Bullet) || bulletB.GetType() == typeof(Missile)))
                        {
                            var controllerY = ship.Y + 1;

                            var controller = game.Map.GetEntity(controllerX, controllerY);
                            if (controller == null || controller.GetType() != typeof(MissileController))
                            {
                                return false;
                            }
                        }
                    }
                    break;
                case 2:
                    if (ship.X <= wall) return false;
                    controllerX = ship.X - 1;
                    bulletA = game.Map.GetEntity(controllerX, ship.Y);

                    if (bulletA != null)
                    {
                        if (bulletA.PlayerNumber != playerNumber
                            && (bulletA.GetType() == typeof(Bullet) || bulletA.GetType() == typeof(Missile)))
                        {
                            var controllerY = ship.Y - 1;

                            var controller = game.Map.GetEntity(controllerX, controllerY);
                            if (controller == null || controller.GetType() != typeof(MissileController))
                            {
                                return false;
                            }
                        }
                    }

                    bulletBY = ship.Y + 1;
                    bulletB = game.Map.GetEntity(controllerX, bulletBY);

                    if (bulletB != null)
                    {
                        if (bulletB.PlayerNumber != playerNumber
                            && (bulletB.GetType() == typeof(Bullet) || bulletB.GetType() == typeof(Missile)))
                        {
                            var controllerY = ship.Y - 1;

                            var controller = game.Map.GetEntity(controllerX, controllerY);
                            if (controller == null || controller.GetType() != typeof(MissileController))
                            {
                                return false;
                            }
                        }
                    }
                    break;
            }
            return true;
        }
Beispiel #18
0
        private static void DoBotMove(Match game, PlayerType playerType, int playerNumber, int depth, BotMove move)
        {
            //TODO: BotMove
            game.Depth++;

            var command = move.Command.ToString();

            game.SetPlayerMove(playerNumber, command);

            game.Update(playerType);//set Command = Nothing after update
        }
Beispiel #19
0
        private static void UndoAlienLevelTwoMove(Match game, PlayerType playerType, int playerNumber, AlienLevelTwoMove move)
        {
            //TODO: level two alien undomove
            game.Undo(playerType);

            var alienManager = game.GetPlayer(playerNumber).AlienManager;

            var shotAlien = FindShotAlien(alienManager, move.ShotAlienId);

            alienManager.UndoIssueShootOrdersIfPossible(shotAlien);

            game.Depth--;
        }
Beispiel #20
0
        private static List<ShipCommand> FindLegalShipCommands(Match game, Player player, int playerNumber, List<ShipCommand> list)
        {
            var ship = player.Ship;

            if (ShipMustMoveRight(game, ship, playerNumber))
            {
                return new List<ShipCommand> { ShipCommand.MoveRight };
            }

            if (ShipMustMoveLeft(game, ship, playerNumber))
            {
                return new List<ShipCommand> { ShipCommand.MoveLeft };
            }

            if (ShipMustShoot(game, ship, playerNumber))
            {
                return new List<ShipCommand> { ShipCommand.Shoot };
            }

            if (ShipMustShieldBuildingOnRight(game, ship, playerNumber))
            {
                return new List<ShipCommand> { ShipCommand.MoveRight };
            }

            if (ShipMustShieldBuildingOnLeft(game, ship, playerNumber))
            {
                return new List<ShipCommand> { ShipCommand.MoveLeft };
            }

            if (ShipKanKill(game, ship, playerNumber))
            {
                return new List<ShipCommand> { ShipCommand.Shoot };
            }

            if (!ShipKanMoveRight(game, ship, playerNumber))
            {
                list.Remove(ShipCommand.MoveRight);
            }

            if (!ShipKanMoveLeft(game, ship, playerNumber))
            {
                list.Remove(ShipCommand.MoveLeft);
            }

            if (!ShipKanBuildController(game, ship, playerNumber))
            {
                list.Remove(ShipCommand.BuildMissileController);
            }

            if (!ShipKanBuildFactory(game, ship, playerNumber))
            {
                list.Remove(ShipCommand.BuildAlienFactory);
            }

            list.Remove(ShipCommand.Shoot);
            list.Remove(ShipCommand.BuildShield);
            return list;
        }
Beispiel #21
0
        public static Node<ShipCommand> ExpectiMax(Match game, PlayerType playerType, int depth, int depthLimit)
        {
            Node<ShipCommand> best_move;

            var gameOver = game.GameIsOver();

            if (gameOver || depth >= depthLimit)
            {
                var command = ShipCommand.Nothing;
                return new Node<ShipCommand>(command, GetMatchStateEvaluation(gameOver));
            }

            var playerNumber = (playerType == PlayerType.OpponentAliens || playerType == PlayerType.OpponentBot) ? 2 : 1;

            if (playerType == PlayerType.OpponentAliens)
            {
                best_move = null;

                var alienManager = game.GetPlayer(playerNumber).AlienManager;

                var levelOneMove = MakeAlienLevelOneMove(alienManager);

                DoAlienLevelOneMove(game, playerType, playerNumber, depth);

                var moveList = MakeAlienLevelTwoMoveList(game, playerNumber);

                foreach (var move in moveList)
                {
                    DoAlienLevelTwoMove(game, playerType, playerNumber, move);
                    var _move = ExpectiMax(game, PlayerType.HomeAliens, depth + 1, depthLimit);
                    UndoAlienLevelTwoMove(game, playerType, playerNumber, move);
                    double score;
                    if (best_move == null)
                    {
                        score = move.Probability * _move.Score;
                        best_move = _move;
                        best_move.SetScore(score);
                    }
                    else
                    {
                        score = best_move.Score + move.Probability * _move.Score;
                        best_move.SetScore(score);
                    }
                }

                UndoAlienLevelOneMove(game, playerType, playerNumber, levelOneMove);
            }
            else if (playerType == PlayerType.HomeAliens)
            {

                best_move = null;

                var alienManager = game.GetPlayer(playerNumber).AlienManager;

                var levelOneMove = MakeAlienLevelOneMove(alienManager);

                DoAlienLevelOneMove(game, playerType, playerNumber, depth);

                var moveList = MakeAlienLevelTwoMoveList(game, playerNumber);

                foreach (var move in moveList)
                {
                    DoAlienLevelTwoMove(game, playerType, playerNumber, move);
                    var _move = ExpectiMax(game, PlayerType.HomeBot, depth + 1, depthLimit);
                    UndoAlienLevelTwoMove(game, playerType, playerNumber, move);

                    double score;
                    if (best_move == null)
                    {
                        score = move.Probability * _move.Score;
                        best_move = _move;
                        best_move.SetScore(score);
                    }
                    else
                    {
                        score = best_move.Score + move.Probability * _move.Score;
                        best_move.SetScore(score);
                    }
                }

                UndoAlienLevelOneMove(game, playerType, playerNumber, levelOneMove);

            }
            else if (playerType == PlayerType.HomeBot)
            {
                best_move = null;

                var moveList = MakeBotMoveList(game, playerNumber);

                foreach (var move in moveList)
                {
                    DoBotMove(game, playerType, playerNumber, depth, move);
                    var _move = ExpectiMax(game, PlayerType.OpponentBot, depth + 1, depthLimit);
                    UndoBotMove(game, playerType, playerNumber, move);

                    if (best_move == null || best_move.Score < _move.Score)
                    {
                        best_move = _move;

                        if (depth == 2)
                        {
                            best_move.Update(move.Command);
                        }
                    }
                }
            }
            else
            {
                best_move = null;

                var moveList = MakeBotMoveList(game, playerNumber);

                foreach (var move in moveList)
                {
                    DoBotMove(game, playerType, playerNumber, depth, move);

                    var _move = ExpectiMax(game, PlayerType.OpponentAliens, depth + 1, depthLimit);

                    UndoBotMove(game, playerType, playerNumber, move);

                    if (best_move == null || best_move.Score < _move.Score)
                    {
                        best_move = _move;
                    }
                }
            }

            return best_move;
        }
Beispiel #22
0
        private static bool IsShipInDanger(Match game, Ship ship, int playerNumber)
        {
            var ghostY = (playerNumber == 1) ? ship.Y - 1 : ship.Y + 1;
            for (var x = ship.X; x < ship.X + ship.Width; x++)
            {
                var ghost = game.Map.GetEntity(x, ghostY);

                if (ghost != null && ghost.PlayerNumber != playerNumber)
                {
                    //if (ghost.GetType() == typeof(Bullet)
                    //    || ghost.GetType() == typeof(Missile))
                    //{
                    //    return true;
                    //}

                    if (ghost.GetType() == typeof(Alien))
                    {
                        var alienManager = game.GetPlayer((playerNumber == 1) ? 2 : 1).AlienManager;

                        if (AlienAreMovingForward(alienManager.Waves, alienManager.DeltaX))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
Beispiel #23
0
        //shoot
        private bool ShipMustShoot(Match game, Ship playerShip, int playerNumber)
        {
            var player = game.GetPlayer(playerNumber);
            if (player.Missiles.Count >= player.MissileLimit) return false;

            var bulletAX = playerShip.X + 1;

            var shieldAY = (playerNumber == 1) ? playerShip.Y - 2 : playerShip.Y + 2;
            var shieldBY = (playerNumber == 1) ? playerShip.Y - 1 : playerShip.Y + 1;

            //emergency
            var bulletA = game.Map.GetEntity(bulletAX, shieldAY);

            if (bulletA != null)
            {
                if (bulletA.PlayerNumber != playerNumber
                    && (bulletA.GetType() == typeof(Bullet) || bulletA.GetType() == typeof(Missile)))
                {
                    var missile = game.Map.GetEntity(bulletAX, shieldBY);

                    if (missile == null)
                    {
                        return true;
                    }
                }
            }
            //end emergency

            var bulletBY = (playerNumber == 1) ? playerShip.Y - 3 : playerShip.Y + 3;

            var bulletB = game.Map.GetEntity(bulletAX, bulletBY);

            if (bulletB != null)
            {
                if (bulletB.PlayerNumber != playerNumber
                    && (bulletB.GetType() == typeof(Bullet) || bulletB.GetType() == typeof(Missile)))
                {
                    //Sorted by shield or alien

                    if ((bulletA != null
                        && (bulletA.GetType() == typeof(Shield) || bulletA.GetType() == typeof(Alien))))
                    {
                        return false;
                    }

                    if (bulletA == null)
                    {
                        var alienManger = game.GetPlayer(playerNumber == 1 ? 2 : 1).AlienManager;
                        var deltaX = alienManger.DeltaX;
                        var waves = alienManger.Waves;

                        if (waves.Count > 0 && !AlienAreMovingForward(waves, deltaX))
                        {
                            if (deltaX == 1)
                            {

                                var alienX = playerShip.X;

                                var alien = game.Map.GetEntity(alienX, shieldAY);

                                if (alien != null && alien.GetType() == typeof(Alien))
                                {
                                    return false;
                                }
                            }
                            else
                            {
                                var alienX = playerShip.X + 2;
                                var alienEntity = game.Map.GetEntity(alienX, shieldAY);

                                if (alienEntity != null && alienEntity.GetType() == typeof(Alien))
                                {
                                    return false;
                                }
                            }
                        }
                    }

                   var shieldB = game.Map.GetEntity(bulletAX, shieldBY);

                    if (shieldB != null && shieldB.GetType() == typeof(Shield))
                    {
                        return false;
                    }
                    return true;
                }
            }
            return false;
        }
Beispiel #24
0
        private static List<AlienLevelTwoMove> MakeAlienLevelTwoMoveList(Match game, int playerNumber)
        {
            var movelist = new List<AlienLevelTwoMove>();

            var alienManager = game.GetPlayer(playerNumber).AlienManager;

            if (AlienKanShoot(alienManager))
            {
                var shotAlienId = AlienThatCanShootUsingStrategyA(game, playerNumber).Id;

                movelist.Add(new AlienLevelTwoMove(.333, shotAlienId));

                var strategyBAliens = AliensThatCanShootUsingSTrategyB(game, playerNumber);

                if (strategyBAliens == null)
                {
                    movelist.Add(new AlienLevelTwoMove(.666));
                }
                else
                {
                    var probability = 0.666 / strategyBAliens.Count;

                    foreach (var alien in strategyBAliens)
                    {
                        movelist.Add(new AlienLevelTwoMove(probability, alien.Id));
                    }
                }
            }
            else
            {
                movelist.Add(new AlienLevelTwoMove());
            }

            return movelist;
        }
Beispiel #25
0
        //Right
        public static bool ShipMustMoveRight(Match game, Ship ship, int playerNumber)
        {
            var wall = (playerNumber == 1) ? game.Map.Width - 4 : 1;
            if (playerNumber == 1 && ship.X >= wall) return false;
            if (playerNumber == 2 && ship.X <= wall) return false;

            var bulletAX = (playerNumber == 1) ? ship.X : ship.X + 2;
            var bulletAY = (playerNumber == 1) ? ship.Y - 2 : ship.Y + 2;

            var bulletA = game.Map.GetEntity(bulletAX, bulletAY);

            if (bulletA == null
                || !((bulletA.PlayerNumber != playerNumber)
                && (bulletA.GetType() == typeof(Bullet)
                || bulletA.GetType() == typeof(Missile))))
            {
                var bulletBX = ship.X + 1;
                var bulletBY = (playerNumber == 1) ? ship.Y - 3 : ship.Y + 3;
                var bulletB = game.Map.GetEntity(bulletBX, bulletBY);

                if (bulletB != null)
                {
                    if (bulletB.PlayerNumber != playerNumber
                        && (bulletB.GetType() == typeof(Bullet) || bulletB.GetType() == typeof(Missile)))
                    {
                        var bulletC = game.Map.GetEntity(bulletAX, bulletBY);

                        if (bulletC != null)
                        {
                            if (bulletC.PlayerNumber != playerNumber
                                && (bulletC.GetType() == typeof(Bullet) || bulletC.GetType() == typeof(Missile)))
                            {
                                if (bulletA == null)
                                {
                                    var blockA = game.Map.GetEntity(bulletBX, bulletAY);

                                    if (blockA == null)
                                    {
                                        var alienManger = game.GetPlayer(playerNumber == 1 ? 2 : 1).AlienManager;
                                        var deltaX = alienManger.DeltaX;
                                        var waves = alienManger.Waves;

                                        if (waves.Count > 0 && !AlienAreMovingForward(waves, deltaX))
                                        {
                                            if (deltaX == 1)
                                            {
                                                switch (playerNumber)
                                                {
                                                    case 1:
                                                        if (ship.X <= 1) return true;
                                                        var alienX = ship.X - 1;
                                                        var alien = game.Map.GetEntity(alienX, bulletAY);
                                                        if (alien == null || alien.GetType() != typeof(Alien))
                                                        {
                                                            return true;
                                                        }
                                                        break;
                                                    case 2:
                                                        alienX = ship.X;
                                                        alien = game.Map.GetEntity(alienX, bulletAY);
                                                        if (alien == null || alien.GetType() != typeof(Alien))
                                                        {
                                                            return true;
                                                        }
                                                        break;
                                                }
                                            }
                                            else
                                            {
                                                switch (playerNumber)
                                                {
                                                    case 1:
                                                        var alienX = ship.X + 2;
                                                        var alien = game.Map.GetEntity(alienX, bulletAY);
                                                        if (alien == null || alien.GetType() != typeof(Alien))
                                                        {
                                                            return true;
                                                        }
                                                        break;
                                                    case 2:
                                                        if (ship.X >= game.Map.Width - 4) return true;
                                                        alienX = ship.X + 3;
                                                        alien = game.Map.GetEntity(alienX, bulletAY);
                                                        if (alien == null || alien.GetType() != typeof(Alien))
                                                        {
                                                            return true;
                                                        }
                                                        break;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            return true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                //TODO: double at Y-3:Y+3

                var bulletDX = (playerNumber == 1) ? ship.X + 2 : ship.X;

                var bulletD = game.Map.GetEntity(bulletDX, bulletBY);

                if (bulletD != null)
                {
                    if (bulletD.PlayerNumber != playerNumber
                        && (bulletD.GetType() == typeof(Bullet) || bulletD.GetType() == typeof(Missile)))
                    {
                        var bulletEX = (playerNumber == 1) ? ship.X : ship.X + 2;

                        var bulletE = game.Map.GetEntity(bulletEX, bulletBY);

                        if (bulletE != null)
                        {
                            if (bulletE.PlayerNumber != playerNumber
                                && (bulletE.GetType() == typeof(Bullet) || bulletE.GetType() == typeof(Missile)))
                            {
                                var shieldA = game.Map.GetEntity(bulletDX, bulletAY);
                                if (shieldA == null)
                                {
                                    var shieldB = game.Map.GetEntity(bulletEX, bulletAY);

                                    if (shieldB == null)
                                    {
                                        //start
                                        var alienManger = game.GetPlayer(playerNumber == 1 ? 2 : 1).AlienManager;
                                        var deltaX = alienManger.DeltaX;
                                        var waves = alienManger.Waves;

                                        if (waves.Count > 0 && !AlienAreMovingForward(waves, deltaX))
                                        {
                                            if (deltaX == 1)
                                            {
                                                if (ship.X <= 1)
                                                {
                                                    var alienX = ship.X + 1;
                                                    var alien = game.Map.GetEntity(alienX, bulletAY);
                                                    if (alien == null || alien.GetType() != typeof(Alien))
                                                    {
                                                        return true;
                                                    }
                                                }
                                                else
                                                {
                                                    var alienAX = ship.X - 1;
                                                    var alienA = game.Map.GetEntity(alienAX, bulletAY);
                                                    if (alienA == null || alienA.GetType() != typeof(Alien))
                                                    {
                                                        var alienBX = ship.X + 1;
                                                        var alienB = game.Map.GetEntity(alienBX, bulletAY);
                                                        if (alienA == null || alienA.GetType() != typeof(Alien))
                                                        {
                                                            return true;
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                if (ship.X >= game.Map.Width - 4)
                                                {
                                                    var alienX = ship.X + 1;
                                                    var alien = game.Map.GetEntity(alienX, bulletAY);
                                                    if (alien == null || alien.GetType() != typeof(Alien))
                                                    {
                                                        return true;
                                                    }
                                                }
                                                else
                                                {
                                                    var alienAX = ship.X + 3;
                                                    var alienA = game.Map.GetEntity(alienAX, bulletAY);
                                                    if (alienA == null || alienA.GetType() != typeof(Alien))
                                                    {
                                                        var alienBX = ship.X + 1;
                                                        var alienB = game.Map.GetEntity(alienBX, bulletAY);
                                                        if (alienA == null || alienA.GetType() != typeof(Alien))
                                                        {
                                                            return true;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            return true;
                                        }
                                    }
                                }
                            }
                        }//here

                        var buildingY = (playerNumber == 1) ? ship.Y + 1 : ship.Y - 1;
                        var building = game.Map.GetEntity(bulletDX, buildingY);

                        if (building != null
                            && (building.GetType() == typeof(MissileController)
                            || building.GetType() == typeof(AlienFactory)))
                        {
                            var player = game.GetPlayer(playerNumber);
                            if (player.Missiles.Count < player.MissileLimit) return true;
                        }

                    }
                }

                //@ Y+4/Y-4

                var newWall = (playerNumber == 1) ? wall - 1 : wall + 1;

                switch (playerNumber)
                {
                    case 1:
                        if (ship.X < newWall)
                        {
                            var buildingX = ship.X + 3;
                            var buildingY = ship.Y + 1;

                            var building = game.Map.GetEntity(buildingX, buildingY);

                            if (building != null
                                && (building.GetType() == typeof(MissileController)
                                || building.GetType() == typeof(AlienFactory)))
                            {
                                var bulletEY = ship.Y - 4;
                                var bulletE = game.Map.GetEntity(buildingX, bulletEY);

                                //test bullet/missile
                                if (bulletE != null)
                                {
                                    if (bulletE.PlayerNumber != playerNumber
                                        && (bulletE.GetType() == typeof(Bullet)
                                        || bulletE.GetType() == typeof(Missile)))
                                    {
                                        return true;
                                    }
                                }
                            }
                        }
                        break;
                    case 2:
                        if (ship.X > newWall)
                        {
                            var buildingX = ship.X - 1;
                            var buildingY = ship.Y - 1;

                            var building = game.Map.GetEntity(buildingX, buildingY);

                            if (building != null
                                && (building.GetType() == typeof(MissileController)
                                || building.GetType() == typeof(AlienFactory)))
                            {
                                var bulletEY = ship.Y + 4;
                                var bulletE = game.Map.GetEntity(buildingX, bulletEY);

                                //test bullet/missile
                                if (bulletE != null)
                                {
                                    if (bulletE.PlayerNumber != playerNumber
                                        && (bulletE.GetType() == typeof(Bullet)
                                        || bulletE.GetType() == typeof(Missile)))
                                    {
                                        return true;
                                    }
                                }

                            }
                        }
                        break;
                }
                return false;
            }

            //orginal
            var shieldCY = (playerNumber == 1) ? ship.Y - 1 : ship.Y + 1;
            var shieldC = game.Map.GetEntity(bulletAX, shieldCY);

            if ((bulletA.PlayerNumber != playerNumber)
                && (bulletA.GetType() == typeof(Bullet) || bulletA.GetType() == typeof(Missile)))
            {
                if (shieldC == null)
                {
                    var alienManger = game.GetPlayer(playerNumber == 1 ? 2 : 1).AlienManager;
                    var deltaX = alienManger.DeltaX;
                    var waves = alienManger.Waves;

                    if (waves.Count > 0 && !AlienAreMovingForward(waves, deltaX))
                    {
                        if (deltaX == 1)
                        {
                            switch (playerNumber)
                            {
                                case 1:
                                    if (ship.X <= 1) return true;
                                    var alienX = ship.X - 1;
                                    var alien = game.Map.GetEntity(alienX, shieldCY);
                                    if (alien == null || alien.GetType() != typeof(Alien))
                                    {
                                        return true;
                                    }
                                    break;
                                case 2:
                                    alienX = ship.X + 1;
                                    alien = game.Map.GetEntity(alienX, shieldCY);
                                    if (alien == null || alien.GetType() != typeof(Alien))
                                    {
                                        return true;
                                    }
                                    break;
                            }
                        }
                        else
                        {
                            switch (playerNumber)
                            {
                                case 1:
                                    var alienX = ship.X + 1;
                                    var alien = game.Map.GetEntity(alienX, shieldCY);
                                    if (alien == null || alien.GetType() != typeof(Alien))
                                    {
                                        return true;
                                    }
                                    break;
                                case 2:
                                    if (ship.X >= game.Map.Width - 4) return true;
                                    alienX = ship.X + 3;
                                    alien = game.Map.GetEntity(alienX, shieldCY);
                                    if (alien == null || alien.GetType() != typeof(Alien))
                                    {
                                        return true;
                                    }
                                    break;
                            }
                        }
                    }
                    else
                    {
                        return true;
                    }
                }
            }
            return false;
        }
Beispiel #26
0
        private static List<BotMove> MakeBotMoveList(Match game, int playerNumber)
        {
            var botMoveList = new List<BotMove>();

            var player = game.GetPlayer(playerNumber);

            if (player.Ship == null)
            {
                botMoveList.Add(new BotMove());
                return botMoveList;
            }

            var shipCommandsList = Enum.GetValues(typeof(ShipCommand)).Cast<ShipCommand>().ToList();

            var legalShipCommands = FindLegalShipCommands(game, player, playerNumber, shipCommandsList);

            foreach (var shipCommand in legalShipCommands)
            {
                botMoveList.Add(new BotMove(shipCommand));
            }

            return botMoveList;
        }