示例#1
0
 public static bool PlayerAttack(Player activePlayer, Player inactivePlayer, string coords)
 {
     try
     {
         string[] coordArray = CleanUpCoords(coords).Split(' ');
         xCoord = int.Parse(coordArray[0]);
         yCoord = int.Parse(coordArray[1]);
         bool spaceIsEmpty      = activePlayer.enemyBoard.IsSpaceEmpty(xCoord, yCoord, activePlayer.enemyBoard);
         bool theirSpaceIsEmpty = inactivePlayer.board.IsSpaceEmpty(xCoord, yCoord, inactivePlayer.board);
         if (Board.IsOnBoard(xCoord, yCoord))
         {
             if (!spaceIsEmpty)
             {
                 return(WasAlreadyAttacked());
             }
             else if (theirSpaceIsEmpty)
             {
                 AttackMissed(activePlayer, inactivePlayer, xCoord, yCoord);
             }
             else
             {
                 ItWasAHit(activePlayer, inactivePlayer, xCoord, yCoord);
             }
             return(true);
         }
     }
     catch (FormatException) {}
     return(FormatErrorMessage());
 }
示例#2
0
        public bool SetShipDirection(int xCoord, int yCoord, string direction, int shipLength, Player player)
        {
            int xEndPoint = 0;
            int yEndPoint = 0;

            if (direction == "e" || direction == "w")
            {
                yEndPoint = ChangeDirectionIntoCoords(direction, shipLength) + yCoord;
            }
            else if (direction == "s" || direction == "n")
            {
                xEndPoint = ChangeDirectionIntoCoords(direction, shipLength) + xCoord;
            }
            else
            {
                Errors.ErrorMessage = "Please enter one of the 4 cardinal directions only.";
                return(false);
            }
            if (!Board.IsOnBoard(xEndPoint, yEndPoint))
            {
                Errors.ErrorMessage = "That would put the ship off of the board, please choose another direction.";
                return(false);
            }
            if (direction == "n")
            {
                xCoord -= (shipLength - 1);
            }
            if (direction == "w")
            {
                yCoord -= (shipLength - 1);
            }
            if (!NoShipsInPath(shipLength, player.board, xCoord, yCoord, yEndPoint))
            {
                return(false);
            }
            ship = (shipLength == 2) ? player.realShips[0] : player.realShips[1];
            string axis = "";

            for (int i = 0; i < shipLength; i++)
            {
                int addToYCoord = (yEndPoint == 0) ? 0 : i;
                int addToXCoord = (addToYCoord == 0) ? i : 0;
                direct = (addToYCoord == 0) ? yCoord : xCoord;
                axis   = (addToYCoord == 0) ? "y" : "x";
                player.board.boardArray[xCoord + addToXCoord, yCoord + addToYCoord] = 1;
                if (shipLength == 2)
                {
                    scoutArray[i] = ((axis == "y") ? xCoord + addToXCoord : yCoord + addToYCoord);
                }
                if (shipLength == 4)
                {
                    battleshipArray[i] = ((axis == "y") ? xCoord + addToXCoord : yCoord + addToYCoord);
                }
            }
            setShipArray(ship, ((shipLength == 2) ? scoutArray : battleshipArray), direct, axis);
            DisplayBoard(player.board);
            return(true);
        }
示例#3
0
 public static bool PlaceShip(string coords, Player player)
 {
     string[] coordArray = CleanUpCoords(coords).Split(' ');
     xCoord = int.Parse(coordArray[0]);
     yCoord = int.Parse(coordArray[1]);
     if (Board.IsOnBoard(xCoord, yCoord) && player.board.IsSpaceEmpty(xCoord, yCoord, player.board))
     {
         return(true);
     }
     else if (Board.IsOnBoard(xCoord, yCoord) == false)
     {
         Errors.ErrorMessage = "That is out of the range of the board.  Numbers must be between 0 and 9.";
         return(false);
     }
     else
     {
         Errors.ErrorMessage = "That space is already taken. Please choose another coordinate set.";
         return(false);
     }
 }