Esempio n. 1
0
        private static string SavedGamesLoadFromDb()
        {
            var gameSaves = GameStateHandler.ShowGameSavesInDb();

            if (gameSaves.Count == 0)
            {
                Console.WriteLine("No saved games in database!");
                return("");
            }

            var index = 1;

            foreach (var gameSave in gameSaves)
            {
                Console.WriteLine($"{index} {gameSave.Name}");
                index++;
            }

            bool userCanceled;
            int  userInput;

            (userInput, userCanceled) = GetUserIntInput("Which game would you like to load?",
                                                        1, gameSaves.Count);
            if (userCanceled)
            {
                return("");
            }

            GameState gameStateToLoad = gameSaves[userInput - 1];

            var gameToLoad = GameStateHandler.LoadGameFromDb(gameStateToLoad.GameStateId);

            if (gameToLoad != null)
            {
                GameRunner(gameToLoad);
            }
            else
            {
                Console.WriteLine("Game was not found!");
            }
            return("");
        }
Esempio n. 2
0
        /*
         * private static string SavedGamesLoad()
         * {
         *  string[]? games = GameStateHandler.AllSavedGames();
         *  if (games.Length == 0)
         *  {
         *      Console.WriteLine("No game files found!");
         *      return "";
         *  }
         *  for (var i = 0; i < games.Length; i++)
         *  {
         *      Console.WriteLine($"{i + 1} {games.GetValue(i)}");
         *  }
         *
         *  bool userCanceled;
         *  int userInput;
         *  (userInput, userCanceled) = GetUserIntInput("Which game would you like to load?",
         *      1, games.Length);
         *  if (userCanceled)
         *  {
         *      return "";
         *  }
         *
         *  var gameToLoad = GameStateHandler.LoadGame(games.GetValue(userInput - 1).ToString());
         *  if (gameToLoad != null)
         *  {
         *      GameRunner(gameToLoad);
         *  }
         *  else
         *  {
         *      Console.WriteLine("Game was not found!");
         *  }
         *  return "";
         * }
         *
         * private static string SaveSettings()
         * {
         *  Console.Clear();
         *  int boardWidth;
         *  int boardHeight;
         *  int minesAmount;
         *
         *  bool userCanceled;
         *
         *  (boardWidth, userCanceled) = GetUserIntInput("Enter board width (3-20)", 3, 30, 0);
         *  if (userCanceled)
         *  {
         *      return "";
         *  }
         *  (boardHeight, userCanceled) = GetUserIntInput("Enter board height (3-20)", 3, 16, 0);
         *  if (userCanceled)
         *  {
         *      return "";
         *  }
         *  (minesAmount, userCanceled) = GetUserIntInput($"Enter the amount of mines (1- {boardHeight*boardWidth - 1})",
         *      1, boardHeight*boardWidth - 1, 0);
         *  if (userCanceled)
         *  {
         *      return "";
         *  }
         *
         *  _settings.BoardHeight = boardHeight;
         *  _settings.BoardWidth = boardWidth;
         *  _settings.MinesAmount = minesAmount;
         *  GameConfigHandler.SaveConfig(_settings);
         *
         *  return "";
         * }
         */

        private static string GameRunner(Game game)
        {
            var  done      = false;
            bool firstMove = game.PlayerZeroMove;
            var  gameLost  = false;
            var  gameWon   = false;

            if (game.Name == "")

            {
                Console.WriteLine("Set name for game instance");
                Console.Write(">");
                var command = Console.ReadLine()?.Trim() ?? "";
                game.Name = command;
            }

            do
            {
                GameStateHandler.SaveGameToDb(game, game.Name);
                GameUI.GameUI.PrintBoard(game);
                var  userXint = 0;
                var  userYint = 0;
                bool flag;

                bool userCanceled;
                (flag, userCanceled) = GetUserBoolInput("Flag (F) / Open (A)");
                if (!userCanceled)
                {
                    (userXint, userCanceled) = GetUserIntInput("Enter X coordinate", 1, game.BoardWidth);
                    if (!userCanceled)
                    {
                        (userYint, userCanceled) = GetUserIntInput("Enter Y coordinate", 1, game.BoardHeight);
                    }
                }

                if (userCanceled)
                {
                    done = true;
                }
                else
                {
                    gameLost = game.Move(userYint - 1, userXint - 1, flag);
                }

                if (!firstMove && !userCanceled)
                {
                    gameWon = game.CheckIfGameWon();
                    done    = gameWon || gameLost;
                }

                if (firstMove)
                {
                    firstMove = false;
                }
                GameStateHandler.SaveGame(game, game.Name);
            } while (!done);

            GameUI.GameUI.PrintBoard(game);

            if (gameLost)
            {
                Console.WriteLine("GAME OVER!");
                GameStateHandler.DeleteSave(game.Name);
                GameStateHandler.DeleteFromDb(game);
            }
            else if (gameWon)
            {
                Console.WriteLine("You won!");
                GameStateHandler.DeleteSave(game.Name);
                GameStateHandler.DeleteFromDb(game);
            }
            return("");
        }