protected Entity(int id, EntityType type, int x, int y)
 {
     this.id   = id;
     this.type = type;
     coord     = new Coord(x, y);
     fcoord    = FastCoord.Create(x, y);
 }
Example #2
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}");
                         }
                     }
                 }
             }
         }
     }
 }
        public void NearMap_GoOut_ReturnsValidFastCoord(int x, int y, int orientation)
        {
            var coord     = new Coord(x, y);
            var fastCoord = FastCoord.Create(coord);
            var neighbor  = FastCoord.Neighbor(fastCoord, orientation);

            neighbor.Should().Be(-1);
        }
 public GameState()
 {
     FastCoord.Init();
     FastShipPosition.Init();
     forecaster = new Forecaster(this);
     admiral    = new Admiral(this);
     strateg    = new Strateg(this);
 }
        public void NearMap_GoIn_ReturnsValidFastCoord(int x, int y, int orientation)
        {
            var coord     = new Coord(x, y);
            var fastCoord = FastCoord.Create(coord);
            var neighbor  = FastCoord.Neighbor(fastCoord, orientation);
            var actual    = FastCoord.ToCoord(neighbor);

            actual.Should().Be(coord.Neighbor(orientation));
        }
        public override string ToString()
        {
            var targetCoordString     = targetCoord.HasValue ? $", goto: {FastCoord.ToCoord(targetCoord.Value)}" : "";
            var targetBarrelIdString  = targetBarrelId.HasValue ? $", barrel: {targetBarrelId.Value}" : "";
            var fireToCoordString     = fireToCoord.HasValue ? $", fireTo: {FastCoord.ToCoord(fireToCoord.Value)}" : "";
            var explicitCommandString = explicitCommand.HasValue ? $", explicit: {explicitCommand.Value}" : "";

            return($"{role}{targetCoordString}{targetBarrelIdString}{fireToCoordString}{explicitCommandString}");
        }
Example #7
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);
        }
Example #8
0
        public void FarFromMap_ReturnsValidFastCoord(int x, int y)
        {
            var coord     = new Coord(x, y);
            var fastCoord = FastCoord.Create(coord);

            fastCoord.Should().Be(-1);
            var actual = FastCoord.ToCoord(fastCoord);

            actual.Should().Be(new Coord(-1000, -1000));
        }
 public void ReturnsValidValue()
 {
     for (int x = -10; x < Constants.MAP_WIDTH + 10; x++)
     {
         for (int y = -10; y < Constants.MAP_HEIGHT + 10; y++)
         {
             var coord     = new Coord(x, y);
             var fastCoord = FastCoord.Create(coord);
             FastCoord.IsInsideMap(fastCoord).Should().Be(coord.IsInsideMap(), coord.ToString());
         }
     }
 }
Example #10
0
 public void InsideMap_ReturnsValidFastCoord()
 {
     for (int x = 0; x < Constants.MAP_WIDTH; x++)
     {
         for (int y = 0; y < Constants.MAP_HEIGHT; y++)
         {
             var coord     = new Coord(x, y);
             var fastCoord = FastCoord.Create(coord);
             var actual    = FastCoord.ToCoord(fastCoord);
             actual.Should().Be(coord);
         }
     }
 }
 public void InsideMap_ReturnsValidFastCoord(int orientation)
 {
     for (int x = 0; x < Constants.MAP_WIDTH; x++)
     {
         for (int y = 0; y < Constants.MAP_HEIGHT; y++)
         {
             var coord     = new Coord(x, y);
             var fastCoord = FastCoord.Create(coord);
             var neighbor  = FastCoord.Neighbor(fastCoord, orientation);
             var actual    = FastCoord.ToCoord(neighbor);
             actual.Should().Be(coord.Neighbor(orientation));
         }
     }
 }
        private void BuildCannonballsForecast(TurnState turnState)
        {
            for (var i = 0; i < turnState.cannonballs.Count; i++)
            {
                if (turnState.cannonballs[i].turns - 1 >= 0 && turnState.cannonballs[i].turns - 1 < Settings.NAVIGATION_PATH_DEPTH)
                {
                    GetTurnForecast(turnState.cannonballs[i].turns - 1).cannonballCoordsMap[turnState.cannonballs[i].fcoord] = true;
                }
            }

            if (Settings.CANNONBALLS_FORECAST_TRAVEL_TIME_LIMIT > 0)
            {
                foreach (var enemyShip in turnState.enemyShips)
                {
                    if (!enemiesCanFire.Contains(enemyShip.id))
                    {
                        continue;
                    }
                    var targets = Enumerable.Range(0, 6).Select(o => enemyShip.fbow).ToArray();
                    for (var dist = 1; dist <= 10; dist++)
                    {
                        var travelTime = (int)(1 + Math.Round(dist / 3.0));
                        if (travelTime > Settings.CANNONBALLS_FORECAST_TRAVEL_TIME_LIMIT)
                        {
                            break;
                        }
                        if (travelTime >= Settings.NAVIGATION_PATH_DEPTH)
                        {
                            break;
                        }
                        for (var orientation = 0; orientation < 6; orientation++)
                        {
                            targets[orientation] = FastCoord.Neighbor(targets[orientation], orientation);
                        }
                        for (var orientation = 0; orientation < 6; orientation++)
                        {
                            if (FastCoord.IsInsideMap(targets[orientation]))
                            {
                                GetTurnForecast(travelTime).cannonballCoordsMap[targets[orientation]] = true;
                            }
                        }
                    }
                }
            }
        }
Example #13
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());
                 }
             }
         }
     }
 }
        private static FireTarget TryGetPreferredFireTarget(Ship ship, int?preferredFireTargetCoord)
        {
            if (!preferredFireTargetCoord.HasValue)
            {
                return(null);
            }

            var distanceTo = FastCoord.Distance(ship.fbow, preferredFireTargetCoord.Value);

            if (distanceTo > 10)
            {
                return(null);
            }

            var travelTime = (int)(1 + Math.Round(distanceTo / 3.0));

            return(new FireTarget(preferredFireTargetCoord.Value, travelTime, 0, FireTargetType.Explicit));
        }
        public CollectableBarrel FindNearestBarrelToCollect(TurnState turnState, int fcoord, 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    bestDist   = int.MaxValue;
            Barrel bestBarrel = null;

            foreach (var barrel in turnState.barrels)
            {
                if (used == null || !used.Contains(barrel.id))
                {
                    var dist = FastCoord.Distance(fcoord, barrel.fcoord);
                    if (dist < bestDist)
                    {
                        int hitTurns;
                        if (barrelsHitTurns.TryGetValue(barrel.id, out hitTurns))
                        {
                            continue;
                        }
                        bestBarrel = barrel;
                        bestDist   = dist;
                    }
                }
            }
            return(bestBarrel == null ? null : new CollectableBarrel {
                barrel = bestBarrel, dist = bestDist
            });
        }
 private void BuildMineForecast(int mineCoord)
 {
     if (!FastCoord.IsInsideMap(mineCoord))
     {
         return;
     }
     for (var depth = 0; depth < Settings.NAVIGATION_PATH_DEPTH; depth++)
     {
         var turnForecast = GetTurnForecast(depth);
         turnForecast.mineDamageCoordMap[mineCoord] += Constants.MINE_DAMAGE;
         if (turnForecast.cannonballCoordsMap[mineCoord])
         {
             for (int orientation = 0; orientation < 6; orientation++)
             {
                 var neighbor = FastCoord.Neighbor(mineCoord, orientation);
                 turnForecast.nearMineDamageCoordMap[neighbor] += Constants.NEAR_MINE_DAMAGE;
             }
         }
     }
 }
