From c419853bd3ea9b780ea51fc67d2923f5b440ea28 Mon Sep 17 00:00:00 2001
From: Seilenthal <mark.seilenthal@siemens.com>
Date: Thu, 5 Dec 2024 13:08:30 +0100
Subject: [PATCH] Commenting + Bug-Fixing Saving and Loading Systems

---
 MastermindController.cs |  2 +-
 MastermindGame.cs       | 47 +++++++++++++++++++++++++++++++++--------
 2 files changed, 39 insertions(+), 10 deletions(-)

diff --git a/MastermindController.cs b/MastermindController.cs
index c80d9f9..dd43a42 100644
--- a/MastermindController.cs
+++ b/MastermindController.cs
@@ -1,7 +1,6 @@
 using Game;
 using Mastermind;
 using System;
-using System.ComponentModel.Design;
 using System.IO;
 
 namespace Controller
@@ -259,6 +258,7 @@ namespace Controller
                         {
                             Console.WriteLine("");
                             Console.WriteLine(allGames[i].Trim());
+                            
                         }
                     }
                     Console.WriteLine("Loaded the game data successfully");
diff --git a/MastermindGame.cs b/MastermindGame.cs
index 5a324f9..2d3610a 100644
--- a/MastermindGame.cs
+++ b/MastermindGame.cs
@@ -3,13 +3,6 @@ using System;
 using System.Dynamic;
 using System.IO;
 
-public enum Hints
-{
-    None,
-    Yellow,
-    Green
-}
-
 namespace Game
 {
     /// <summary>
@@ -17,6 +10,13 @@ namespace Game
     /// </summary>
     public class MastermindGame
     {
+        public enum Hints
+        {
+            None,
+            Yellow,
+            Green
+        }
+
         private int[] code;
         private readonly int code_length;
         private readonly bool duplicates;
@@ -70,6 +70,9 @@ namespace Game
                 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)
             {
                 GenerateCode();
@@ -77,6 +80,7 @@ namespace Game
             } 
             else
             {
+                // Checks if a old game exists
                 if (File.Exists(autoSaveFilePath))
                 {
                     try
@@ -86,6 +90,7 @@ namespace Game
                     }
                     catch (Exception e)
                     {
+                        // 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...");
                         GenerateCode();
                         AutoSaveGame();
@@ -98,7 +103,6 @@ namespace Game
                     AutoSaveGame();
                 }
             }
-            //GenerateCode();
 
             // Debugging (Gets removed later)
             foreach(int zahl in code)
@@ -341,6 +345,9 @@ namespace Game
 
         }
 
+        /// <summary>
+        /// Saves the current game state in a txt-file
+        /// </summary>
         private void AutoSaveGame()
         {
             try
@@ -348,14 +355,19 @@ namespace Game
                 Console.WriteLine("Auto-saving...");
                 using (StreamWriter wr = new StreamWriter(autoSaveFilePath))
                 {
+                    // Saves the generated code
                     wr.WriteLine(string.Join("-", code));
 
+                    // Saves the length of the generated code
                     wr.WriteLine(code_length);
 
+                    // Saves the currentAttempt
                     wr.WriteLine(currentAttempt);
 
+                    // Saves the max allowed attempts
                     wr.WriteLine(maxAttempt);
 
+                    // Saves the last played attempts
                     for (int i = 0; i < currentAttempt; i++)
                     {
                         wr.WriteLine(string.Join("-", attempts[i]));
@@ -370,30 +382,40 @@ namespace Game
             }
         }
 
+        /// <summary>
+        /// Loads the saved game state from a txt-file
+        /// </summary>
         private void LoadAutoSaveGame(out int maxAttempt, out int code_length)
         {
             using (StreamReader sr = new StreamReader(autoSaveFilePath))
             {
+                // Reads generatedCode 
                 string secretCodeLine = sr.ReadLine();
                 
+                // Reads the code length
                 string code_lengthLine = sr.ReadLine();
 
+                // Initializes the correct data types
                 code_length = int.Parse(code_lengthLine);
                 string[] secretCode = new string[code_length]; 
                 secretCode = secretCodeLine.Split("-");
                 code = new int[code_length];
 
+                // Converts the generated code to integers
                 for(int i = 0; i < code_length; i++)
                 {
                     code[i] = Convert.ToInt32(secretCode[i]);
                 }
 
+                // Reads and converts currentAttempt
                 string currentAttemptLine = sr.ReadLine();
                 currentAttempt = int.Parse(currentAttemptLine);
 
+                // Reads and converts maxAttempt
                 string maxAttemptLine = sr.ReadLine();
                 maxAttempt = int.Parse(maxAttemptLine);
 
+                // Reads and converts all the last attempts 
                 string line;
                 while((line = sr.ReadLine()) != null)
                 {
@@ -408,6 +430,9 @@ namespace Game
             }
         }
 
+        /// <summary>
+        /// Deletes the saved file that stores the current game state
+        /// </summary>
         public void DeleteAutoSaveFile()
         {
             if(File.Exists(autoSaveFilePath))
@@ -416,6 +441,10 @@ namespace Game
             }
         }
 
+        /// <summary>
+        /// Saves the most important information of the completed game in a txt-file
+        /// </summary>
+        /// <param name="win">Information on whether the code was guessed correctly or not</param>
         public void SaveGame(bool win)
         {
             try
@@ -430,7 +459,6 @@ namespace Game
                     wr.WriteLine($"Attempts needed: {currentAttempt}");
                     wr.WriteLine($"Result: {(win ? "Won" : "Lost")}");
                     wr.WriteLine("Guessed Combinations:");
-                    wr.WriteLine("*");
 
                     for (int i = 0; i < currentAttempt; i++)
                     {
@@ -438,6 +466,7 @@ namespace Game
                     }
 
                     wr.WriteLine("===================");
+                    wr.WriteLine("*");
                 }
                 Console.WriteLine("Game was saved successfully");
             } 
-- 
GitLab