public void GetCollisionType_ComplexCases_ReturnsValidResult(int myx, int myy, int myor, int mysp, ShipMoveCommand myc, int ox, int oy, int oor, int osp, ShipMoveCommand oc, CollisionType expected)
        {
            var my    = FastShipPosition.Create(myx, myy, myor, mysp);
            var other = FastShipPosition.Create(ox, oy, oor, osp);

            GetCollisionType(my, myc, other, oc).Should().Be(expected);
        }
 private void PredictEnemiesCanMine(TurnState turnState)
 {
     foreach (var kvp in enemiesMineCooldown.ToList())
     {
         var cooldown = kvp.Value - 1;
         if (cooldown == 0)
         {
             enemiesMineCooldown.Remove(kvp.Key);
         }
         else
         {
             enemiesMineCooldown[kvp.Key] = cooldown;
         }
     }
     foreach (var mine in turnState.mines)
     {
         if (knownMines.Add(mine.id))
         {
             foreach (var kvp in lastEnemiesPositions)
             {
                 if (FastShipPosition.Mine(kvp.Value) == mine.fcoord)
                 {
                     enemiesMineCooldown[kvp.Key] = Constants.MINING_COOLDOWN;
                 }
             }
         }
     }
 }
Beispiel #3
0
        public void Move_ReturnsValidPosition()
        {
            int i = 0;

            for (int x = 0; x < Constants.MAP_WIDTH; x++)
            {
                for (int y = 0; y < Constants.MAP_HEIGHT; y++)
                {
                    for (int speed = 0; speed <= Constants.MAX_SHIP_SPEED; speed++)
                    {
                        for (int orientation = 0; orientation < 6; orientation++)
                        {
                            foreach (var moveCommand in ShipMoveCommands.all)
                            {
                                var shipPosition  = new ShipPosition(new Coord(x, y), orientation, speed);
                                var nextPositions = shipPosition.Apply(moveCommand);
                                nextPositions.Count.Should().Be(2);
                                var fastShipPosition = FastShipPosition.Create(shipPosition);

                                for (int phase = 0; phase < nextPositions.Count; phase++)
                                {
                                    if ((i++) % 99 == 0)
                                    {
                                        Console.Out.WriteLine($"shipPosition: {shipPosition}; moveCommand: {moveCommand}; phase: {phase}; nextPosition: {nextPositions[phase]}");
                                    }
                                    var actual = FastShipPosition.ToShipPosition(FastShipPosition.GetPositionAtPhase(FastShipPosition.Move(fastShipPosition, moveCommand), phase));
                                    actual.Should().Be(nextPositions[phase], $"shipPosition: {shipPosition}; moveCommand: {moveCommand}; phase: {phase}; nextPosition: {nextPositions[phase]}");
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #4
0
 public void DistanceTo_ReturnsValidValue()
 {
     for (int xt = 0; xt < Constants.MAP_WIDTH; xt++)
     {
         for (int yt = 0; yt < Constants.MAP_HEIGHT; yt++)
         {
             for (int x = 0; x < Constants.MAP_WIDTH; x++)
             {
                 for (int y = 0; y < Constants.MAP_HEIGHT; y++)
                 {
                     for (int speed = 0; speed <= Constants.MAX_SHIP_SPEED; speed++)
                     {
                         for (int orientation = 0; orientation < 6; orientation++)
                         {
                             var shipPosition     = new ShipPosition(new Coord(x, y), orientation, speed);
                             var target           = new Coord(xt, yt);
                             var fastShipPosition = FastShipPosition.Create(shipPosition);
                             FastShipPosition.DistanceTo(fastShipPosition, FastCoord.Create(target)).Should().Be(shipPosition.DistanceTo(target), $"shipPosition: {shipPosition}; target: {target}");
                         }
                     }
                 }
             }
         }
     }
 }
Beispiel #5
0
        private void CollectBarrels(TurnState turnState)
        {
            if (turnState.myShips.Count == 1)
            {
                strateg.decisions[turnState.myShips[0].id] = CollectFreeBarrels(turnState, turnState.myShips[0]);
                return;
            }

            if (turnState.enemyShips.Count == 1)
            {
                CollectEnemyBarrels(turnState, turnState.enemyShips[0], turnState.enemyShips[0]);
                return;
            }

            var ship1      = turnState.myShips[0];
            var ship2      = turnState.myShips[1];
            var enemyShip1 = turnState.enemyShips[0];
            var enemyShip2 = turnState.enemyShips[1];

            if (FastShipPosition.DistanceTo(ship1.fposition, enemyShip1.fcoord) > FastShipPosition.DistanceTo(ship2.fposition, enemyShip1.fcoord))
            {
                enemyShip1 = turnState.enemyShips[1];
                enemyShip2 = turnState.enemyShips[0];
            }

            CollectEnemyBarrels(turnState, enemyShip1, enemyShip2);
        }
Beispiel #6
0
        public StrategicDecision RunAway_FreeWay(TurnState turnState, Ship ship, StrategicDecision prevDecision)
        {
            var finalCoords = new List <int>();

            foreach (var enemyShip in turnState.enemyShips)
            {
                var move = FastShipPosition.Move(enemyShip.fposition, ShipMoveCommand.Faster);
                move = FastShipPosition.Move(FastShipPosition.GetFinalPosition(move), ShipMoveCommand.Faster);
                move = FastShipPosition.Move(FastShipPosition.GetFinalPosition(move), ShipMoveCommand.Faster);
                var finalCoord = FastShipPosition.Coord(FastShipPosition.GetFinalPosition(move));
                finalCoords.Add(finalCoord);
            }

            var candidates = runTargets.OrderByDescending(t => (int)finalCoords.Average(fc => FastCoord.Distance(t, fc) * FastCoord.Distance(t, fc))).Take(3).ToArray();

            var runTarget = candidates.OrderBy(
                t =>
            {
                var cost = 0;
                foreach (var enemyShip in turnState.enemyShips)
                {
                    cost += WayEvaluator.CalcCost(ship.fcoord, t, enemyShip.fcoord);
                }
                return(cost);
            }).First();

            return(new StrategicDecision {
                role = StrategicRole.RunAway, targetCoord = runTarget
            });
        }
 public GameState()
 {
     FastCoord.Init();
     FastShipPosition.Init();
     forecaster = new Forecaster(this);
     admiral    = new Admiral(this);
     strateg    = new Strateg(this);
 }
        public CollectableBarrel FindNearestBarrelToCollect(TurnState turnState, Ship ship, HashSet <int> used = null)
        {
            var barrelsHitTurns = new Dictionary <int, int>();

            foreach (var barrel in turnState.barrels)
            {
                foreach (var cannonball in turnState.cannonballs)
                {
                    if (cannonball.fcoord == barrel.fcoord)
                    {
                        int prevTurns;
                        if (!barrelsHitTurns.TryGetValue(barrel.id, out prevTurns) || prevTurns > cannonball.turns)
                        {
                            barrelsHitTurns[barrel.id] = cannonball.turns;
                        }
                    }
                }
            }

            var    nextShipPosition1 = FastShipPosition.GetFinalPosition(FastShipPosition.Move(ship.fposition, ShipMoveCommand.Faster));
            var    nextShipPosition2 = FastShipPosition.GetFinalPosition(FastShipPosition.Move(ship.fposition, ShipMoveCommand.Wait));
            var    bestDist          = int.MaxValue;
            Barrel bestBarrel        = null;

            foreach (var barrel in turnState.barrels)
            {
                if (used == null || !used.Contains(barrel.id))
                {
                    var dist = FastShipPosition.DistanceTo(nextShipPosition1, barrel.fcoord);
                    if (dist < bestDist)
                    {
                        var enemyTravelTime = dist / 2 + 1;
                        int hitTurns;
                        if (barrelsHitTurns.TryGetValue(barrel.id, out hitTurns) && hitTurns <= enemyTravelTime)
                        {
                            continue;
                        }
                        bestBarrel = barrel;
                        bestDist   = dist;
                    }
                    dist = FastShipPosition.DistanceTo(nextShipPosition2, barrel.fcoord);
                    if (dist < bestDist)
                    {
                        var enemyTravelTime = dist / 2 + 1;
                        int hitTurns;
                        if (barrelsHitTurns.TryGetValue(barrel.id, out hitTurns) && hitTurns <= enemyTravelTime)
                        {
                            continue;
                        }
                        bestBarrel = barrel;
                        bestDist   = dist;
                    }
                }
            }
            return(bestBarrel == null ? null : new CollectableBarrel {
                barrel = bestBarrel, dist = bestDist
            });
        }
        private CollisionType GetCollisionType(int my, ShipMoveCommand myc, int other, ShipMoveCommand oc)
        {
            uint myMovement;
            uint otherMovement;
            var  collisionType = CollisionChecker.Move(my, myc, other, oc, out myMovement, out otherMovement);

            Console.Out.WriteLine(FastShipPosition.ToShipPosition(FastShipPosition.GetMovedPosition(myMovement)) + " - " + FastShipPosition.ToShipPosition(FastShipPosition.GetFinalPosition(myMovement)));
            Console.Out.WriteLine(FastShipPosition.ToShipPosition(FastShipPosition.GetMovedPosition(otherMovement)) + " - " + FastShipPosition.ToShipPosition(FastShipPosition.GetFinalPosition(otherMovement)));
            return(collisionType);
        }
Beispiel #10
0
        public void DistanceTo_StrangeCase_ReturnsValidValue()
        {
            //coord: 23, 5, orientation: 0, speed: 0 23, 5
            var shipPosition     = new ShipPosition(new Coord(23, 5), 0, 0);
            var target           = new Coord(20, 5);
            var fastShipPosition = FastShipPosition.Create(shipPosition);
            var fastTarget       = FastCoord.Create(target);

            FastShipPosition.DistanceTo(fastShipPosition, fastTarget).Should().Be(1000);
        }
Beispiel #11
0
 public Ship(int index, int id, int x, int y, int orientation, int speed, int rum, int owner) : base(id, EntityType.Ship, x, y)
 {
     this.index       = index;
     this.orientation = orientation;
     this.speed       = speed;
     this.rum         = rum;
     this.owner       = owner;
     bow       = coord.Neighbor(orientation);
     stern     = coord.Neighbor((orientation + 3) % 6);
     fposition = FastShipPosition.Create(x, y, orientation, speed);
     fbow      = FastShipPosition.Bow(fposition);
     fstern    = FastShipPosition.Stern(fposition);
 }
Beispiel #12
0
 public void Create_OutsideMap_ReturnsValidPosition(int x, int y)
 {
     for (int speed = 0; speed <= Constants.MAX_SHIP_SPEED; speed++)
     {
         for (int orientation = 0; orientation < 6; orientation++)
         {
             var shipPosition     = new ShipPosition(new Coord(x, y), orientation, speed);
             var fastShipPosition = FastShipPosition.Create(shipPosition);
             var actual           = FastShipPosition.ToShipPosition(fastShipPosition);
             actual.coord.Should().Be(new Coord(-1000, -1000), $"it's coord of [{shipPosition}]");
             actual.speed.Should().Be(shipPosition.speed, $"it's speed of [{shipPosition}]");
             actual.orientation.Should().Be(shipPosition.orientation, $"it's orientation of [{shipPosition}]");
         }
     }
 }
 private void BuildMinesForecast(TurnState turnState)
 {
     for (var i = 0; i < turnState.mines.Count; i++)
     {
         var mineCoord = turnState.mines[i].fcoord;
         BuildMineForecast(mineCoord);
     }
     foreach (var enemyShip in turnState.enemyShips)
     {
         if (!enemiesMineCooldown.ContainsKey(enemyShip.id))
         {
             var mineCoord = FastShipPosition.Mine(enemyShip.fposition);
             BuildMineForecast(mineCoord);
         }
     }
 }
Beispiel #14
0
 public void Stern_ReturnsValidCoord()
 {
     for (int x = 0; x < Constants.MAP_WIDTH; x++)
     {
         for (int y = 0; y < Constants.MAP_HEIGHT; y++)
         {
             for (int speed = 0; speed <= Constants.MAX_SHIP_SPEED; speed++)
             {
                 for (int orientation = 0; orientation < 6; orientation++)
                 {
                     var shipPosition     = new ShipPosition(new Coord(x, y), orientation, speed);
                     var fastShipPosition = FastShipPosition.Create(shipPosition);
                     FastCoord.ToCoord(FastShipPosition.Stern(fastShipPosition)).Should().Be(shipPosition.stern, shipPosition.ToString());
                 }
             }
         }
     }
 }
Beispiel #15
0
 public void IsInsideMap_ReturnsValidValue()
 {
     for (int x = -2; x < Constants.MAP_WIDTH + 2; x++)
     {
         for (int y = -2; y < Constants.MAP_HEIGHT + 2; y++)
         {
             for (int speed = 0; speed <= Constants.MAX_SHIP_SPEED; speed++)
             {
                 for (int orientation = 0; orientation < 6; orientation++)
                 {
                     var shipPosition     = new ShipPosition(new Coord(x, y), orientation, speed);
                     var fastShipPosition = FastShipPosition.Create(shipPosition);
                     FastShipPosition.IsInsideMap(fastShipPosition).Should().Be(shipPosition.IsInsideMap(), shipPosition.ToString());
                 }
             }
         }
     }
 }
        public void ApplyPath(Ship ship, List <PathItem> path)
        {
            var index     = ship.index;
            var movedShip = ship.fposition;

            for (var i = 0; i < path.Count; i++)
            {
                var pathItem = path[i];
                GetTurnForecast(i).myShipsSourcePositions[index] = pathItem.sourcePosition;
                GetTurnForecast(i).myShipsPositions[index]       = pathItem.targetPosition;
                GetTurnForecast(i).myShipsMoveCommands[index]    = pathItem.command;
                movedShip = pathItem.targetPosition;
            }
            for (var i = path.Count; i < Settings.NAVIGATION_PATH_DEPTH; i++)
            {
                movedShip = FastShipPosition.GetFinalPosition(FastShipPosition.Move(movedShip, ShipMoveCommand.Wait));
                GetTurnForecast(i).myShipsPositions[index] = movedShip;
            }
        }
        public StrategicDecision MakeStrategicDecision(TurnState turnState, StrategicDecision prevDecision, Ship ship, Ship enemyShip)
        {
            var barrels = CollectableBarrels(turnState, enemyShip);

            var nextShipPosition1 = FastShipPosition.GetFinalPosition(FastShipPosition.Move(ship.fposition, ShipMoveCommand.Faster));
            var nextShipPosition2 = FastShipPosition.GetFinalPosition(FastShipPosition.Move(ship.fposition, ShipMoveCommand.Wait));

            var target = barrels.FirstOrDefault(
                b => FastShipPosition.DistanceTo(nextShipPosition1, b.barrel.fcoord) < b.dist - 1 ||
                FastShipPosition.DistanceTo(nextShipPosition2, b.barrel.fcoord) < b.dist - 1);

            if (target == null)
            {
                return(strateg.RunAway(turnState, ship, prevDecision));
            }

            var barrelToFire = barrels.TakeWhile(b => b != target).LastOrDefault();

            return(strateg.Collect(target.barrel).FireTo(barrelToFire?.barrel.fcoord));
        }
 private ShipPathChainItem(
     ShipPathChainItem prev,
     ShipMoveCommand command,
     int fposition,
     int depth,
     ShipMoveCommand startCommand,
     int ftarget,
     int pathDamage)
 {
     this.prev         = prev;
     this.command      = command;
     this.fposition    = fposition;
     this.depth        = depth;
     this.startCommand = startCommand;
     dist            = FastShipPosition.DistanceTo(fposition, ftarget);
     this.pathDamage = pathDamage;
     if (depth == Settings.NAVIGATION_PATH_DEPTH)
     {
         SetDamage(pathDamage);
     }
 }
Beispiel #19
0
 public void Create_InsideMap_ReturnsValidPosition()
 {
     for (int speed = 0; speed <= Constants.MAX_SHIP_SPEED; speed++)
     {
         for (int orientation = 0; orientation < 6; orientation++)
         {
             for (int x = 0; x < Constants.MAP_WIDTH; x++)
             {
                 for (int y = 0; y < Constants.MAP_HEIGHT; y++)
                 {
                     var shipPosition     = new ShipPosition(new Coord(x, y), orientation, speed);
                     var fastShipPosition = FastShipPosition.Create(shipPosition);
                     var actual           = FastShipPosition.ToShipPosition(fastShipPosition);
                     actual.coord.Should().Be(shipPosition.coord, $"it's coord of [{shipPosition}]");
                     actual.speed.Should().Be(shipPosition.speed, $"it's speed of [{shipPosition}]");
                     actual.orientation.Should().Be(shipPosition.orientation, $"it's orientation of [{shipPosition}]");
                 }
             }
         }
     }
 }
Beispiel #20
0
        public StrategicDecision WalkFree(TurnState turnState, Ship ship, StrategicDecision prevDecision)
        {
            switch (prevDecision?.role)
            {
            case StrategicRole.Free:
                break;

            default:
                prevDecision = new StrategicDecision {
                    role = StrategicRole.Free, targetCoord = freeTargets[0]
                };
                break;
            }
            if (FastShipPosition.DistanceTo(ship.fposition, prevDecision.targetCoord.Value) < Settings.FREE_WALK_TARGET_REACH_DIST)
            {
                var freeIndex = (Array.IndexOf(freeTargets, prevDecision.targetCoord.Value) + 1) % freeTargets.Length;
                return(new StrategicDecision {
                    role = StrategicRole.Free, targetCoord = freeTargets[freeIndex]
                });
            }
            return(prevDecision);
        }
        private void BuildMyShipsForecast(TurnState turnState)
        {
            var prevPositions = new int[turnState.myShips.Count];

            for (var i = 0; i < turnState.myShips.Count; i++)
            {
                prevPositions[i] = turnState.myShips[i].fposition;
            }
            for (int depth = 0; depth < Settings.NAVIGATION_PATH_DEPTH; depth++)
            {
                turnForecasts[depth].myShipsSourcePositions = prevPositions;
                var commands = new ShipMoveCommand[prevPositions.Length];
                turnForecasts[depth].myShipsMoveCommands = commands;
                var nextPositions = new int[prevPositions.Length];
                for (var i = 0; i < prevPositions.Length; i++)
                {
                    var position = prevPositions[i];
                    var movement = FastShipPosition.Move(position, ShipMoveCommand.Wait);
                    nextPositions[i] = FastShipPosition.GetFinalPosition(movement);
                }
                prevPositions = turnForecasts[depth].myShipsPositions = nextPositions;
            }
        }
        private List <FireTarget> GetFireTargets(Ship ship, Ship enemyShip)
        {
            var result      = new List <FireTarget>();
            var enemyIndex  = enemyShip.index;
            var cannonCoord = FastShipPosition.Bow(ship.fposition);

            for (var turn = 0; turn < Settings.CANNONS_TRAVEL_TIME_LIMIT + 1; turn++)
            {
                var forecast      = gameState.forecaster.GetTurnForecast(turn);
                var enemyPosition = forecast.enemyShipsFinalPositions[enemyIndex];

                var coords      = new[] { FastShipPosition.Coord(enemyPosition), FastShipPosition.Bow(enemyPosition), FastShipPosition.Stern(enemyPosition) };
                var targetTypes = new[] { FireTargetType.ShipCenter, FireTargetType.ShipBow, FireTargetType.ShipStern };
                for (var i = 0; i < coords.Length; i++)
                {
                    var target = coords[i];
                    if (FastCoord.IsInsideMap(target))
                    {
                        if (forecast.myShipsPositions.Any(m => FastShipPosition.Collides(m, target)))
                        {
                            continue;
                        }
                        var distanceTo = FastCoord.Distance(cannonCoord, target);
                        if (distanceTo <= 10)
                        {
                            var travelTime = (int)(1 + Math.Round(distanceTo / 3.0));
                            if (travelTime == turn)
                            {
                                result.Add(new FireTarget(target, turn, i == 0 ? Constants.HIGH_DAMAGE : Constants.LOW_DAMAGE, targetTypes[i]));
                            }
                        }
                    }
                }
            }
            return(result);
        }
Beispiel #23
0
 public void CollidesShip_ReturnsValidValue()
 {
     for (int xt = 0; xt < Constants.MAP_WIDTH; xt += 3)
     {
         for (int yt = 0; yt < Constants.MAP_HEIGHT; yt += 3)
         {
             for (int speedT = 0; speedT <= Constants.MAX_SHIP_SPEED; speedT++)
             {
                 for (int orientationT = 0; orientationT < 6; orientationT++)
                 {
                     for (int x = 0; x < Constants.MAP_WIDTH; x++)
                     {
                         for (int y = 0; y < Constants.MAP_HEIGHT; y++)
                         {
                             for (int speed = 0; speed <= Constants.MAX_SHIP_SPEED; speed++)
                             {
                                 for (int orientation = 0; orientation < 6; orientation++)
                                 {
                                     if (!(Math.Abs(x - xt) < 6 && Math.Abs(y - yt) < 6))
                                     {
                                         continue;
                                     }
                                     var shipPosition       = new ShipPosition(new Coord(x, y), orientation, speed);
                                     var targetPosition     = new ShipPosition(new Coord(xt, yt), orientationT, speedT);
                                     var fastShipPosition   = FastShipPosition.Create(shipPosition);
                                     var fastTargetPosition = FastShipPosition.Create(targetPosition);
                                     FastShipPosition.CollidesShip(fastShipPosition, fastTargetPosition).Should().Be(shipPosition.CollidesShip(targetPosition), $"shipPosition: {shipPosition}; targetPosition: {targetPosition}");
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
        private static void Split(int position, ShipMoveCommand command, out int coord, out int bow, out int stern, out int orientation, out int speed, out int ncoord, out int nbow, out int nstern, out int norientation)
        {
            coord        = FastShipPosition.Coord(position);
            orientation  = FastShipPosition.Orientation(position);
            speed        = FastShipPosition.Speed(position);
            bow          = FastCoord.Neighbor(coord, orientation);
            stern        = FastCoord.Neighbor(coord, (orientation + 3) % 6);
            ncoord       = coord;
            nbow         = bow;
            nstern       = stern;
            norientation = orientation;
            switch (command)
            {
            case ShipMoveCommand.Faster:
                if (speed < Constants.MAX_SHIP_SPEED)
                {
                    speed++;
                }
                break;

            case ShipMoveCommand.Slower:
                if (speed > 0)
                {
                    speed--;
                }
                break;

            case ShipMoveCommand.Port:
                norientation = (orientation + 1) % 6;
                break;

            case ShipMoveCommand.Starboard:
                norientation = (orientation + 5) % 6;
                break;
            }
        }
        private void BuildEnemyShipsForecast(TurnState turnState)
        {
            var prevPositions = new int[turnState.enemyShips.Count];

            for (var i = 0; i < turnState.enemyShips.Count; i++)
            {
                prevPositions[i] = turnState.enemyShips[i].fposition;
            }

            for (var depth = 0; depth < Settings.NAVIGATION_PATH_DEPTH; depth++)
            {
                var nextMovedPositions = new int[prevPositions.Length];
                var nextFinalPositions = new int[prevPositions.Length];
                for (var i = 0; i < prevPositions.Length; i++)
                {
                    var position = prevPositions[i];
                    var movement = FastShipPosition.Move(position, ShipMoveCommand.Wait);
                    nextMovedPositions[i] = FastShipPosition.GetMovedPosition(movement);
                    nextFinalPositions[i] = FastShipPosition.GetFinalPosition(movement);
                }
                turnForecasts[depth].enemyShipsMovedPositions = nextMovedPositions;
                prevPositions = turnForecasts[depth].enemyShipsFinalPositions = nextFinalPositions;
            }
        }
        public void MakeStrategicDecisions(TurnState turnState)
        {
            if (turnState.barrels.Any())
            {
                strateg.MakeStandardStrategicDecisions(turnState);
                return;
            }

            if (turnState.myShips.Max(s => s.rum) > turnState.enemyShips.Max(s => s.rum) || turnState.myShips.Min(s => s.rum) > 50)
            {
                foreach (var ship in turnState.myShips)
                {
                    StrategicDecision prevDecision;
                    strateg.decisions.TryGetValue(ship.id, out prevDecision);
                    strateg.decisions[ship.id] = strateg.RunAway(turnState, ship, prevDecision);
                }
                return;
            }

            if (turnState.myShips.Count == 1)
            {
                var ship = turnState.myShips[0];
                StrategicDecision prevDecision;
                strateg.decisions.TryGetValue(ship.id, out prevDecision);
                strateg.decisions[ship.id] = strateg.RunAway(turnState, ship, prevDecision);
                return;
            }

            var maxRum = turnState.myShips.Max(s => s.rum);
            var minRum = turnState.myShips.Min(s => s.rum);
            var ship1  = turnState.myShips.First(s => s.rum == minRum);
            var ship2  = turnState.myShips.Last(s => s.rum == maxRum);
            var last   = turnState.myShips.FirstOrDefault(s => s != ship1 && s != ship2);

            if (last != null)
            {
                StrategicDecision prevDecision;
                strateg.decisions.TryGetValue(last.id, out prevDecision);
                strateg.decisions[last.id] = strateg.RunAway(turnState, last, prevDecision);
            }

            if (FastCoord.Distance(ship1.fbow, ship2.fbow) <= 4)
            {
                StrategicDecision prevDecision;
                strateg.decisions.TryGetValue(ship1.id, out prevDecision);
                if (prevDecision?.role == StrategicRole.Fire || prevDecision?.role == StrategicRole.Explicit)
                {
                    strateg.decisions[ship1.id] = new StrategicDecision {
                        role = StrategicRole.Explicit, explicitCommand = ShipMoveCommand.Slower
                    };
                    strateg.decisions[ship2.id] = new StrategicDecision {
                        role = StrategicRole.Approach, targetCoord = prevDecision.fireToCoord
                    };
                }
                else
                {
                    var nextShip1Position = FastShipPosition.GetFinalPosition(FastShipPosition.Move(ship1.fposition, ShipMoveCommand.Wait));
                    nextShip1Position           = FastShipPosition.GetFinalPosition(FastShipPosition.Move(nextShip1Position, ShipMoveCommand.Slower));
                    strateg.decisions[ship1.id] = new StrategicDecision {
                        role = StrategicRole.Fire, fireToCoord = FastShipPosition.Coord(nextShip1Position)
                    };
                    strateg.decisions[ship2.id] = new StrategicDecision {
                        role = StrategicRole.Approach, targetCoord = FastShipPosition.Coord(nextShip1Position)
                    };
                }
            }
            else
            {
                var x = (FastCoord.GetX(ship1.fcoord) + FastCoord.GetX(ship2.fcoord)) / 2;
                var y = (FastCoord.GetY(ship1.fcoord) + FastCoord.GetY(ship2.fcoord)) / 2;
                if (x < 5)
                {
                    x = 5;
                }
                if (x > Constants.MAP_WIDTH - 6)
                {
                    x = Constants.MAP_WIDTH - 6;
                }
                if (y < 5)
                {
                    y = 5;
                }
                if (y > Constants.MAP_HEIGHT - 6)
                {
                    x = Constants.MAP_HEIGHT - 6;
                }

                strateg.decisions[ship1.id] = new StrategicDecision {
                    role = StrategicRole.Approach, targetCoord = FastCoord.Create(x, y)
                };
                strateg.decisions[ship2.id] = new StrategicDecision {
                    role = StrategicRole.Approach, targetCoord = FastCoord.Create(x, y)
                };
            }
        }
Beispiel #27
0
        private void RunOrSuicide(TurnState turnState)
        {
            if (turnState.myShips.Max(s => s.rum) > turnState.enemyShips.Max(s => s.rum) || turnState.myShips.Min(s => s.rum) > 50)
            {
                foreach (var ship in turnState.myShips)
                {
                    StrategicDecision prevDecision;
                    strateg.decisions.TryGetValue(ship.id, out prevDecision);
                    strateg.decisions[ship.id] = strateg.RunAway(turnState, ship, prevDecision);
                }
                return;
            }

            if (turnState.myShips.Count == 1)
            {
                var ship = turnState.myShips[0];
                StrategicDecision prevDecision;
                strateg.decisions.TryGetValue(ship.id, out prevDecision);
                strateg.decisions[ship.id] = strateg.RunAway(turnState, ship, prevDecision);
                return;
            }

            var ship1 = turnState.myShips[0];
            var ship2 = turnState.myShips[1];

            if (ship1.rum > ship2.rum)
            {
                var tmp = ship1;
                ship1 = ship2;
                ship2 = tmp;
            }
            if (FastCoord.Distance(ship1.fbow, ship2.fbow) <= 4)
            {
                StrategicDecision prevDecision;
                strateg.decisions.TryGetValue(ship1.id, out prevDecision);
                if (prevDecision?.role == StrategicRole.Fire || prevDecision?.role == StrategicRole.Explicit)
                {
                    strateg.decisions[ship1.id] = new StrategicDecision {
                        role = StrategicRole.Explicit, explicitCommand = ShipMoveCommand.Slower
                    };
                    strateg.decisions[ship2.id] = new StrategicDecision {
                        role = StrategicRole.Approach, targetCoord = prevDecision.fireToCoord
                    };
                }
                else
                {
                    var nextShip1Position = FastShipPosition.GetFinalPosition(FastShipPosition.Move(ship1.fposition, ShipMoveCommand.Wait));
                    nextShip1Position           = FastShipPosition.GetFinalPosition(FastShipPosition.Move(nextShip1Position, ShipMoveCommand.Slower));
                    strateg.decisions[ship1.id] = new StrategicDecision {
                        role = StrategicRole.Fire, fireToCoord = FastShipPosition.Coord(nextShip1Position)
                    };
                    strateg.decisions[ship2.id] = new StrategicDecision {
                        role = StrategicRole.Approach, targetCoord = FastShipPosition.Coord(nextShip1Position)
                    };
                }
            }
            else
            {
                var x = (FastCoord.GetX(ship1.fcoord) + FastCoord.GetX(ship2.fcoord)) / 2;
                var y = (FastCoord.GetY(ship1.fcoord) + FastCoord.GetY(ship2.fcoord)) / 2;
                if (x < 5)
                {
                    x = 5;
                }
                if (x > Constants.MAP_WIDTH - 6)
                {
                    x = Constants.MAP_WIDTH - 6;
                }
                if (y < 5)
                {
                    y = 5;
                }
                if (y > Constants.MAP_HEIGHT - 6)
                {
                    x = Constants.MAP_HEIGHT - 6;
                }

                strateg.decisions[ship1.id] = new StrategicDecision {
                    role = StrategicRole.Approach, targetCoord = FastCoord.Create(x, y)
                };
                strateg.decisions[ship2.id] = new StrategicDecision {
                    role = StrategicRole.Approach, targetCoord = FastCoord.Create(x, y)
                };
            }
        }
Beispiel #28
0
 public void SetUp()
 {
     FastShipPosition.Init();
 }
Beispiel #29
0
        public void CollectEnemyBarrels(TurnState turnState, Ship enemyShip1, Ship enemyShip2)
        {
            var barrels1 = strateg.strategy1vs1.CollectableBarrels(turnState, enemyShip1);
            var barrels2 = strateg.strategy1vs1.CollectableBarrels(turnState, enemyShip2);

            var ship1 = turnState.myShips[0];
            var ship2 = turnState.myShips[1];

            var nextShip1Position1 = FastShipPosition.GetFinalPosition(FastShipPosition.Move(ship1.fposition, ShipMoveCommand.Faster));
            var nextShip1Position2 = FastShipPosition.GetFinalPosition(FastShipPosition.Move(ship1.fposition, ShipMoveCommand.Wait));

            var nextShip2Position1 = FastShipPosition.GetFinalPosition(FastShipPosition.Move(ship2.fposition, ShipMoveCommand.Faster));
            var nextShip2Position2 = FastShipPosition.GetFinalPosition(FastShipPosition.Move(ship2.fposition, ShipMoveCommand.Wait));

            var target1 = barrels1.FirstOrDefault(
                b => FastShipPosition.DistanceTo(nextShip1Position1, b.barrel.fcoord) < b.dist - 1 ||
                FastShipPosition.DistanceTo(nextShip1Position2, b.barrel.fcoord) < b.dist - 1);

            var target2 = barrels2.FirstOrDefault(
                b => FastShipPosition.DistanceTo(nextShip2Position1, b.barrel.fcoord) < b.dist - 1 ||
                FastShipPosition.DistanceTo(nextShip2Position2, b.barrel.fcoord) < b.dist - 1);

            while (true)
            {
                if (target1 == null)
                {
                    StrategicDecision decision;
                    strateg.decisions.TryGetValue(ship1.id, out decision);
                    strateg.decisions[ship1.id] = strateg.RunAway(turnState, ship1, decision);
                }

                if (target2 == null)
                {
                    StrategicDecision decision;
                    strateg.decisions.TryGetValue(ship2.id, out decision);
                    strateg.decisions[ship2.id] = strateg.RunAway(turnState, ship2, decision);
                }

                if (target1 == null && target2 == null)
                {
                    return;
                }

                if (target1 != null && target2 == null)
                {
                    var barrelToFire = barrels1.TakeWhile(b => b != target1).LastOrDefault();
                    strateg.decisions[ship1.id] = strateg.Collect(target1.barrel).FireTo(barrelToFire?.barrel.fcoord);
                    return;
                }

                if (target2 != null && target1 == null)
                {
                    var barrelToFire = barrels2.TakeWhile(b => b != target2).LastOrDefault();
                    strateg.decisions[ship2.id] = strateg.Collect(target2.barrel).FireTo(barrelToFire?.barrel.fcoord);
                    return;
                }

                if (target1.barrel != target2.barrel)
                {
                    var barrelToFire1 = barrels1.TakeWhile(b => b.barrel != target1.barrel && b.barrel != target2.barrel).LastOrDefault();
                    strateg.decisions[ship1.id] = strateg.Collect(target1.barrel).FireTo(barrelToFire1?.barrel.fcoord);

                    var barrelToFire2 = barrels2.TakeWhile(b => b.barrel != target1.barrel && b.barrel != target2.barrel && b.barrel != barrelToFire1?.barrel).LastOrDefault();
                    strateg.decisions[ship2.id] = strateg.Collect(target2.barrel).FireTo(barrelToFire2?.barrel.fcoord);
                    return;
                }

                var dist1 = Math.Min(FastShipPosition.DistanceTo(nextShip1Position1, target1.barrel.fcoord),
                                     FastShipPosition.DistanceTo(nextShip1Position2, target1.barrel.fcoord));
                var dist2 = Math.Min(FastShipPosition.DistanceTo(nextShip2Position1, target1.barrel.fcoord),
                                     FastShipPosition.DistanceTo(nextShip2Position2, target1.barrel.fcoord));
                if (dist1 < dist2)
                {
                    target2 = barrels2.FirstOrDefault(
                        b => b.barrel != target1.barrel && (FastShipPosition.DistanceTo(nextShip2Position1, b.barrel.fcoord) < b.dist - 1 ||
                                                            FastShipPosition.DistanceTo(nextShip2Position2, b.barrel.fcoord) < b.dist - 1));
                }
                else
                {
                    target1 = barrels1.FirstOrDefault(
                        b => b.barrel != target2.barrel && (FastShipPosition.DistanceTo(nextShip1Position1, b.barrel.fcoord) < b.dist - 1 ||
                                                            FastShipPosition.DistanceTo(nextShip1Position2, b.barrel.fcoord) < b.dist - 1));
                }
            }
        }
        public static CollisionType Move(int myPosition, ShipMoveCommand myCommand, int otherPosition, ShipMoveCommand otherCommand, out uint myMovement, out uint otherMovement)
        {
            var result = CollisionType.None;

            int myCoord, myBow, myStern, myOrientation, mySpeed, nmyCoord, nmyBow, nmyStern, nmyOrientation;

            Split(myPosition, myCommand, out myCoord, out myBow, out myStern, out myOrientation, out mySpeed, out nmyCoord, out nmyBow, out nmyStern, out nmyOrientation);

            int otherCoord, otherBow, otherStern, otherOrientation, otherSpeed, notherCoord, notherBow, notherStern, notherOrientation;

            Split(otherPosition, otherCommand, out otherCoord, out otherBow, out otherStern, out otherOrientation, out otherSpeed, out notherCoord, out notherBow, out notherStern, out notherOrientation);

            // move ships

            for (int i = 1; i <= Constants.MAX_SHIP_SPEED; i++)
            {
                if (!GoForward(i, ref myCoord, ref myBow, ref myStern, ref myOrientation, ref mySpeed, ref nmyCoord, ref nmyBow, ref nmyStern))
                {
                    result |= CollisionType.MyWall;
                }
                if (!GoForward(i, ref otherCoord, ref otherBow, ref otherStern, ref otherOrientation, ref otherSpeed, ref notherCoord, ref notherBow, ref notherStern))
                {
                    result |= CollisionType.OtherWall;
                }

                var myCollides    = mySpeed > 0 && (nmyBow == notherBow || nmyBow == notherCoord || nmyBow == notherStern);
                var otherCollides = otherSpeed > 0 && (notherBow == nmyBow || notherBow == nmyCoord || notherBow == nmyStern);

                if (myCollides)
                {
                    nmyCoord = myCoord;
                    nmyBow   = myBow;
                    nmyStern = myStern;
                    mySpeed  = 0;
                    result  |= CollisionType.MyMove;
                }
                if (otherCollides)
                {
                    notherCoord = otherCoord;
                    notherBow   = otherBow;
                    notherStern = otherStern;
                    otherSpeed  = 0;
                    result     |= CollisionType.OtherMove;
                }

                myCoord = nmyCoord;
                myBow   = nmyBow;
                myStern = nmyStern;

                otherCoord = notherCoord;
                otherBow   = notherBow;
                otherStern = notherStern;
            }

            var myMovedPosition    = FastShipPosition.Create(myCoord, myOrientation, mySpeed);
            var otherMovedPosition = FastShipPosition.Create(otherCoord, otherOrientation, otherSpeed);

            // rotate ships
            nmyBow   = FastCoord.Neighbor(myCoord, nmyOrientation);
            nmyStern = FastCoord.Neighbor(myCoord, (nmyOrientation + 3) % 6);

            notherBow   = FastCoord.Neighbor(otherCoord, notherOrientation);
            notherStern = FastCoord.Neighbor(otherCoord, (notherOrientation + 3) % 6);

            var rotationCollides =
                nmyBow == notherBow || nmyBow == notherCoord || nmyBow == notherStern ||
                nmyCoord == notherBow || nmyCoord == notherCoord || nmyCoord == notherStern ||
                nmyStern == notherBow || nmyStern == notherCoord || nmyStern == notherStern;

            if (rotationCollides)
            {
                if (myCommand == ShipMoveCommand.Port || myCommand == ShipMoveCommand.Starboard)
                {
                    result |= CollisionType.MyRotation;
                }
                if (otherCommand == ShipMoveCommand.Port || otherCommand == ShipMoveCommand.Starboard)
                {
                    result |= CollisionType.OtherRotation;
                }
            }
            else
            {
                myBow         = nmyBow;
                myStern       = nmyStern;
                myOrientation = nmyOrientation;

                otherBow         = notherBow;
                otherStern       = notherStern;
                otherOrientation = notherOrientation;
            }

            var myFinalPosition = FastShipPosition.Create(myCoord, myOrientation, mySpeed);

            myMovement = FastShipPosition.Move(myMovedPosition, myFinalPosition);

            var otherFinalPosition = FastShipPosition.Create(otherCoord, otherOrientation, otherSpeed);

            otherMovement = FastShipPosition.Move(otherMovedPosition, otherFinalPosition);

            return(result);
        }