Beispiel #1
0
        private static void SetUpPlayerShips(BattleShips battleShips, Player player, Menu menu)
        {
            string userInput;

            do
            {
                Console.WriteLine("Enter 'D' for a default ship setup or 'C' to create custom ships.");
                Console.Write(">");
                userInput = Console.ReadLine()?.Trim().ToUpper() ?? "D";
            } while (userInput != "D" && userInput != "C");

            if (userInput == "D")
            {
                battleShips.AddDefaultShipsToPlayerShipList(player);
            }
            else
            {
                do
                {
                    Console.WriteLine($"({player.Ships.Count + 1}) Enter ship name: ");
                    Console.Write(">");
                    var shipName = Console.ReadLine()?.Trim().ToUpper() ?? "Default";
                    var(shipWidth, _, _) = AskForUserInput($"({player.Ships.Count + 1}) Enter ship width",
                                                           battleShips.Width >= battleShips.Height ? battleShips.Width : battleShips.Height, 1, menu);
                    battleShips.AddShipToPlayerShipList(player, new Ship()
                    {
                        Name = shipName, Width = shipWidth
                    });
                } while (player.Ships.Count < 5);
            }
        }
Beispiel #2
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

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

            foreach (var player1Ship in Player1Ships)
            {
                battleShips.AddShipToPlayerShipList(Player1, player1Ship);
            }

            foreach (var player2Ship in Player2Ships)
            {
                battleShips.AddShipToPlayerShipList(Player2, player2Ship);
            }

            battleShips.ShipsCanTouch = ShipsCanTouch;
            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 }));
        }