Example #17
0
 public void NearMap_ReturnsValidFastCoord()
 {
     for (int x = -1; x < Constants.MAP_WIDTH + 1; x++)
     {
         {
             var coord     = new Coord(x, -1);
             var fastCoord = FastCoord.Create(coord);
             fastCoord.Should().BeGreaterOrEqualTo(0);
             var actual = FastCoord.ToCoord(fastCoord);
             actual.Should().Be(coord);
         }
         {
             var coord     = new Coord(x, Constants.MAP_HEIGHT);
             var fastCoord = FastCoord.Create(coord);
             fastCoord.Should().BeGreaterOrEqualTo(0);
             var actual = FastCoord.ToCoord(fastCoord);
             actual.Should().Be(coord);
         }
     }
     for (int y = -1; y < Constants.MAP_HEIGHT + 1; y++)
     {
         {
             var coord     = new Coord(-1, y);
             var fastCoord = FastCoord.Create(coord);
             fastCoord.Should().BeGreaterOrEqualTo(0);
             var actual = FastCoord.ToCoord(fastCoord);
             actual.Should().Be(coord);
         }
         {
             var coord     = new Coord(Constants.MAP_WIDTH, y);
             var fastCoord = FastCoord.Create(coord);
             fastCoord.Should().BeGreaterOrEqualTo(0);
             var actual = FastCoord.ToCoord(fastCoord);
             actual.Should().Be(coord);
         }
     }
 }
        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 static bool GoForward(int phaseSpeed, ref int coord, ref int bow, ref int stern, ref int orientation, ref int speed, ref int ncoord, ref int nbow, ref int nstern)
        {
            ncoord = coord;
            nbow   = bow;
            nstern = stern;

            if (phaseSpeed > speed)
            {
                return(true);
            }

            var newCoord = bow;

            if (FastCoord.IsInsideMap(newCoord))
            {
                ncoord = newCoord;
                nbow   = FastCoord.Neighbor(ncoord, orientation);
                nstern = FastCoord.Neighbor(ncoord, (orientation + 3) % 6);
                return(true);
            }

            speed = 0;
            return(false);
        }
        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);
        }
        public static int CalcCost(int startCoord, int endCoord, int enemyCoord)
        {
            if (FastCoord.Distance(startCoord, endCoord) < FastCoord.Distance(enemyCoord, endCoord))
            {
                return(0);
            }

            var startX = FastCoord.GetX(startCoord);
            var startY = FastCoord.GetY(startCoord);

            var endX = FastCoord.GetX(endCoord);
            var endY = FastCoord.GetY(endCoord);

            var deltaX = endX - startX;
            var deltaY = endY - startY;

            var enemyDist = int.MaxValue;

            for (int i = 1; i < 10; i++)
            {
                var x = startX + deltaX * i / 10;
                var y = startY + deltaY * i / 10;

                var dist = FastCoord.Distance(enemyCoord, FastCoord.Create(x, y));
                if (dist < enemyDist)
                {
                    enemyDist = dist;
                }
            }

            if (enemyDist >= costs.Length)
            {
                return(0);
            }
            return(costs[enemyDist]);
        }
Example #22
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)
                };
            }
        }
Example #23
0
 public override string ToString()
 {
     return($"{nameof(ftarget)}: {FastCoord.ToCoord(ftarget)}, {nameof(rum)}: {rum}, {nameof(turns)}: {turns}, {nameof(TargetType)}: {TargetType}");
 }
        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);
        }
 public override string ToString()
 {
     return($"enemiesMov: {string.Join("; ", enemyShipsMovedPositions.Select(FastShipPosition.ToShipPosition))} | " +
            $"enemiesFin: {string.Join("; ", enemyShipsFinalPositions.Select(FastShipPosition.ToShipPosition))} | " +
            $"myships: {string.Join("; ", myShipsPositions.Select(FastShipPosition.ToShipPosition))} | " +
            $"cannonballs: {string.Join("; ", cannonballCoordsMap.Select((b, i) => new { b, i }).Where(x => x.b).Select(x => FastCoord.ToCoord(x.i)))} | " +
            $"mines: {string.Join("; ", mineDamageCoordMap.Select((d, i) => new { d, i }).Where(x => x.d != 0).Select(x => FastCoord.ToCoord(x.i)))} | " +
            $"nearMines: {string.Join("; ", nearMineDamageCoordMap.Select((d, i) => new { d, i }).Where(x => x.d != 0).Select(x => FastCoord.ToCoord(x.i)))}");
 }
        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)
                };
            }
        }
