Beispiel #1
0
        public static void GetNextFiringCoordinate(FiringDirection dir,
                                                   int rowNumber, int columnNumber, out int newRowNumber, out int newColumnNumber)
        {
            switch (dir)
            {
            case FiringDirection.North:
                newRowNumber    = rowNumber - 1;
                newColumnNumber = columnNumber;
                break;

            case FiringDirection.South:
                newRowNumber    = rowNumber + 1;
                newColumnNumber = columnNumber;
                break;

            case FiringDirection.East:
                newRowNumber    = rowNumber;
                newColumnNumber = columnNumber + 1;
                break;

            default:
                newRowNumber    = rowNumber;
                newColumnNumber = columnNumber - 1;
                break;
            }
        }
Beispiel #2
0
        public static FiringDirection GetRandPerpendicularDirection(FiringDirection dir)
        {
            Random rand = new Random();
            int    r    = rand.Next(1);

            if (dir == FiringDirection.East || dir == FiringDirection.West)
            {
                if (r == 0)
                {
                    return(FiringDirection.North);
                }
                else
                {
                    return(FiringDirection.South);
                }
            }
            else
            {
                if (r == 0)
                {
                    return(FiringDirection.West);
                }
                else
                {
                    return(FiringDirection.East);
                }
            }
        }
Beispiel #3
0
        public static FiringDirection GetOppositeDirection(FiringDirection dir)
        {
            switch (dir)
            {
            case FiringDirection.North:
                return(FiringDirection.South);

            case FiringDirection.South:
                return(FiringDirection.North);

            case FiringDirection.East:
                return(FiringDirection.West);

            default:
                return(FiringDirection.East);
            }
        }
    Vector3 GetFiringDirectionForTargetingMode(FiringDirection mode)
    {
        switch (mode)
        {
        case FiringDirection.VECTOR:
            return(fixVector.normalized);

        case FiringDirection.FORWARD:
            return(transform.forward);

        case FiringDirection.PLAYERDIRECT:
            return(GetVectorToNearestLivingPlayer());

        case FiringDirection.PLAYERPREDICT:
            return(GetPrecictVectorToNearestLivingPlayer());

        default:
            throw new UnityException("unsupported targetingmode \"" + mode.ToString() + "\"");
        }
    }
Beispiel #5
0
        private void FireOpponentMissileDifficult()
        {
            // fire opponent missile
            bool done = false;

            while (done == false)
            {
                // start firing randomly until we hit a ship
                if (huntMode == false)
                {
                    int rowNumber            = rand.Next(Battlefield.FIELD_SIZE);
                    int columnNumber         = rand.Next(Battlefield.FIELD_SIZE);
                    FireMissileStatus status = UserBoard.FireMissile(columnNumber, rowNumber);
                    if (status == FireMissileStatus.Hit || status == FireMissileStatus.Miss)
                    {
                        done = true;
                    }
                    // If this is the first hit, then remember the coordinates and start the algorithm
                    if (status == FireMissileStatus.Hit)
                    {
                        startingColumn      = columnNumber;
                        startingRow         = rowNumber;
                        curColumn           = columnNumber;
                        curRow              = rowNumber;
                        huntMode            = true;
                        sunkenShips         = UserBoard.GetSunkenShipCount();
                        dirSelected         = false;
                        numDirectionChanges = 0;
                    }
                }
                else
                {
                    // Do some intelligent firing since we are already in hunt mode

                    if (dirSelected == false)
                    {
                        huntDir     = DirectionHelper.GetRandDirection();
                        dirSelected = true;
                        numDirectionChanges++;
                    }
                    int tempRow    = 0;
                    int tempColumn = 0;
                    DirectionHelper.GetNextFiringCoordinate(huntDir, curRow, curColumn, out tempRow, out tempColumn);
                    FireMissileStatus status = UserBoard.FireMissile(tempColumn, tempRow);
                    if (status == FireMissileStatus.Hit || status == FireMissileStatus.Miss)
                    {
                        done = true;
                        int curSunkenShips = UserBoard.GetSunkenShipCount();
                        if (sunkenShips != curSunkenShips)
                        {
                            // we sunk a battle ship, so we are done with this algorithm
                            // start over again with another random firing
                            huntMode = false;
                        }
                        else
                        {
                            if (status == FireMissileStatus.Hit)
                            {
                                curRow    = tempRow;
                                curColumn = tempColumn;
                            }
                            else if (status == FireMissileStatus.Miss)
                            {
                                // we exhausted all the hits in one direction, now pick a different direction
                                if (numDirectionChanges == 1 || numDirectionChanges == 3)
                                {
                                    // we are moving in one direction, we still need to go to opposite dir
                                    huntDir = DirectionHelper.GetOppositeDirection(huntDir);
                                }
                                else
                                {
                                    // we exhausted opposite directions, so go perpendicular
                                    huntDir = DirectionHelper.GetRandPerpendicularDirection(huntDir);
                                }
                                numDirectionChanges++;
                                curRow    = startingRow;
                                curColumn = startingColumn;
                            }
                        }
                    }
                    else if (status == FireMissileStatus.AlreadyFired || status == FireMissileStatus.Illegal)
                    {
                        // we exhausted all the hits in one direction, now pick a different direction
                        if (numDirectionChanges == 1 || numDirectionChanges == 3)
                        {
                            // we are moving in one direction, we still need to go to opposite dir
                            huntDir = DirectionHelper.GetOppositeDirection(huntDir);
                        }
                        else
                        {
                            // we exhausted opposite directions, so go perpendicular
                            huntDir = DirectionHelper.GetRandPerpendicularDirection(huntDir);
                        }
                        numDirectionChanges++;
                        curRow    = startingRow;
                        curColumn = startingColumn;

                        // some ship placements might make this algorithm get stuck in an infinite loop
                        // let us break it manually and start over again
                        if (numDirectionChanges > 6)
                        {
                            Console.WriteLine("Coming out of a bind ... :-)");
                            huntMode = false;
                        }
                    }
                }
            }
        }