Exemplo n.º 1
0
        public static void SaveGame(Game game, SavedGame existingSavedGame = null !)
        {
            var savedGame = new SavedGame()
            {
                DateTime    = DateTime.Now,
                Board       = game.GetBoard(),
                BoardHeight = game.BoardHeight,
                BoardWidth  = game.BoardWidth,
                GameStatus  = game.GameStatus
            };

            using var ctx = new AppDbContext();

            if (existingSavedGame != null)
            {
                existingSavedGame.Board       = savedGame.Board;
                existingSavedGame.BoardHeight = savedGame.BoardHeight;
                existingSavedGame.BoardWidth  = savedGame.BoardWidth;
                existingSavedGame.DateTime    = savedGame.DateTime;
                ctx.SavedGames.Update(existingSavedGame);
            }
            else
            {
                ctx.SavedGames.Add(savedGame);
            }

            ctx.SaveChanges();
        }
Exemplo n.º 2
0
        public static void SaveSettings(Game game)
        {
            using (var ctx = new AppDbContext())
            {
                string saveName;
                string gameState = "";
                foreach (CellState variable in game.GetBoard())
                {
                    gameState += Convert.ToInt32(variable) + ",";
                }

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

                Console.WriteLine("Enter save name");
                do
                {
                    saveName = Console.ReadLine();
                } while (saveName == "");


                GameSettings data          = new GameSettings();
                var          dataFromDBase = ctx.GameSettingses.ToList();
                foreach (GameSettings gameSettings in dataFromDBase)
                {
                    if (saveName.Equals(gameSettings.GameName))
                    {
                        gameSettings.GameName    = saveName;
                        gameSettings.BoardHeight = game.BoardHeight;
                        gameSettings.BoardWidth  = game.BoardWidth;
                        gameSettings.GameState   = gameState;
                        ctx.SaveChanges();
                        return;
                    }
                }

                data.GameName    = saveName;
                data.BoardHeight = game.BoardHeight;
                data.BoardWidth  = game.BoardWidth;
                data.GameState   = gameState;
                data.BombAmount  = game.Bombs;
                ctx.GameSettingses.Add(data);
                ctx.SaveChanges();
            }
        }
Exemplo n.º 3
0
        public async Task <ActionResult> OnPost()
        {
            if (FieldSize == "Back to Main Menu")
            {
                return(RedirectToPage("./MainMenu"));
            }

            if (ModelState.IsValid)
            {
                if (FieldSize == "Small Field")
                {
                    GameOptions.Height = 9;
                    GameOptions.Width  = 9;
                    GameOptions.Bombs  = 10;
                }

                if (FieldSize == "Medium Field")
                {
                    GameOptions.Height = 16;
                    GameOptions.Width  = 16;
                    GameOptions.Bombs  = 40;
                }

                if (FieldSize == "Large Field")
                {
                    GameOptions.Height = 16;
                    GameOptions.Width  = 30;
                    GameOptions.Bombs  = 99;
                }

                if (GameOptions.Bombs > GameOptions.Height * GameOptions.Width - 1)
                {
                    BombAmount = false;
                }

                foreach (GameSettings settings in _context.GameSettingses)
                {
                    if (settings.GameName == GameOptions.GameName)
                    {
                        isCorrect = false;
                    }
                }

                if (!isCorrect || !BombAmount)
                {
                    return(RedirectToPage("./StartGame", new { correct = isCorrect, bombAmount = BombAmount }));
                }

                var game = new GameEngine.Game(GameOptions.Height, GameOptions.Width);
                game.PlaceBomb(GameOptions.Height, GameOptions.Width, GameOptions.Bombs);
                string gameCells = "";
                foreach (CellState variable in game.GetBoard())
                {
                    gameCells += Convert.ToInt32(variable) + ",";
                }

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

                var gameState = new GameSettings()
                {
                    BoardWidth  = GameOptions.Width,
                    BoardHeight = GameOptions.Height,
                    GameName    = GameOptions.GameName,
                    BombAmount  = GameOptions.Bombs,
                    GameState   = gameCells
                };
                _context.GameSettingses.Add(gameState);
                await _context.SaveChangesAsync();

                return(RedirectToPage("./PlayGame", new { gameId = gameState.GameId }));
            }

            return(Page());
        }
Exemplo n.º 4
0
        public bool areFourConnected(Game game)
        {
            Console.WriteLine("Start Checking if Board has a winner!");
            // horizontalCheck
            for (int j = 0; j < game.BoardHeight - 3; j++)
            {
                for (int i = 0; i < game.BoardWidth; i++)
                {
                    if (game.GetBoard()[i, j] == CellState.B && game.GetBoard()[i, j + 1] == CellState.B &&
                        game.GetBoard()[i, j + 2] == CellState.B && game.GetBoard()[i, j + 3] == CellState.B)
                    {
                        return(true);
                    }

                    if (game.GetBoard()[i, j] == CellState.R && game.GetBoard()[i, j + 1] == CellState.R &&
                        game.GetBoard()[i, j + 2] == CellState.R && game.GetBoard()[i, j + 3] == CellState.R)
                    {
                        return(true);
                    }
                }
            }

            // verticalCheck
            for (int i = 0; i < game.BoardWidth - 3; i++)
            {
                for (int j = 0; j < game.BoardHeight; j++)
                {
                    if (game.GetBoard()[i, j] == CellState.B && game.GetBoard()[i + 1, j] == CellState.B &&
                        game.GetBoard()[i + 2, j] == CellState.B && game.GetBoard()[i + 3, j] == CellState.B)
                    {
                        return(true);
                    }

                    if (game.GetBoard()[i, j] == CellState.R && game.GetBoard()[i + 1, j] == CellState.R &&
                        game.GetBoard()[i + 2, j] == CellState.R && game.GetBoard()[i + 3, j] == CellState.R)
                    {
                        return(true);
                    }
                }
            }

            // ascendingDiagonalCheck
            for (int i = 3; i < game.BoardWidth; i++)
            {
                for (int j = 0; j < game.BoardHeight - 3; j++)
                {
                    if (game.GetBoard()[i, j] == CellState.B && game.GetBoard()[i - 1, j + 1] == CellState.B &&
                        game.GetBoard()[i - 2, j + 2] == CellState.B && game.GetBoard()[i - 3, j + 3] == CellState.B)
                    {
                        return(true);
                    }
                    if (game.GetBoard()[i, j] == CellState.R && game.GetBoard()[i - 1, j + 1] == CellState.R &&
                        game.GetBoard()[i - 2, j + 2] == CellState.R && game.GetBoard()[i - 3, j + 3] == CellState.R)
                    {
                        return(true);
                    }
                }
            }

            // descendingDiagonalCheck
            for (int i = 3; i < game.BoardWidth; i++)
            {
                for (int j = 3; j < game.BoardHeight; j++)
                {
                    if (game.GetBoard()[i, j] == CellState.B && game.GetBoard()[i - 1, j - 1] == CellState.B &&
                        game.GetBoard()[i - 2, j - 2] == CellState.B && game.GetBoard()[i - 3, j - 3] == CellState.B)
                    {
                        return(true);
                    }
                    if (game.GetBoard()[i, j] == CellState.R && game.GetBoard()[i - 1, j - 1] == CellState.R &&
                        game.GetBoard()[i - 2, j - 2] == CellState.R && game.GetBoard()[i - 3, j - 3] == CellState.R)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }