コード例 #1
0
        private static string HumanVsHumanMainGame(BattleShips battleShips, Menu menu)
        {
            int    x;
            int    y;
            string?userChoice;
            bool   userSaved;

            Player currentPlayer;

            do
            {
                currentPlayer = battleShips.Player1Turn ? battleShips.Player1 : battleShips.Player2;

                if (battleShips.GameType == GameType.HumanVsHuman || battleShips.Player1Turn)
                {
                    BattleShipsUI.PrintBoard(battleShips, currentPlayer);
                }

                menu.DisplayCustomMenuItems();
                menu.DisplayPredefinedMenuItems();

                Console.WriteLine(battleShips.Player1Turn ? "Player 1's turn" : "Player 2's turn");

                if (battleShips.GameType == GameType.HumanVsHuman || battleShips.Player1Turn)
                {
                    (x, userChoice, userSaved) = AskForUserInput("Enter X coordinate", battleShips.Width, 1, menu);

                    if (userChoice != null)
                    {
                        break;
                    }

                    if (userSaved)
                    {
                        SaveGame(battleShips);
                        continue;
                    }

                    do
                    {
                        (y, userChoice, userSaved) = AskForUserInput("Enter Y coordinate", battleShips.Height, 1, menu);
                        if (!userSaved)
                        {
                            continue;
                        }
                        SaveGame(battleShips);
                        BattleShipsUI.PrintBoard(battleShips, currentPlayer);
                    } while (userSaved);

                    if (userChoice != null)
                    {
                        break;
                    }

                    var shipHasBeenHit = battleShips.FireAShot(currentPlayer, x - 1, y - 1);
                    BattleShipsUI.PrintBoard(battleShips, currentPlayer);
                    DisplayShotResult(shipHasBeenHit);
                }
                else
                {
                    var shipHasBeenHit = battleShips.FireAiShot();
                    BattleShipsUI.PrintBoard(battleShips, currentPlayer);
                    DisplayShotResult(shipHasBeenHit);
                }

                if (battleShips.Player1.HasLost || battleShips.Player2.HasLost)
                {
                    var message = battleShips.Player1.HasLost
                        ? "Player 2 has won the game!"
                        : "Player 1 has won the game!";
                    Console.WriteLine(message);
                    WaitForUserInput("Press any key to quit the game");
                    userChoice = "M";
                    break;
                }

                var turnMessage = battleShips.Player1Turn
                    ? "Player2's turn, enter any key to continue..."
                    : "Player1's turn, enter any key to continue...";
                WaitForUserInput(turnMessage);
                battleShips.Player1Turn = !battleShips.Player1Turn;
            } while (true);

            return(userChoice);
        }
コード例 #2
0
        private static void PlacePlayerShipsOnBoard(BattleShips battleShips, Player player, Menu menu)
        {
            string userShipPlacementChoice = "";

            do
            {
                Console.WriteLine("Press 'R' for random placement or press 'C' to place the ships yourself.");
                Console.Write(">");
                userShipPlacementChoice = Console.ReadLine()?.Trim().ToUpper() ?? "R";
            } while (userShipPlacementChoice != "R" && userShipPlacementChoice != "C");

            if (userShipPlacementChoice == "R")
            {
                battleShips.PlaceShipsAutomatically(player);
            }
            else
            {
                int nrOfShipsLeft = player.Ships.Count;

                foreach (var playerShip in player.Ships)
                {
                    BattleShipsUI.PrintBoard(battleShips, player);
                    Console.WriteLine($"Nr. of ships left to place: {nrOfShipsLeft}");
                    Console.WriteLine($"Ship width: {playerShip.Width}");
                    var shipOrientation = "";
                    do
                    {
                        Console.WriteLine("Ship orientation: Choose 'V' for vertical and 'H' for horizontal");
                        Console.Write(">");
                        shipOrientation = Console.ReadLine()?.Trim().ToUpper() ?? "V";
                    } while (shipOrientation != "V" && shipOrientation != "H");

                    Console.WriteLine($"Orientation: {shipOrientation}");
                    do
                    {
                        int startRow;
                        int startCol;

                        (startRow, _, _) = AskForUserInput("Choose the starting row coordinate for your ship",
                                                           battleShips.Height, 1, menu);
                        (startCol, _, _) = AskForUserInput("Choose the starting column coordinate for your ship",
                                                           battleShips.Width, 1, menu);

                        startRow--;
                        startCol--;

                        int endRow = startRow;
                        int endCol = startCol;

                        if (shipOrientation == "H")
                        {
                            for (var i = 1; i < playerShip.Width; i++)
                            {
                                endCol++;
                            }

                            playerShip.Orientation = ShipOrientation.Horizontal;
                        }
                        else
                        {
                            for (var i = 1; i < playerShip.Width; i++)
                            {
                                endRow++;
                            }

                            playerShip.Orientation = ShipOrientation.Horizontal;
                        }

                        if (endRow > battleShips.Height - 1 || endCol > battleShips.Width - 1)
                        {
                            Console.WriteLine("Ship ending row/column coordinate is out of bounds! Please try again");
                            continue;
                        }

                        List <Panel> affectedPanels = new List <Panel>();

                        if (!battleShips.ShipsCanTouch)
                        {
                            var orientation = shipOrientation == "H" ? 0 : 1;
                            battleShips.FindAffectedPanelsAroundTheShip(player, orientation, affectedPanels, startRow,
                                                                        startCol, endRow, endCol);
                        }
                        else
                        {
                            affectedPanels.AddRange(player.GameBoard.Range(startRow, startCol, endRow, endCol));
                        }

                        var shipPlacementPanels = player.GameBoard.Range(startRow, startCol, endRow, endCol);

                        if (affectedPanels.Any(x => x.IsOccupied))
                        {
                            var message = "A ship has already been placed here!";
                            Console.WriteLine(!battleShips.ShipsCanTouch
                                ? $"{message} Also the ships cannot touch! Please try again"
                                : $"{message} Please try again");
                            continue;
                        }

                        playerShip.StartCol = startCol;
                        playerShip.StartRow = startRow;
                        playerShip.EndCol   = endCol;
                        playerShip.EndRow   = endRow;

                        foreach (var panel in shipPlacementPanels)
                        {
                            panel.PanelState = playerShip.PanelState;
                        }

                        nrOfShipsLeft--;
                        break;
                    } while (true);
                }
            }
        }