public async Task <IActionResult> OnPostAsync(int id)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToPage("PlayGame", new { id }));
            }
            var(row, col) = await ParseCoordinatesAsync();

            var battleShipsSave = await _appDbContext.BattleShipsSaves.FirstOrDefaultAsync(e => e.Id == id);

            BattleShips = new BattleShips(battleShipsSave);

            ShotHitTheTarget = BattleShips.FireAShot(BattleShips.Player1Turn ? BattleShips.Player1 : BattleShips.Player2, col, row);

            BattleShips.Player1Turn = !BattleShips.Player1Turn;
            BattleShips.UpdateBattleShipsSave(battleShipsSave);

            _appDbContext.BattleShipsSaves.Update(battleShipsSave);
            await _appDbContext.SaveChangesAsync();

            return(RedirectToPage("PlayGame", "ShowShotResult", new { id, shotHit = ShotHitTheTarget }));
        }
示例#2
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);
        }