示例#1
0
        public void Move(ShipMoveCommand command)
        {
            switch (command)
            {
            case ShipMoveCommand.Faster:
                Faster();
                break;

            case ShipMoveCommand.Slower:
                Slower();
                break;

            case ShipMoveCommand.Port:
                Port();
                break;

            case ShipMoveCommand.Starboard:
                Starboard();
                break;

            default:
                Wait();
                break;
            }
        }
        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);
        }
 public ShipPathChainItem Next(int nextPosition, ShipMoveCommand moveCommand, int ftarget, int nextDamage)
 {
     return(new ShipPathChainItem(
                this,
                moveCommand,
                nextPosition,
                depth + 1,
                prev == null ? moveCommand : startCommand,
                ftarget,
                pathDamage + nextDamage));
 }
        public List <ShipPosition> Apply(ShipMoveCommand moveCommand)
        {
            var result   = new List <ShipPosition>();
            var newSpeed = speed;

            switch (moveCommand)
            {
            case ShipMoveCommand.Faster:
                newSpeed++;
                break;

            case ShipMoveCommand.Slower:
                newSpeed--;
                break;
            }
            if (newSpeed > Constants.MAX_SHIP_SPEED)
            {
                newSpeed = Constants.MAX_SHIP_SPEED;
            }
            if (newSpeed < 0)
            {
                newSpeed = 0;
            }
            var movedShip = new ShipPosition(coord, orientation, newSpeed);

            for (var sp = 1; sp <= newSpeed; sp++)
            {
                var newShip = new ShipPosition(movedShip.coord.Neighbor(orientation), orientation, newSpeed);
                if (!newShip.IsInsideMap())
                {
                    movedShip = new ShipPosition(movedShip.coord, orientation, 0);
                    break;
                }
                movedShip = newShip;
            }
            result.Add(movedShip);
            switch (moveCommand)
            {
            case ShipMoveCommand.Port:
                movedShip = new ShipPosition(movedShip.coord, (orientation + 1) % 6, movedShip.speed);
                break;

            case ShipMoveCommand.Starboard:
                movedShip = new ShipPosition(movedShip.coord, (orientation + 5) % 6, movedShip.speed);
                break;
            }
            result.Add(movedShip);
            return(result);
        }
 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);
     }
 }
        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 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;
            }
        }
        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);
        }
示例#9
0
 public static uint Move(int fastShipPosition, ShipMoveCommand moveCommand)
 {
     return(moves[fastShipPosition | ((int)moveCommand << positionBits)]);
 }
        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);
        }