Skip to content
Snippets Groups Projects
Commit b9be7c77 authored by seilenthalma100744's avatar seilenthalma100744
Browse files

Fixed Auto-save bug

parent 1282d215
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment