From d02333d6be8b8e9b1ed80a9558f4fa86eb91d1ba Mon Sep 17 00:00:00 2001 From: Seilenthal <mark.seilenthal@siemens.com> Date: Thu, 28 Nov 2024 14:03:29 +0100 Subject: [PATCH] Bug Fix + Added Data Saving --- MastermindController.cs | 5 ++++- MastermindGame.cs | 45 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/MastermindController.cs b/MastermindController.cs index 4156fed..bd69f2e 100644 --- a/MastermindController.cs +++ b/MastermindController.cs @@ -1,4 +1,5 @@ -using Mastermind; +using Game; +using Mastermind; using System; namespace Controller @@ -80,6 +81,7 @@ namespace Controller Console.ForegroundColor = ConsoleColor.DarkMagenta; Console.WriteLine("Congratulations! You have guessed the code."); Console.ForegroundColor = ConsoleColor.Gray; + game.SaveGame(true); breaker = true; } } @@ -87,6 +89,7 @@ namespace Controller if (!breaker) { Console.WriteLine("You have used all of your attempts. Next time you will win!"); + game.SaveGame(false); } Console.WriteLine("\n=========================="); diff --git a/MastermindGame.cs b/MastermindGame.cs index 070de44..8785c93 100644 --- a/MastermindGame.cs +++ b/MastermindGame.cs @@ -1,5 +1,6 @@ -using System; -using PseudoRandomGenerator; +using PseudoRandomGenerator; +using System; +using System.IO; public enum Hints { @@ -8,7 +9,7 @@ public enum Hints Green } -namespace Mastermind +namespace Game { /// <summary> /// Manages the game mechanics. It includes methods for generating the code, providing feedback after each guess, and checking the win condition @@ -22,6 +23,7 @@ namespace Mastermind private int currentAttempt; private int[][] attempts; // Jagged array (array of arrays) to save attempts private (int guess, Hints hint)[,] feedback; + private const string SaveFilePath = "mastermind_gamehistorie.txt"; /// <summary> /// Initializes a new game with the specified difficulty level @@ -125,6 +127,13 @@ namespace Mastermind { int counter = 0; // Counter for tracking positions in the code int[] used = new int[10]; // Tracks used numbers to prevent duplicate hints + + // Resets the feedback-Array + for(int i = 0; i < feedback.GetLength(0); i++) + { + feedback[i,0] = (-1, Hints.None); + } + foreach(int i in input) { bool check = false; @@ -287,6 +296,36 @@ namespace Mastermind } + public void SaveGame(bool win) + { + try + { + Console.WriteLine("Game is saved..."); + using (StreamWriter wr = new StreamWriter(SaveFilePath, append: true)) + { + wr.WriteLine("==================="); + wr.WriteLine($"Datum: {DateTime.Now}"); + wr.WriteLine($"Max attempts: {maxAttempt}"); + wr.WriteLine($"Generated Code: {string.Join("-", code)}"); + wr.WriteLine($"Attempts needed: {currentAttempt}"); + wr.WriteLine($"Result: {(win ? "Won" : "Lost")}"); + wr.WriteLine("Guessed Combinations:"); + + for (int i = 0; i < currentAttempt; i++) + { + wr.WriteLine($" Attempt {i + 1}: {string.Join("-", attempts[i])}"); + } + + wr.WriteLine("==================="); + } + Console.WriteLine("Game was saved successfully"); + } + catch (Exception e) + { + Console.WriteLine($"Error! Something went wrong when saving the game data: {e.Message}"); + } + } + public bool StillHasTrials() { return currentAttempt < maxAttempt; -- GitLab