public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            BattleShips battleShips = new BattleShips(Height, Width, Player1, Player2)
            {
                ShipsCanTouch = ShipsCanTouch
            };

            battleShips.AddDefaultShipsToPlayerShipList(Player1);
            battleShips.AddDefaultShipsToPlayerShipList(Player2);

            battleShips.PlaceShipsAutomatically(Player1);
            battleShips.PlaceShipsAutomatically(Player2);

            var save = battleShips.CreateBattleShipsSave(GameName);

            if (_context.BattleShipsSaves.Any(e => e.SaveName == GameName))
            {
                ModelState.AddModelError("GameName", "A save game with this name already exists.");
                return(Page());
            }

            await _context.BattleShipsSaves.AddAsync(save);

            await _context.SaveChangesAsync();

            return(RedirectToPage("/PlayGame", new { id = save.Id }));
        }
示例#2
0
        private static string HumanVsAiNewGame()
        {
            var menu = new Menu(MenuLevel.Level1);

            menu.AddNewMenuItem(new MenuItem("Save game", "S", null));

            (int height, _, _) = AskForUserInput("Please enter the board height", 20, 10, menu);
            (int width, _, _)  = AskForUserInput("Please enter the board width", 20, 10, menu);

            Player player1 = new Player();
            Player player2 = new Player();

            BattleShips battleShips = new BattleShips(height, width, player1, player2);

            Console.WriteLine("Enter 'T' if the ships can touch. Enter any other symbol if not.");
            Console.Write(">");
            var touchRules = Console.ReadLine()?.Trim().ToUpper() ?? "E";

            if (touchRules != "T")
            {
                battleShips.ShipsCanTouch = false;
            }

            Console.Clear();
            Console.WriteLine("Player 1 ships");
            SetUpPlayerShips(battleShips, player1, menu);
            PlacePlayerShipsOnBoard(battleShips, player1, menu);

            Console.Clear();
            battleShips.AddDefaultShipsToPlayerShipList(player2);
            battleShips.PlaceShipsAutomatically(player2);
            battleShips.GameType = GameType.HumanVsAi;

            var userChoice = "";

            userChoice = HumanVsHumanMainGame(battleShips, menu);

            return(userChoice !);
        }
示例#3
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);
                }
            }
        }