Exemplo n.º 1
0
        public static void PrintBoard(GameBoardEngine gameBoardEngine, GameBoard game)
        {
            Console.Clear();
            for (int yIndex = 0; yIndex < game.Height; yIndex++)
            {
                var line = "";

                for (int xIndex = 0; xIndex < game.Width; xIndex++)
                {
                    line = line + " " + GetSingleState(gameBoardEngine.GetPanel(xIndex + 1, yIndex + 1)) + " ";

                    if (xIndex < game.Width - 1)
                    {
                        line = line + _verticalSeparator;
                    }
                }

                Console.WriteLine(line);

                if (yIndex < game.Height - 1)
                {
                    line = "";
                    for (int xIndex = 0; xIndex < game.Width; xIndex++)
                    {
                        line = line + _horizontalSeparator + _horizontalSeparator + _horizontalSeparator;
                        if (xIndex < game.Height - 1)
                        {
                            line += _centerSeparator;
                        }
                    }
                    Console.WriteLine(line);
                }
            }
        }
Exemplo n.º 2
0
        private static string LoadGame()
        {
            var dbOption = new DbContextOptionsBuilder <AppDbContext>()
                           .UseSqlite("Data Source=/Users/maria/csharp2019fall/Proge/minesweeper/WebApp/app.db").Options;

            var ctx   = new AppDbContext(dbOption);
            var query = ctx.GameBoards.ToList();

            Console.Clear();
            Console.WriteLine("Save games:");

            foreach (var game in query)
            {
                Console.WriteLine($"{game.SaveGameName} ({game.Height}x{game.Width})");
            }

            GameBoard gameBoard;

            var userXint     = 0;
            var userYint     = 0;
            var userCanceled = false;
            var plantFlag    = false;
            var saveGame     = false;

            do
            {
                Console.WriteLine("Enter '0' to cancel input");
                Console.WriteLine("Please enter a save game name:");
                Console.WriteLine(">");

                var userInput = Console.ReadLine();

                if (ctx.GameBoards.Any(e => e.SaveGameName == userInput))
                {
                    gameBoard = ctx.GameBoards.FirstOrDefault(e => e.SaveGameName == userInput);
                    break;
                }
                if (userInput == "0")
                {
                    userCanceled = true;
                    ClearConsoleIfUserCanceled(userCanceled);
                    return("");
                }
                Console.WriteLine("No such save file found.");
            } while (true);

            GameBoardEngine gameBoardEngine = new GameBoardEngine(ctx);

            gameBoardEngine.DeserializeGameBoardPanels(gameBoard);

            MainGame(gameBoardEngine, gameBoard, ctx, userYint, userXint, userCanceled, saveGame, plantFlag, "updateGame");

            return("");
        }
Exemplo n.º 3
0
        static void MainGame(GameBoardEngine gameEngine, GameBoard game,
                             AppDbContext ctx, int userYint, int userXint, bool userCanceled, bool saveGame, bool plantFlag, string type)
        {
            do
            {
                Console.Clear();
                ConsoleUI.PrintBoard(gameEngine, game);

                (userYint, userCanceled, plantFlag, saveGame) = GetUserIntInput("Enter Y coordinate", 1, game.Height, 0, "F", "S");
                if (userCanceled)
                {
                    ClearConsoleIfUserCanceled(userCanceled);
                    break;
                }
                if (plantFlag)
                {
                    PlantFlag(gameEngine, game, plantFlag);
                }
                else if (saveGame)
                {
                    SaveGameIfUserSaved(gameEngine, game, saveGame, ctx, type);
                }
                else
                {
                    (userXint, userCanceled, _, _) = GetUserIntInput("Enter X coordinate", 1, game.Width, 0, null);
                    ClearConsoleIfUserCanceled(userCanceled);
                    if (!gameEngine.GameBoardPanels.Any(e => e.IsRevealed))
                    {
                        gameEngine.FirstMove(userXint, userYint, new Random(), game);
                    }
                    gameEngine.RevealPanel(userXint, userYint, game);
                }
            } while (game.Status == GameStatus.InProgress);

            switch (game.Status)
            {
            case GameStatus.Completed:
                Console.WriteLine("GAME WON");
                break;

            case GameStatus.Failed:
                ConsoleUI.PrintBoard(gameEngine, game);
                Console.WriteLine("GAME LOST!");
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemplo n.º 4
0
        private static string NewGame()
        {
            var dbOption = new DbContextOptionsBuilder <AppDbContext>()
                           .UseSqlite("Data Source=/Users/maria/csharp2019fall/Proge/minesweeper/WebApp/app.db").Options;

            var ctx        = new AppDbContext(dbOption);
            var gameEngine = new GameBoardEngine(ctx);

            var userHeight       = 0;
            var userWidth        = 0;
            var userCanceledGame = false;

            (userHeight, userCanceledGame, _, _) = GetUserIntInput("Enter board height", 8, 64, 0);
            ClearConsoleIfUserCanceled(userCanceledGame);

            (userWidth, userCanceledGame, _, _) = GetUserIntInput("Enter board width", 8, 64, 0);
            ClearConsoleIfUserCanceled(userCanceledGame);


            var game = new GameBoard
            {
                Height    = userHeight,
                MineCount = 10,
                Width     = userWidth
            };

            gameEngine.CreateNewGameBoard(userHeight, userWidth, 10);

            ConsoleUI.PrintBoard(gameEngine, game);

            var userXint     = 0;
            var userYint     = 0;
            var userCanceled = false;
            var plantFlag    = false;
            var saveGame     = false;

            (userYint, userCanceled, _, _) = GetUserIntInput("Enter Y coordinate", 1, game.Height, 0);
            ClearConsoleIfUserCanceled(userCanceled);

            (userXint, userCanceled, _, _) = GetUserIntInput("Enter X coordinate", 1, game.Width, 0);
            ClearConsoleIfUserCanceled(userCanceled);

            gameEngine.FirstMove(userXint, userYint, new Random(), game);
            gameEngine.RevealPanel(userXint, userYint, game);

            MainGame(gameEngine, game, ctx, userYint, userXint, userCanceled, saveGame, plantFlag, "newGame");
            return("");
        }
Exemplo n.º 5
0
        static void PlantFlag(GameBoardEngine gameBoardEngine, GameBoard gameBoard, bool plantFlag)
        {
            if (!plantFlag)
            {
                return;
            }

            var userYint = 0;
            var userXint = 0;

            (userYint, _, _, _) = GetUserIntInput("Enter Flag Y coordinate", 1, gameBoard.Height, 0);

            (userXint, _, _, _) = GetUserIntInput("Enter Flag X coordinate", 1, gameBoard.Width, 0);

            gameBoardEngine.FlagPanel(userXint, userYint);
        }
Exemplo n.º 6
0
 private static void AddGameToDb
     (AppDbContext context, GameBoardEngine gameBoardEngine, GameBoard gameBoard, string saveGameName)
 {
     while (true)
     {
         if (context.GameBoards.Any(e => e.SaveGameName == saveGameName))
         {
             Console.WriteLine($"{saveGameName} name already exists. Please pick a new name.");
             Console.WriteLine(">");
             saveGameName = Console.ReadLine();
         }
         else
         {
             gameBoard.SaveGameName = saveGameName;
             break;
         }
     }
     gameBoardEngine.AddGameBoardToDb(gameBoard);
 }
Exemplo n.º 7
0
        public IActionResult OnPost()
        {
            GameBoard !.MineCount = 10;

            var gameBoardEngine = new GameBoardEngine(_appDbContext);
            var gameBoard       = gameBoardEngine.CreateNewGameBoard(GameBoard.Height, GameBoard.Width, GameBoard.MineCount);

            gameBoard.SaveGameName = GameBoard.SaveGameName;

            if (!ModelState.IsValid)
            {
                return(Page());
            }

            if (_appDbContext.GameBoards.Any(e => e.SaveGameName == GameBoard !.SaveGameName))
            {
                ModelState.AddModelError("GameBoard.SaveGameName", "A save game with this name already exists.");
                return(Page());
            }
            gameBoardEngine.AddGameBoardToDb(gameBoard);
            return(RedirectToPage("/PlayGame/Index", new { id = gameBoard.Id }));
        }
Exemplo n.º 8
0
        static void SaveGameIfUserSaved(GameBoardEngine gameBoardEngine, GameBoard gameBoard, bool saveGame, AppDbContext context, string type)
        {
            if (!saveGame)
            {
                return;
            }

            if (type == "updateGame")
            {
                gameBoardEngine.UpdateGameBoard(gameBoard);
                SavingAnimation();
                return;
            }

            Console.Clear();
            Console.WriteLine("Please write a name for the save:");
            Console.WriteLine(">");

            var saveGameName = Console.ReadLine();

            AddGameToDb(context, gameBoardEngine, gameBoard, saveGameName);
            SavingAnimation();
        }
Exemplo n.º 9
0
 public Index(AppDbContext appDbContext)
 {
     _appDbContext   = appDbContext;
     GameBoardEngine = new GameBoardEngine(_appDbContext);
 }