public void OnGet(int?GameId,
                          string?ShipId,
                          string?settings,
                          int?x, int?y,
                          bool?isNewShip,
                          string?shipType,
                          int?random,
                          bool?isRotate)
        {
            if (GameId == null && settings == "original")
            {
                GameBrain.CreateGame();
                GameBrain.CreateShips();
                GameBrain.GenerateField();
            }
            else
            {
                if (GameId != null)
                {
                    GameBrain.LoadGame(GameId.Value);
                }
            }
            GameBrain.GenerateField();
            GameBrain.ReCalcCounters();

            if (isNewShip == true && shipType != null)
            {
                GameBrain.CreateShip(shipType, true);
                GameBrain.Save();
            }

            if (ShipId != null)
            {
                if (isNewShip != true)
                {
                    GameBrain.SelectedShip = _context.ShipGameAssignments.Single(x => x.ShipName == ShipId && x.IsPlayer);
                    if (GameBrain.Game != null)
                    {
                        GameBrain.SelectedShip.Cells = GameBrain.Game.Cells.Where(x => x.IsPlayer && x.ShipName == GameBrain.SelectedShip.ShipName).ToList();
                    }
                }

                if (GameBrain.SelectedShip != null && GameBrain.Game != null)
                {
                    GameBrain.SelectedShip.Game_Id = GameBrain.Game.Id;
                    if (isRotate == true)
                    {
                        GameBrain.MoveShip(x, y, true, true);
                        GameBrain.SelectedShip.IsRotated = !GameBrain.SelectedShip.IsRotated;
                    }
                    else
                    {
                        GameBrain.MoveShip(x, y, true);
                    }
                }
            }


            if (random == 1)
            {
                GameBrain.DoRandom(true);
            }

            if (GameBrain.SelectedShip != null)
            {
                GameBrain.CreateFieldForBot(GameBrain.SelectedShip);
            }

            Game         = GameBrain.Game;
            SelectedShip = GameBrain.SelectedShip;

            _context.SaveChanges();
        }
예제 #2
0
파일: Program.cs 프로젝트: Wepled/RazorShip
        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("");
        }