예제 #1
0
        public async Task <ActionResult> OnGet(int?gameId, int?col, int?row, bool?over, bool?win)
        {
            if (win != null)
            {
                WinGame = win.Value;
            }

            if (over != null)
            {
                GameOver = over.Value;
            }

            GameSettings = await _context.GameSettingses.ToListAsync();

            if (gameId == null)
            {
                return(RedirectToPage("./StartGame"));
            }

            var match = false;

            foreach (GameSettings settings in _context.GameSettingses)
            {
                if (settings.GameId == gameId)
                {
                    match = true;
                }
            }

            if (!match)
            {
                return(RedirectToPage("./MainMenu"));
            }

            GameId      = gameId.Value;
            BoardHeight = GameSettings.First(a => a.GameId == gameId).BoardHeight;
            BoardWidth  = GameSettings.First(a => a.GameId == gameId).BoardWidth;
            var game = new GameEngine.Game(GameSettings.First(a => a.GameId == gameId).BoardHeight,
                                           GameSettings.First(a => a.GameId == gameId).BoardWidth);
            var indexOfList = 0;
            var gameCells   = GameSettings.First(a => a.GameId == gameId)
                              .GameState.ToString();
            var gameCallsList = gameCells.Split(",");

            for (var i = 0; i < GameSettings.First(a => a.GameId == gameId).BoardHeight; i++)
            {
                for (var j = 0; j < GameSettings.First(a => a.GameId == gameId).BoardWidth; j++)
                {
                    game.Board[i, j] = (CellState)int.Parse(gameCallsList[indexOfList]);
                    indexOfList++;
                }
            }

            Board = game.Board;
            if (col != null && row != null)
            {
                game.Move(row.Value, col.Value);
                if (game.GetValue(row.Value, col.Value) == CellState.B)
                {
                    return(RedirectToPage("./PlayGame", new { over = true, gameId = GameId }));
                }

                var emptyCellCount = 0;
                foreach (CellState cellState in Board)
                {
                    if (NotOpenValues.Contains(cellState))
                    {
                        emptyCellCount++;
                    }
                }


                string gameState = "";
                foreach (CellState variable in Board)
                {
                    gameState += Convert.ToInt32(variable) + ",";
                }

                gameState = gameState.Remove(gameState.Length - 1);

                _context.GameSettingses.First(a => a.GameId == gameId).GameState = gameState;
                await _context.SaveChangesAsync();

                if (emptyCellCount == GameSettings.First(a => a.GameId == gameId).BombAmount)
                {
                    return(RedirectToPage("./PlayGame", new { win = true, gameId = GameId }));
                }
            }

            return(Page());
        }