예제 #1
0
        private static string LoadGame()
        {
            Game?     game      = null;
            GameState?gameState = null;

            while (game == null)
            {
                Console.Clear();
                var saves = GameSaves.GetPlayableSaves();
                Console.WriteLine($"Available saves: {string.Join(", ", saves)}");
                var filename =
                    InputHandler.GetUserStringInput("Choose the game to load " +
                                                    $"(D - default name ({GameSaves.DefaultName}), X - exit):",
                                                    1, 30, "Enter a valid name!", true);
                if (filename == null)
                {
                    return("");
                }
                if (filename.ToLower() == "d")
                {
                    filename = null;
                }
                gameState = GameSaves.LoadSave(filename);
                game      = gameState != null?JsonConvert.DeserializeObject <Game>(gameState.Data) : null;
            }
            _saveGame = game;
            return(gameState !.Opponent == 0 ? PlayGameTwoPlayers() : PlayGameComputer());
        }
예제 #2
0
        private static void SaveGame(Game game, int mode)
        {
            var playableSaves = GameSaves.GetPlayableSaves();

            Console.WriteLine($"Existing playable saves: {string.Join(", ", playableSaves)}");
            var finishedSaves = GameSaves.GetFinishedSaves();

            Console.WriteLine($"Existing finished saves: {string.Join(", ", finishedSaves)}");
            var filename = InputHandler.GetUserStringInput(
                $"Choose the name for a save (D - default name ({GameSaves.DefaultName}), X - drop the game):", 1, 30,
                "Enter a valid name!", true);

            if (filename == null)
            {
                return;
            }
            if (filename.ToLower() == "d")
            {
                filename = GameSaves.DefaultName;
            }
            string?rewriteFilename = filename;

            while (playableSaves.Contains(rewriteFilename) || finishedSaves.Contains(rewriteFilename))
            {
                rewriteFilename = InputHandler.GetUserStringInput(
                    "The save with this name already exists. Choose another name, or X to rewrite the save:", 1, 30,
                    "Enter a valid name!", true);
                if (rewriteFilename == null)
                {
                    break;
                }
            }
            GameSaves.Save(game, rewriteFilename ?? filename, mode);
        }