Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Mastermind
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Package Registry
Operate
Terraform modules
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
seilenthalma100744
Mastermind
Commits
c419853b
Commit
c419853b
authored
3 months ago
by
Seilenthal
Browse files
Options
Downloads
Patches
Plain Diff
Commenting + Bug-Fixing Saving and Loading Systems
parent
b8f6b4ae
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
MastermindController.cs
+1
-1
1 addition, 1 deletion
MastermindController.cs
MastermindGame.cs
+38
-9
38 additions, 9 deletions
MastermindGame.cs
with
39 additions
and
10 deletions
MastermindController.cs
+
1
−
1
View file @
c419853b
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"
);
...
...
This diff is collapsed.
Click to expand it.
MastermindGame.cs
+
38
−
9
View file @
c419853b
...
...
@@ -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"
);
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment