From d24135dac6e0d7e8ea20e85e82418ca6c6478d61 Mon Sep 17 00:00:00 2001 From: Fabian <muellerfa100445@th-nuernberg.de> Date: Thu, 5 Dec 2024 12:06:59 +0100 Subject: [PATCH] Tutorial Fix + Endversion --- Program.cs | 36 ++++++++- Tutorial.cs | 218 +++++++++++++++++++++++++++++++--------------------- 2 files changed, 162 insertions(+), 92 deletions(-) diff --git a/Program.cs b/Program.cs index 52cd02a..cf6a275 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Data; using Controller; namespace Mastermind @@ -43,15 +44,39 @@ namespace Mastermind conLastGame.ShowLastGames(amount); break; case "q": - return; + Console.WriteLine("Are you sure that you want to quit? Confirm with [Q] to go back to the Main menu press [M]."); + while (true) + { + input = Console.ReadLine(); //string? allows for null-value + if (input != null) //"filter out" null values to avoid crash + { + if (input.ToLower() == "q") + { + return; + } + else if (input.ToLower() == "m") + { + Console.Clear(); + Main(); + } + else + { + ValidOption(); + } + } + else + { + ValidOption(); + } + } default: - Console.WriteLine("Please pick a valid Option."); + ValidOption(); break; } } else { - Console.WriteLine("Please pick a valid Option."); + ValidOption(); } } } @@ -71,6 +96,11 @@ namespace Mastermind Console.WriteLine("[L]ast saved Games"); Console.WriteLine("[Q]uit"); } + + public static void ValidOption() + { + Console.WriteLine("Please pick a valid Option."); + } } } \ No newline at end of file diff --git a/Tutorial.cs b/Tutorial.cs index 6b76fd2..45495e8 100644 --- a/Tutorial.cs +++ b/Tutorial.cs @@ -1,5 +1,7 @@ using Controller; using System; +using Mastermind; +using Game; namespace Mastermind { @@ -7,121 +9,159 @@ namespace Mastermind { public static void Tutorial() { + TutorialMenu(); + while (true) { + string? input = Console.ReadLine(); //string? allows for null-value + if (input != null) //"filter out" null values to avoid crash { - TutorialMenu(); - while (true) + switch (input.ToLower()) { - string? input = Console.ReadLine(); //string? allows for null-value - if (input != null) //"filter out" null values to avoid crash - { - switch (input.ToLower()) - { - case "e": - Console.Clear(); - Console.WriteLine("In this game a secret code consisting of numbers (0-9) is generated\nwhich the player attempts to decipher the code through repeated guesses."); - Console.WriteLine("After each guess, feedback is provided: the number of digits in the correct position (green)\nand the number of digits that are part of the code but in the wrong position (yellow)."); - Console.WriteLine("The goal is to crack the code in as few attempts as possible.\nThe game ends when the code is correctly guessed or the maximum number of attempts is reached."); - Console.WriteLine("This Game offers three diffculties which influence the following:"); - Console.WriteLine("\neasy\nCode Length: 4\nNo duplicate numbers\nAttempts: 6"); - Console.WriteLine("\nmedium\nCode Length: 6\nNo duplicate numbers\nAttempts: 4"); - Console.WriteLine("\nhard\nCode Length: 8\nDuplicate Numbers possible\nAttempts: 3"); - Console.Write("\nPlease press any key in order to go back"); - Console.ReadKey(); - Console.Clear(); - TutorialMenu(); - break; - case "t": - Console.WriteLine("In this Tutorial Game you will have unlimited tries. Also the Code won't change."); - TutorialGame(); - Console.Clear(); - Program.MainMenu(); - return; - case "b": - Console.Clear(); - Program.MainMenu(); - return; - default: - Console.WriteLine("Please pick a valid Option."); - break; - } - } - else - { - Console.WriteLine("Please pick a valid Option."); - } + case "e": + Console.Clear(); + Console.WriteLine("In this game a secret code consisting of numbers (0-9) is generated\nwhich the player attempts to decipher the code through repeated guesses."); + Console.WriteLine("After each guess, feedback is provided: the number of digits in the correct position (green)\nand the number of digits that are part of the code but in the wrong position (yellow)."); + Console.WriteLine("The goal is to crack the code in as few attempts as possible.\nThe game ends when the code is correctly guessed or the maximum number of attempts is reached."); + Console.WriteLine("This Game offers three diffculties which influence the following:"); + Console.WriteLine("\neasy\nCode Length: 4\nNo duplicate numbers\nAttempts: 6"); + Console.WriteLine("\nmedium\nCode Length: 6\nNo duplicate numbers\nAttempts: 4"); + Console.WriteLine("\nhard\nCode Length: 8\nDuplicate Numbers possible\nAttempts: 3"); + Console.Write("\nPlease press any key in order to go back"); + Console.ReadKey(); + Console.Clear(); + TutorialMenu(); + break; + case "t": + Console.WriteLine("In this Tutorial Game you will have unlimited tries. Also the Code won't change."); + TutorialGame(); + Console.Clear(); + Program.MainMenu(); + return; + case "b": + Console.Clear(); + Program.MainMenu(); + return; + default: + Program.ValidOption(); + break; } } + else + { + Program.ValidOption(); + } } - + } static void TutorialGame() { int[] tutorialCode = [2, 3, 5, 8]; - int correctGuesses; //used to count how many guesses are correct number in correct position - int cNumberfPos; //used to count how many gueeses are correct number in wrong position - int indexCounter; //used to cycle through the array tutorialCode to check for correct position + int[] tutorialGuess; + int correctGuesses = 0; + int cNumberfPos = 0; + MastermindController controller = new(); - do + do { - MastermindController controller = new(); - int[] tutorialGuess = controller.ReadInput(tutorialCode.Length); //allow user to input their Guess and write them in the array tutorialGuess - correctGuesses = 0; - cNumberfPos = 0; - indexCounter = 0; - foreach (int n in tutorialGuess) //loop through each variable in the array tutorialGuess + correctGuesses = 0; + cNumberfPos = 0; + tutorialGuess = controller.ReadInput(tutorialCode.Length); //allow user to input their Guess and write them in the array tutorialGuess + HintCounter(tutorialGuess, tutorialCode, ref correctGuesses, ref cNumberfPos); + TutorialBoard(tutorialGuess, correctGuesses, cNumberfPos); + } + while (correctGuesses != 4); + + Console.WriteLine("Congratulations! You have guessed all 4 Numbers right. You should now be ready for a real game."); + Console.WriteLine("To return to the main menu please press any key."); + Console.ReadKey(); + } + static void TutorialMenu() + { + Console.Clear(); + Console.WriteLine("\nPlease Choose:"); + Console.WriteLine("[E]xplanation"); + Console.WriteLine("[T]utorial Game"); + Console.WriteLine("[B]ack"); + } + + static void HintCounter(int[] tutorialGuess, int[] tutorialCode, ref int correctGuesses, ref int cNumberfPos) + { + int counter = 0; // Counter for tracking positions in the code + int[] used = new int[10]; // Tracks used numbers to prevent duplicate hints + + foreach (int i in tutorialGuess) + { + bool check = false; + + // Checks if the number is in the right place + if (i == tutorialCode[counter]) { - bool checker = false; - if (n == tutorialCode[indexCounter]) //check if user guess has correct number in corresponding position + // duplicates are not allowed, therefore check if the number hasn't already been used + if (used[i] == 0) { - correctGuesses++; - indexCounter++; - checker = true; + used[i] = 1; // Marks as used } - else if (checker == false) + correctGuesses++; + counter++; + check = true; + } + // Checks if the number is there but in the wrong place. Deactivated for difficulty: hard + else if (!check) + { + foreach (int j in tutorialCode) { - foreach (int m in tutorialCode) //check if user guess exists in code, if yes --> right number, wrong position + if (i == j) { - if (n == m) + // duplicates are not allowed, therefore check if the number hasn't already been used + if (used[i] == 0) { cNumberfPos++; - indexCounter++; + used[i] = 1; // Marks as used } } } - else //wrong number in wrong position - { - indexCounter++; - } - } - for (int i = 0; i < correctGuesses; i++) - { - Console.ForegroundColor = ConsoleColor.Green; - Console.Write("x "); - Console.ForegroundColor = ConsoleColor.Gray; + counter++; } + } + } + static void TutorialBoard(int[] tutorialGuess, int correctGuesses, int cNumberfPos) + { + Console.WriteLine($"\n{new string('=', 30)}"); - for (int i = 0; i < cNumberfPos; i++) - { - Console.ForegroundColor = ConsoleColor.Yellow; - Console.Write("x "); - Console.ForegroundColor = ConsoleColor.Gray; + // Shows code of the user + Console.Write($"{"Your provided code: ",-24}"); + foreach (int n in tutorialGuess) + { + Console.Write(n + " "); + } + + Console.WriteLine($"\n {new string('-', 30)}"); + // Shows hints + Console.Write($"{"Recieved Hints:",-24}"); + + for (int i = 0; i < correctGuesses; i++) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.Write("X "); + Console.ForegroundColor = ConsoleColor.Gray; + } + for (int i = 0; i < cNumberfPos; i++) + { + Console.ForegroundColor = ConsoleColor.Yellow; + Console.Write("Y "); + Console.ForegroundColor = ConsoleColor.Gray; + } + if (correctGuesses + cNumberfPos != 4) + { + for (int i = 4; (i - correctGuesses - cNumberfPos) > 0; i--) + { + Console.Write("- "); } } - while (correctGuesses != 4); - Console.WriteLine("Congratulations! You have guessed all 4 Numbers right. You should now be ready for a real game."); - Console.WriteLine("To return to the main menu please press any key."); - Console.ReadKey(); - } - static void TutorialMenu() - { - Console.Clear(); - Console.WriteLine("\nPlease Choose:"); - Console.WriteLine("[E]xplanation"); - Console.WriteLine("[T]utorial Game"); - Console.WriteLine("[B]ack"); + Console.WriteLine("\n(The hints aren't in correct order)"); + + Console.WriteLine($"{new string('=', 30)} \n"); } - } } -} +} \ No newline at end of file -- GitLab