Beispiel #1
0
        static SeaBattle LoadGameAction(SeaBattle game)
        {
            Console.WriteLine("Games, Which to load");
            int         i     = 0;
            List <Game> games = game.GetGames();

            foreach (Game g in games)
            {
                i++;
                Console.WriteLine("[" + i + "] " + g.Game_Name);
            }
            game.LoadGame(games[int.Parse(Console.ReadLine()) - V].Id);
            return(game);
        }
Beispiel #2
0
        static string SeaBattle()
        {
            var dbOptions = new DbContextOptionsBuilder <BattleShipDbContext>().UseSqlServer(
                "Server = (localdb)\\mssqllocaldb; Database = BattleShipDbContext; Trusted_Connection = True; MultipleActiveResultSets = true"
                ).Options;

            using var dbCtx = new BattleShipDbContext(dbOptions);
            SeaBattle?game = new SeaBattle(dbCtx);
            Menu?     menu = new Menu(MenuLevel.Level0);

            if (game != null)
            {
                menu.AddMenuItem(new MenuItem(
                                     $"Player Place a ship",
                                     userChoice: "p",
                                     () =>
                {
                    if (game.Game != null)
                    {
                        var(x, y) = GetMoveCordinates(game);
                        game.CreateShip(GetshipType(), true);
                        game.MoveShip(x, y, true, isRotate());
                        if (game.SelectedShip != null)
                        {
                            game.CreateFieldForBot(game.SelectedShip);
                            game.Save();
                            SeabBattleConsoleUI.DrawBoard(game.Game.Cells.Where(g => g.IsPlayer).ToList(), game);
                        }
                        else
                        {
                            Console.WriteLine("Do not working(");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Please Create Game or load");
                    }
                    return("");
                })
                                 );
                menu.AddMenuItem(new MenuItem(
                                     $"Random",
                                     userChoice: "random",
                                     () =>
                {
                    if (game.Game != null)
                    {
                        game.DoRandom(true);
                        if (game.SelectedShip != null)
                        {
                            game.CreateFieldForBot(game.SelectedShip);
                            game.Save();
                            SeabBattleConsoleUI.DrawBoard(game.Game.Cells.Where(g => g.IsPlayer).ToList(), game);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Please Create Game or load");
                    }
                    return("");
                })
                                 );
                menu.AddMenuItem(new MenuItem(
                                     $"Play game",
                                     userChoice: "play",
                                     () =>
                {
                    if (game.Game != null)
                    {
                        return(PlayBattle(game));
                    }
                    else
                    {
                        Console.WriteLine("Please Create Game or load");
                        return("");
                    }
                })
                                 );

                menu.AddMenuItem(new MenuItem(
                                     $"Create game with original settings",
                                     userChoice: "s",
                                     () =>
                {
                    game.CreateGame();
                    return("");
                })
                                 );
                menu.AddMenuItem(new MenuItem(
                                     $"Load game",
                                     userChoice: "l",
                                     () =>
                {
                    game = LoadGameAction(game);
                    return("");
                })
                                 );
                menu.AddMenuItem(new MenuItem(
                                     $"Create game with custom settings",
                                     userChoice: "e",
                                     () =>
                {
                    Game gameToCreate            = new Game();
                    gameToCreate.Game_Name       = "CustomGame_" + game.GetGames().Count();
                    gameToCreate.Width           = GetData("Width", V);
                    gameToCreate.Heigth          = GetData("Heigth", V);
                    gameToCreate.BattleshipCount = GetData("Battleship", V);
                    gameToCreate.CruiserCount    = GetData("Cruiser", V);
                    gameToCreate.SubmarineCount  = GetData("Submarine", V);
                    gameToCreate.PatrolCount     = GetData("Patrol", V);
                    gameToCreate.CarrierCount    = GetData("Carrier", V);
                    gameToCreate.CanGoToAnother  = IsShipsCanGo();
                    game.SaveGame(gameToCreate);
                    game.Game       = gameToCreate;
                    game.Game.Cells = game.GenerateField();
                    Ship shipTocreate;

                    foreach (var shiptype in game.ShipsTypes)
                    {
                        for (int i = 0; i < 2; i++)
                        {
                            shipTocreate          = game.CreateCustomShip(shiptype);
                            shipTocreate.Length   = GetData(shiptype, 2);
                            shipTocreate.IsPlayer = i == V;
                            game.SaveShip(shipTocreate);
                        }
                    }
                    game.LoadGame(game.Game.Id);
                    return("");
                })
                                 );

                var userChoice = menu.RunMenu();

                return(userChoice);
            }
            return("");
        }