Пример #1
0
        private void PlaceShipsRandomly()
        {
            InitBoard.InitializeBoard(MyGame);
            ShipPlacement.RandomPlacement(MyGame, false);
            var optionsBuilder = new DbContextOptionsBuilder();

            optionsBuilder.UseMySQL(
                "server=localhost;database=battleship;user=root;password=toor");
            Ctx = new AppDbContext(optionsBuilder.Options);
            Ctx.Add(MyGame);
            ActionMenu.BombingMenu(MyGame, Ctx);
        }
Пример #2
0
        public static void PlacePlayerShipsOnBoard(Game game)
        {
            InitBoard.InitializeBoard(game);
            var count = 1;

            foreach (var ship in game.GameShips)
            {
                Console.Clear();
                Console.WriteLine(PrintBoard(game));
                string input;
                do
                {
                    Console.WriteLine($"Ships remaining: {game.NumberOfShips - count + 1}");
                    do
                    {
                        Console.WriteLine($"Current ship length: {ship.ShipLength}");
                        Console.WriteLine("Set layout: V vertical(|) or H horizontal(-)");
                        input = Console.ReadLine()?.Trim().ToUpper();
                        if (input == "H" || input == "V")
                        {
                            break;
                        }
                        Console.WriteLine("Invalid input!");
                    } while (true);

                    ship.ShipLayout = ShipLayout.Horizontal;
                    if (input == "V")
                    {
                        ship.ShipLayout = ShipLayout.Vertical;
                    }
                    Console.WriteLine("Set coordinates for this ship" +
                                      $" (size: {ship.ShipLength}, layout: {ship.ShipLayout}):");
                    Console.WriteLine("Coordinate row (left side number): ");
                    var x = IntegerInputHelper("x", ship.ShipLength, ship.ShipLayout, game);
                    Console.WriteLine("Coordinate column (top number): ");
                    var y = IntegerInputHelper("y", ship.ShipLength, ship.ShipLayout, game);
                    if (!ShipPlacement.CanPlaceShip(game, ship, x, y))
                    {
                        Console.WriteLine("Other ship at coordinates!");
                        continue;
                    }

                    InitBoard.PlaceShip(game, ship, x, y);
                    Console.WriteLine("Ship placed successfully!");
                    break;
                } while (true);

                count++;
            }
        }
Пример #3
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            InitBoard.InitializeBoard(Game);
            _context.Games.Add(Game);
            await _context.SaveChangesAsync();

            if (!StandardShips)
            {
                return(RedirectToPage("CustomShips", new { id = _context.Games.Last().GameId }));
            }

            return(RedirectToPage("ShipPlacement", new { id = _context.Games.Last().GameId }));
        }