Example #27
0
 public void Fire(int fastCoord)
 {
     Console.WriteLine($"FIRE {FastCoord.GetX(fastCoord)} {FastCoord.GetY(fastCoord)} FIRE {FastCoord.GetX(fastCoord)} {FastCoord.GetY(fastCoord)}");
 }
        private static void Main22(string[] args)
        {
            FastCoord.Init();

            var coordsX = new List <Coord>();

            for (int x = -1; x < Constants.MAP_WIDTH + 1; x++)
            {
                for (int y = -1; y < Constants.MAP_HEIGHT + 1; y++)
                {
                    var coord = new Coord(x, y);
                    coordsX.Add(coord);
                }
            }

            var indexes = Enumerable.Range(0, coordsX.Count).ToArray();
            var seed    = new Random().Next();

            Console.Out.WriteLine($"Seed: {seed}");
            var random = new Random(seed);

            for (int i = 0; i < indexes.Length; i++)
            {
                var r   = random.Next(i, indexes.Length);
                var tmp = indexes[r];
                indexes[r] = indexes[i];
                indexes[i] = tmp;
            }

            var coords     = indexes.Select(i => coordsX[i]).ToArray();
            var fastCoords = indexes.Select(i => FastCoord.Create(coords[i])).ToArray();

            var ships     = coords.Select(c => new ShipPosition(c, random.Next(6), random.Next(3))).ToArray();
            var fastShips = ships.Select(FastShipPosition.Create).ToArray();

            var stopwatch = Stopwatch.StartNew();

            Console.Out.WriteLine("IsInsideMap");
            stopwatch.Restart();
            int ind = 0;

            for (int i = 0; i < 10000000; i++)
            {
                coords[ind++].IsInsideMap();
                if (ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 10000000; i++)
            {
                FastCoord.IsInsideMap(fastCoords[ind++]);
                if (ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            Console.Out.WriteLine("DistanceTo");
            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 10000000; i++)
            {
                coords[ind++].DistanceTo(coords[0]);
                if (ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 10000000; i++)
            {
                FastCoord.Distance(fastCoords[ind++], fastCoords[0]);
                if (ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            Console.Out.WriteLine("Neighbor");
            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 10000000; i++)
            {
                coords[ind].Neighbor(0);
                coords[ind].Neighbor(1);
                coords[ind].Neighbor(2);
                coords[ind].Neighbor(3);
                coords[ind].Neighbor(4);
                coords[ind].Neighbor(5);
                if (++ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 10000000; i++)
            {
                FastCoord.Neighbor(fastCoords[ind], 0);
                FastCoord.Neighbor(fastCoords[ind], 1);
                FastCoord.Neighbor(fastCoords[ind], 2);
                FastCoord.Neighbor(fastCoords[ind], 3);
                FastCoord.Neighbor(fastCoords[ind], 4);
                FastCoord.Neighbor(fastCoords[ind], 5);
                if (++ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            Console.Out.WriteLine("ShipDistanceTo");
            var shipPosition = new ShipPosition(coords[0], 0, 0);

            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 10000000; i++)
            {
                shipPosition.DistanceTo(coords[ind]);
                if (++ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            var fastShipPosition = FastShipPosition.Create(shipPosition);

            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 10000000; i++)
            {
                FastShipPosition.DistanceTo(fastShipPosition, fastCoords[ind++]);
                if (ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            Console.Out.WriteLine("Collides");
            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 10000000; i++)
            {
                shipPosition.Collides(coords[ind++]);
                if (ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 10000000; i++)
            {
                FastShipPosition.Collides(fastShipPosition, fastCoords[ind++]);
                if (ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            Console.Out.WriteLine("CollidesShip");
            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 10000000; i++)
            {
                shipPosition.CollidesShip(ships[ind++]);
                if (ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 10000000; i++)
            {
                FastShipPosition.CollidesShip(fastShipPosition, fastShips[ind++]);
                if (ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            Console.Out.WriteLine("Move");
            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 1_000_000; i++)
            {
                foreach (var moveCommand in ShipMoveCommands.all)
                {
                    ships[ind].Apply(moveCommand);
                }
                if (++ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 1_000_000; i++)
            {
                foreach (var moveCommand in ShipMoveCommands.all)
                {
                    var moved = FastShipPosition.Move(fastShips[ind], moveCommand);
                    FastShipPosition.GetMovedPosition(moved);
                    FastShipPosition.GetFinalPosition(moved);
                }
                if (++ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 1_000_000; i++)
            {
                foreach (var moveCommand in ShipMoveCommands.all)
                {
                    uint myMovement;
                    uint otherMovement;
                    CollisionChecker.Move(fastShips[ind], moveCommand, fastShips[(ind + 1) % indexes.Length], moveCommand, out myMovement, out otherMovement);
                    FastShipPosition.GetMovedPosition(myMovement);
                    FastShipPosition.GetFinalPosition(myMovement);
                }
                if (++ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);
        }
 public void Test()
 {
     Console.Out.WriteLine(WayEvaluator.CalcCost(FastCoord.Create(19, 16), FastCoord.Create(13, 5), FastCoord.Create(17, 8)));
     Console.Out.WriteLine(WayEvaluator.CalcCost(FastCoord.Create(19, 16), FastCoord.Create(13, 5), FastCoord.Create(16, 8)));
     Console.Out.WriteLine(WayEvaluator.CalcCost(FastCoord.Create(19, 16), FastCoord.Create(13, 5), FastCoord.Create(18, 8)));
 }
 public void SetUp()
 {
     FastCoord.Init();
 }