示例#1
0
 private static void ShowMessages()
 {
     Errors.WriteErrorMessage();
     InfoMessages.WriteInfoMessage();
 }
示例#2
0
        public static void Main()
        {
            InitializeGame();
            int  ShipChosen = 0;
            bool isInitialPositionChosen = false;
            bool isShipPlacedOnBoard     = false;
            bool isGameActive            = false;

            comPlayer = (player2.GetType() == player1.GetType()) ? false : true;
            Player playerVariable = player1;

            while (playerVariable.areShipsEmpty() == false)
            {
                Console.Clear();
                Board.DisplayBoard(playerVariable.board);
                if (playerVariable.areShipsFull())
                {
                    Console.WriteLine("Player {0}, you must choose where to put your ships", (playerVariable == player1) ? 1 : 2);
                }
                Console.WriteLine("Which ship would you like to place?");
                if (playerVariable.areShipsEmpty() == false)
                {
                    ShipChosen = 0;
                    isInitialPositionChosen = false;
                    isShipPlacedOnBoard     = false;
                }
                while (ShipChosen == 0)
                {
                    Errors.WriteErrorMessage();
                    playerVariable.listShips();
                    string shipChoice = Console.ReadLine().ToLower();
                    ShipChosen = SelectShip(shipChoice, playerVariable);
                }
                Console.WriteLine("Select starting coordinates in the following format: x,y");

                int shipLength = playerVariable.realShips[ShipChosen - 1].Length;

                while (!isInitialPositionChosen)
                {
                    Errors.WriteErrorMessage();
                    try
                    {
                        string playerStartCoords = Console.ReadLine().ToLower();
                        isInitialPositionChosen = PlaceShip(playerStartCoords, playerVariable);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("You can only enter numbers between 0 and 9, separated by a comma to set the start point of your ship.");
                        Console.WriteLine("Try again.");
                    }
                }
                Console.WriteLine("Excellent, now choose whether you want it facing N, E, S, or W.");
                while (!isShipPlacedOnBoard)
                {
                    Errors.WriteErrorMessage();
                    try
                    {
                        string direction = Console.ReadLine().ToLower();
                        isShipPlacedOnBoard = playerVariable.board.SetShipDirection(xCoord, yCoord, direction, shipLength, playerVariable);
                    }
                    catch (FormatException)
                    {
                        Errors.ErrorMessage = "Please only write 'N', 'E', 'S', or 'W'";
                    }
                }
                if (playerVariable.areShipsEmpty() == true)
                {
                    Console.Clear();
                    Board.DisplayBoard(playerVariable.board);
                    Errors.WriteErrorMessage();
                    InfoMessages.WriteInfoMessage();
                    if (playerVariable == player1 && !comPlayer)
                    {
                        Console.WriteLine("Look at your board and press any key + Enter to let player 2 enter their ships");
                    }
                    else
                    {
                        Console.WriteLine("Look at your board and press any key + Enter to start the game!");
                    }
                    Console.ReadLine();
                    Console.Clear();
                }
                if (player1.areShipsEmpty() && playerVariable == player1 && !comPlayer)
                {
                    playerVariable = player2;
                }
                else if (player1.areShipsEmpty() && comPlayer)
                {
                    ComputerPlayer.SetUpComputerShips((ComputerPlayer)player2);
                    Errors.ErrorMessage = "";
                }
            }
            isGameActive = true;
            Player activePlayer   = player1;
            Player inactivePlayer = player2;

            while (isGameActive)
            {
                bool wasTurnSuccessful = false;
                InfoMessages.InfoMessage += "Player " + ((activePlayer == player1) ? "1" : "2") + ", it is your turn. Select coordinate (x,y) to attack.";
                ClearBoardAndShowMessages(activePlayer);
                try
                {
                    wasTurnSuccessful = (PlayerAttack(activePlayer, inactivePlayer, Console.ReadLine()));
                }
                catch (Exception)
                {
                    FormatErrorMessage();
                }
                if (wasTurnSuccessful)
                {
                    ClearBoardAndShowMessages(activePlayer);
                }
                if (DidAnyoneWin(inactivePlayer))
                {
                    Console.WriteLine("Congrats Player " + ((activePlayer == player1) ? "1" : "2") + ", you won!!!");
                    isGameActive = false;
                    Console.ReadLine();
                    break;
                }
                if (wasTurnSuccessful)
                {
                    PressEnterToContinue();
                }
                if (!comPlayer && wasTurnSuccessful)
                {
                    activePlayer   = (activePlayer == player1) ? player2 : player1;
                    inactivePlayer = (inactivePlayer == player2) ? player1 : player2;
                }
                else if (comPlayer && wasTurnSuccessful)
                {
                    ComputerPlayer.Attack((ComputerPlayer)player2);
                }
            }
        }