From 52b1eb800fbcb23fb2423488eac759b30c40b18b Mon Sep 17 00:00:00 2001 From: steinti100434 <steinti100434@th-nuernberg.de> Date: Fri, 13 Dec 2024 11:47:24 +0100 Subject: [PATCH] Added Multiplayer , deleted class check --- Check.cs | 17 --- MastermindController.cs | 247 +++++++++++++++++++++++++++++----------- MastermindGame.cs | 35 +++++- 3 files changed, 213 insertions(+), 86 deletions(-) delete mode 100644 Check.cs diff --git a/Check.cs b/Check.cs deleted file mode 100644 index 51a4bf0..0000000 --- a/Check.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Mastermind -{ - internal class Check - { - public void CheckGame(int[] input) - { - - - } - } -} diff --git a/MastermindController.cs b/MastermindController.cs index 211d3f6..b818845 100644 --- a/MastermindController.cs +++ b/MastermindController.cs @@ -92,89 +92,208 @@ namespace Controller switch (gammode) { case "s": + Console.Clear(); + Console.WriteLine("Sibgleplayer mode selected."); + StartSingleplayerGame(); + + break; - // Starts Game + case "m": Console.Clear(); - Console.WriteLine($"Game starts with the difficulty: {difficulty}"); + // Implement Multiplayer functionality here + Console.WriteLine("Multiplayer mode selected."); + StartMultiplayerGame(); + // Placeholder, can be expanded based on the game logic for multiplayer + break; - // Load alt game or start a new one: true -> New game, false -> Load game - bool newGame = UserDecision(); - game = new MastermindGame(difficulty, newGame); - int generatedCodeLength = game.GetCodeLength(); + } + } - bool breaker = false; - while (game.StillHasTrials() && !breaker) - { - // User input Code - int[] userInput = ReadInput(generatedCodeLength); + private void StartSingleplayerGame() + { + // Starts Game + Console.Clear(); + Console.WriteLine($"Game starts with the difficulty: {difficulty}"); - // Debugging (Gets removed later) - foreach (int i in userInput) - { - Console.WriteLine(i); - } + // Load alt game or start a new one: true -> New game, false -> Load game + bool newGame = UserDecision(); + game = new MastermindGame(difficulty, newGame); + int generatedCodeLength = game.GetCodeLength(); + bool checkMenu = false; - game.AddAttempt(userInput); - game.DisplayBoard(); + bool breaker = false; + while (game.StillHasTrials() && !breaker) + { + // User input Code + int[] userInput = ReadInput(generatedCodeLength); - if (game.CheckWin(userInput)) - { - Console.ForegroundColor = ConsoleColor.DarkMagenta; - Console.WriteLine("Congratulations! You have guessed the code."); - Console.ForegroundColor = ConsoleColor.Gray; - game.SaveGame(true); - game.DeleteAutoSaveFile(); - SaveLeaderboard(); - breaker = true; - } + // Debugging (Gets removed later) + foreach (int i in userInput) + { + Console.WriteLine(i); + } + + game.AddAttempt(userInput); + game.DisplayBoard(); + + if (game.CheckWin(userInput)) + { + Console.ForegroundColor = ConsoleColor.DarkMagenta; + Console.WriteLine("Congratulations! You have guessed the code."); + Console.ForegroundColor = ConsoleColor.Gray; + game.SaveGame(true); + game.DeleteAutoSaveFile(); + SaveLeaderboard(); + breaker = true; + } + } + + if (!breaker) + { + Console.WriteLine("You have used all of your attempts. Next time you will win!"); + game.SaveGame(false); + game.DeleteAutoSaveFile(); + } + + Console.WriteLine("\n=========================="); + Console.WriteLine("Do you want to play again?"); + Console.WriteLine("[Y]es or [N]o"); + Console.WriteLine("==========================\n"); + + checkMenu = false; + while (!checkMenu) + { + switch (Console.ReadLine().ToLower()) + { + case "y": + Console.WriteLine("A new game is starting..."); + checkMenu = true; + Console.Clear(); + Play(); + return; + case "n": + Console.WriteLine("Returning to main menu"); + checkMenu = true; + Console.Clear(); + Program.MainMenu(true); + return; + default: + Console.WriteLine("Something went wrong! Try again! Only the letters \"Y\" and \"N\" are allowed."); + break; + } + } + + } + private void StartMultiplayerGame() + { + Console.Clear(); + Console.WriteLine("Multiplayer mode selected."); + Console.WriteLine("Player 1, please enter the code for Player 2 to guess:"); + int[] code1 = ReadInput(difficulty == "easy" ? 4 : difficulty == "medium" ? 6 : 8); + + Console.Clear(); + Console.WriteLine("Player 2, please enter the code for Player 1 to guess:"); + int[] code2 = ReadInput(difficulty == "easy" ? 4 : difficulty == "medium" ? 6 : 8); + + game = new MastermindGame(difficulty); + game.SetCode(code1); + + bool breaker = false; + while (game.StillHasTrials() && !breaker) + { + Console.Clear(); + Console.WriteLine("Player 2, try to guess the code!"); + int[] userInput = ReadInput(game.GetCodeLength()); + + game.AddAttempt(userInput); + game.DisplayBoard(); + + if (game.CheckWin(userInput)) + { + Console.ForegroundColor = ConsoleColor.DarkMagenta; + Console.WriteLine("Congratulations! Player 2 has guessed the code."); + Console.ForegroundColor = ConsoleColor.Gray; + game.SaveGame(true); + breaker = true; + } + + if (!breaker && !game.StillHasTrials()) + { + Console.WriteLine("Player 2 has used all of their attempts. Next time you will win!"); + game.SaveGame(false); + breaker = true; + } + + if (!breaker) + { + Console.WriteLine("Press [Enter] to switch to Player 1."); + Console.ReadLine(); + + game.SetCode(code2); + game.ResetAttempts(); + + Console.Clear(); + Console.WriteLine("Player 1, try to guess the code!"); + userInput = ReadInput(game.GetCodeLength()); + + game.AddAttempt(userInput); + game.DisplayBoard(); + + if (game.CheckWin(userInput)) + { + Console.ForegroundColor = ConsoleColor.DarkMagenta; + Console.WriteLine("Congratulations! Player 1 has guessed the code."); + Console.ForegroundColor = ConsoleColor.Gray; + game.SaveGame(true); + breaker = true; } - if (!breaker) + if (!breaker && !game.StillHasTrials()) { - Console.WriteLine("You have used all of your attempts. Next time you will win!"); + Console.WriteLine("Player 1 has used all of their attempts. Next time you will win!"); game.SaveGame(false); - game.DeleteAutoSaveFile(); + breaker = true; } - Console.WriteLine("\n=========================="); - Console.WriteLine("Do you want to play again?"); - Console.WriteLine("[Y]es or [N]o"); - Console.WriteLine("==========================\n"); + Console.WriteLine("Press [Enter] to switch to Player 2."); + Console.ReadLine(); - checkMenu = false; - while (!checkMenu) - { - switch (Console.ReadLine().ToLower()) - { - case "y": - Console.WriteLine("A new game is starting..."); - checkMenu = true; - Console.Clear(); - Play(); - return; - case "n": - Console.WriteLine("Returning to main menu"); - checkMenu = true; - Console.Clear(); - Program.MainMenu(true); - return; - default: - Console.WriteLine("Something went wrong! Try again! Only the letters \"Y\" and \"N\" are allowed."); - break; - } - } - break; + game.SetCode(code1); + game.ResetAttempts(); + } + } - case "m": - Console.Clear(); - // Implement Multiplayer functionality here - Console.WriteLine("Multiplayer mode selected."); - // Placeholder, can be expanded based on the game logic for multiplayer - break; + Console.WriteLine("\n=========================="); + Console.WriteLine("Do you want to play again?"); + Console.WriteLine("[Y]es or [N]o"); + Console.WriteLine("==========================\n"); + bool checkMenu = false; + while (!checkMenu) + { + switch (Console.ReadLine().ToLower()) + { + case "y": + Console.WriteLine("A new game is starting..."); + checkMenu = true; + Console.Clear(); + Play(); + return; + case "n": + Console.WriteLine("Returning to main menu"); + checkMenu = true; + Console.Clear(); + Program.MainMenu(true); + return; + default: + Console.WriteLine("Something went wrong! Try again! Only the letters \"Y\" and \"N\" are allowed."); + break; + } } } + + public int[] ReadInput(int generatedCodeLength) { int[] input = new int[generatedCodeLength]; diff --git a/MastermindGame.cs b/MastermindGame.cs index 89a8abf..b759f01 100644 --- a/MastermindGame.cs +++ b/MastermindGame.cs @@ -35,6 +35,14 @@ namespace Game { DifficultySetup(difficulty, out code_length, out duplicates, out yellowHints, out maxAttempt, out usedTipp); currentAttempt = 0; + attempts = new int[maxAttempt][]; + feedback = new (int, Hints)[code_length]; + + // Initializes each inner array to avoid null reference later + for (int i = 0; i < maxAttempt; i++) + { + attempts[i] = new int[code_length]; // Preinitializes each inner array + } } /// <summary> @@ -97,6 +105,8 @@ namespace Game } } + + private void DifficultySetup(string difficulty, out int code_length, out bool duplicates, out bool yellowHints, out int maxAttempt, out bool usedTipp) { switch (difficulty.ToLower()) @@ -244,7 +254,7 @@ namespace Game } } // Number is not present in the code - if(!check) + if (!check) { feedback[counter] = (i, Hints.None); counter++; @@ -263,12 +273,13 @@ namespace Game // Tipp-Mechanik if (!usedTipp) { - if(correct >= 3) + if (correct >= 3) { showTipp = true; usedTipp = true; } - } else + } + else { Console.WriteLine("You used your Tipp"); showTipp = false; @@ -364,10 +375,10 @@ namespace Game } // Checks if Tipp should be shown, only when 3 numbers where guessed correctly in one attempt (One-Time) - if(showTipp) + if (showTipp) { int position = GiveTipp(); - Console.WriteLine($"Tipp: At position {position +1} is the number {code[position]}"); + Console.WriteLine($"Tipp: At position {position + 1} is the number {code[position]}"); } // Shows how many attempts are left @@ -570,5 +581,19 @@ namespace Game { return maxAttempt - currentAttempt; } + public void SetCode(int[] newCode) + { + code = newCode; + } + + public void ResetAttempts() + { + currentAttempt = 0; + attempts = new int[maxAttempt][]; + for (int i = 0; i < maxAttempt; i++) + { + attempts[i] = new int[code_length]; + } + } } } -- GitLab