예제 #1
0
        public async Task <IActionResult> Onpost()
        {
            if (ModelState.IsValid)
            {
                GameEngine.Game gameEngine = new GameEngine.Game(_context);

                gameEngine.Settings = new GameSettings();

                gameEngine.InitializeNewBoard(Options.BoardWidth, Options.BoardHeight);

                var entity = new Domain.GameSave()
                {
                    SaveName      = Options.SaveName,
                    BoardHeight   = Options.BoardHeight,
                    BoardWidth    = Options.BoardWidth,
                    NumberOfMines = Options.NumberOfMines,
                    BoardState    = gameEngine.GetSerializedGameState()
                };

                _context.GameSave.Add(entity);
                await _context.SaveChangesAsync();

                return(RedirectToPage("./PlayGame", new{ GameId = entity.GameSaveId }));
            }

            return(Page());
        }
        public ActionResult OnPost()
        {
            if (ModelState.IsValid)
            {
                var savedGames = new List <string>();
                foreach (var gameSave in _context.GameSave)
                {
                    savedGames.Add(gameSave.SaveName);
                }

                if (savedGames.Contains(SaveFileName))
                {
                    return(RedirectToPage(new { dif = Difficulty, SaveFileName }));
                }

                GameEngine.Game gameEngine = new GameEngine.Game(_context);
                //you need to fix the functions, maybe remove them entirely.
                switch (Difficulty)
                {
                case "1":
                    gameEngine.Settings = new GameSettings();

                    break;

                case "2":
                    gameEngine.Settings = new GameSettings()
                    {
                        BoardHeight   = 16,
                        BoardWidth    = 16,
                        NumberOfMines = 40
                    };
                    break;

                case "3":
                    gameEngine.Settings = new GameSettings()
                    {
                        BoardHeight   = 24,
                        BoardWidth    = 24,
                        NumberOfMines = 99
                    };
                    break;

                default:
                    gameEngine.Settings = new GameSettings();
                    break;
                }

                gameEngine.InitializeNewBoard(gameEngine.Settings.BoardWidth, gameEngine.Settings.BoardHeight);

                var entity = new Domain.GameSave()
                {
                    SaveName      = gameEngine.Settings.SaveName,
                    BoardHeight   = gameEngine.Settings.BoardHeight,
                    BoardWidth    = gameEngine.Settings.BoardWidth,
                    NumberOfMines = gameEngine.Settings.NumberOfMines,
                    BoardState    = gameEngine.GetSerializedGameState()
                };
                _context.GameSave.Add(entity);
                _context.SaveChangesAsync();
                return(RedirectToPage("./PlayGame", new { GameId = entity.GameSaveId }));
            }

            return(RedirectToPage("../Index"));
        }