From b9be7c77a9e5d7459cd89f662f36f5ed6aae0ece Mon Sep 17 00:00:00 2001
From: Mark Seilenthal <seilenthalma100744@th-nuernberg.de>
Date: Sun, 15 Dec 2024 15:07:31 +0100
Subject: [PATCH] Fixed Auto-save bug

---
 MastermindGame.cs | 33 ++++++++++++++++++++-------------
 1 file changed, 20 insertions(+), 13 deletions(-)

diff --git a/MastermindGame.cs b/MastermindGame.cs
index 7292125..e1c4fa3 100644
--- a/MastermindGame.cs
+++ b/MastermindGame.cs
@@ -55,22 +55,12 @@ namespace Game
         /// </param>
         public MastermindGame(string difficulty, bool newGame)
         {
-            DifficultySetup(difficulty, out code_length, out duplicates, out yellowHints, out maxAttempt, out usedTipp);
-
-            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
-            }
-            currentAttempt = 0;
-
             // Starts a new Game or loads a old save
             // (newGame = false -> Trys to load old game; newGame = true -> Starts a new Game)
             if (newGame)
             {
+                DifficultySetup(difficulty, out code_length, out duplicates, out yellowHints, out maxAttempt, out usedTipp);
+                SetupGame();
                 GenerateCode();
                 AutoSaveGame();
             }
@@ -88,6 +78,8 @@ namespace Game
                     {
                         // If an error occurs during loading, a new game is started
                         Console.WriteLine($"Error! Something went wrong with the loading of the savegame: {e.Message}. \nA new game is starting...");
+                        DifficultySetup(difficulty, out code_length, out duplicates, out yellowHints, out maxAttempt, out usedTipp);
+                        SetupGame();
                         GenerateCode();
                         AutoSaveGame();
                     }
@@ -95,11 +87,13 @@ namespace Game
                 else
                 {
                     Console.WriteLine("No Savegame found! Starting a new Game");
+                    DifficultySetup(difficulty, out code_length, out duplicates, out yellowHints, out maxAttempt, out usedTipp);
+                    SetupGame();
                     GenerateCode();
                     AutoSaveGame();
                 }
             }
-
+            
             // Debugging (Gets removed later)
             foreach (int zahl in code)
             {
@@ -107,7 +101,17 @@ namespace Game
             }
         }
 
+        private void SetupGame() {
+            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
+            }
+            currentAttempt = 0;
+        }
 
         private void DifficultySetup(string difficulty, out int code_length, out bool duplicates, out bool yellowHints, out int maxAttempt, out bool usedTipp)
         {
@@ -488,6 +492,9 @@ namespace Game
                 string usedTippLine = sr.ReadLine();
                 usedTipp = bool.Parse(usedTippLine);
 
+                // Initializes the arrays
+                SetupGame();
+
                 // Reads and converts all the last attempts 
                 string line;
                 while ((line = sr.ReadLine()) != null)
-- 
GitLab