public void Play() { // Reset Variables Player.Guesses = 7; Player.Won = false; // Add difficulty multiplier later CorrectNumber = rand.Next(MinGuess, MaxGuess); Console.WriteLine(CorrectNumber); // As long as the player has guesses, guess. while (Player.Guesses > 0 && !Player.Won) { // Determine if the guess is actually valid. bool invalidGuess = true; while (invalidGuess) { Fancy.Write($"\t({Player.Guesses} left) Guess a number: "); string guess = Fancy.ReadLine(ConsoleColor.Green).ToLower(); EvaluateGuess(guess, out invalidGuess); } } if (Player.Won) { Fancy.Write("\nWell done! ", afterPause: 500); Player.Won = true; Player.Win++; Player.Plays++; int scoreGained = (Player.Guesses + 1) * (int)Diff; Fancy.Write("You got " + scoreGained + " points! Here are your stats:\n", color: ConsoleColor.Green); Player.Score += scoreGained; } else { Fancy.Write("\nYou lost. It happens to the best of us....\n", afterPause: 750); Player.Won = false; Player.Loss++; Player.Plays++; } Player.ShowAll(); GameIO.SaveGame(Player, Diff); // Save the game. Fancy.Write("\nDo you want to play again? (y or n) "); string answer = Fancy.ReadLine(ConsoleColor.Cyan); if (answer != "y") { Player.IsPlaying = false; } }
static void Main(string[] args) { // Get the player's name Fancy.Write("Welcome to Guess a Game.", " What is your name: ", 15, 30, pause: 500); string playerName = Console.ReadLine(); string[] playerDataArray; // Will store the information from saved game file. Player player; // If there is already a text file, find the player's information int difficulty; // The number representation of the difficulty Difficulty difficultyName; // The actual difficulty // Determine if this player is new or has played before. If the player is a returning player, get the player's data from the game file. bool newPlayer; if (File.Exists(GameIO.GameFile)) { playerDataArray = GameIO.GetPlayerData(playerName, out newPlayer); // Get the data relating to that player from the game file. } else { playerDataArray = null; newPlayer = true; } // If the player is not new load their information from the game file. if (!newPlayer) { Fancy.Write("Welcome back, " + playerDataArray[0], 50, afterPause: 500, color: ConsoleColor.Magenta); try { Fancy.Write($"\nYour previous Difficulty: {playerDataArray[6]} | Choose Game Difficulty (1-4): ", 20); // If the player has a save game, instantiate the information. player = new Player( name: playerDataArray[0], id: playerDataArray[1], totalScore: Convert.ToInt64(playerDataArray[2]), win: Convert.ToInt32(playerDataArray[3]), loss: Convert.ToInt32(playerDataArray[4]), plays: Convert.ToInt32(playerDataArray[5])); } catch (IndexOutOfRangeException) { Fancy.Write( ". You closed the program prematurely: no saved data.\n" + "To save your game history, be sure to exit the program as intended.\n", afterPause: 500, color: ConsoleColor.Yellow); Fancy.Write("Choose Game Difficulty: ", 20); player = new Player(playerName); } catch (Exception ex) { Fancy.Write($"\n\n\tI don't know what went wrong!!\n\t{ex.GetType()}\n\t{ex.Message}\n\n"); player = new Player(playerName); } difficulty = AskForDifficulty(); difficultyName = SetDifficulty(difficulty); } else // No text file? make one with the player's information. { player = new Player(playerName); Fancy.Write("What difficulty do you want to play? (1, 2, 3, 4): ", 20); difficulty = AskForDifficulty(); difficultyName = SetDifficulty(difficulty); GameIO.AddPlayerToFile(player, difficultyName); Tutorial(); } BackUpAssignment(difficulty); GuessANumber game = new GuessANumber(player, difficultyName); // Game Loop, stops when the player doesn't want to play anymore. while (player.IsPlaying) { game.Play(); } Fancy.Write("\n\nThanks for playing!", 75, color: ConsoleColor.Green); Console.Read(); }