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
ad30d032
Commit
ad30d032
authored
3 months ago
by
Seilenthal
Browse files
Options
Downloads
Patches
Plain Diff
Start of adding Attempts
parent
a74c520e
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
MastermindController.cs
+16
-7
16 additions, 7 deletions
MastermindController.cs
MastermindGame.cs
+19
-0
19 additions, 0 deletions
MastermindGame.cs
with
35 additions
and
7 deletions
MastermindController.cs
+
16
−
7
View file @
ad30d032
...
...
@@ -8,7 +8,7 @@ namespace Controller
/// </summary>
class
MastermindController
{
private
MastermindGame
spiel
;
private
MastermindGame
game
;
private
string
difficulty
;
/// <summary>
...
...
@@ -16,6 +16,7 @@ namespace Controller
/// </summary>
public
void
Play
()
{
Console
.
WriteLine
();
Console
.
WriteLine
(
"Welcome to Mastermind!"
);
Console
.
WriteLine
(
"======================"
);
...
...
@@ -52,9 +53,9 @@ namespace Controller
// Starts Game
Console
.
WriteLine
(
$"Game starts with the difficulty:
{
difficulty
}
"
);
spiel
=
new
MastermindGame
(
difficulty
);
game
=
new
MastermindGame
(
difficulty
);
while
(
spiel
.
StillHasTrials
())
while
(
game
.
StillHasTrials
())
{
// User input Code
int
[]
userInput
=
ReadInput
();
...
...
@@ -65,24 +66,29 @@ namespace Controller
Console
.
WriteLine
(
i
);
}
game
.
AddAttempt
(
userInput
);
Console
.
WriteLine
(
"Hier ist gerade Ende. Ich bin ein Star holt mich hier raus (Zitat Fabian)!"
);
}
}
private
int
[]
ReadInput
()
{
int
generatedCodeLength
=
spiel
.
GetCodeLength
();
int
generatedCodeLength
=
game
.
GetCodeLength
();
int
[]
input
=
new
int
[
generatedCodeLength
];
string
inputString
;
string
[]
parts
;
string
[]
parts
;
// The code of the user
bool
inputCheck
;
// Checks user input
do
{
inputCheck
=
true
;
Console
.
WriteLine
(
$"Please enter
{
generatedCodeLength
}
numbers (0 - 9) according to this scheme (e.g.: 0
1 2
3) "
);
Console
.
WriteLine
(
$"Please enter
{
generatedCodeLength
}
numbers (0 - 9) according to this scheme (e.g.: 0
-1-2-
3) "
);
inputString
=
Console
.
ReadLine
();
parts
=
inputString
.
Split
(
'-'
);
// Checks if the code is as long as the generated code
if
(
parts
.
Length
!=
generatedCodeLength
)
{
Console
.
WriteLine
(
$"Error! You wrote the wrong amount of numbers. Please enter only
{
generatedCodeLength
}
numbers!"
);
...
...
@@ -91,10 +97,12 @@ namespace Controller
foreach
(
string
i
in
parts
)
{
// Checks if the string is not a single character
if
(
i
.
Length
!=
1
)
{
if
(
i
==
""
)
{
// Prevent incorrect codes caused by a space character
Console
.
WriteLine
(
"Please don't start and end with \" \" and also don't double it!"
);
}
else
{
...
...
@@ -107,9 +115,10 @@ namespace Controller
if
(
parts
.
Length
==
generatedCodeLength
&&
inputCheck
==
true
)
{
// Convert string to int in array
// Convert
s
string to int in array
for
(
int
i
=
0
;
i
<
input
.
Length
;
i
++)
{
// If the input is a number, converts it to an integer
if
(
int
.
TryParse
(
parts
[
i
],
out
int
tmp
))
{
input
[
i
]
=
tmp
;
...
...
This diff is collapsed.
Click to expand it.
MastermindGame.cs
+
19
−
0
View file @
ad30d032
...
...
@@ -14,6 +14,7 @@ namespace Mastermind
private
readonly
bool
duplicates
;
private
readonly
int
maxAttempt
;
private
int
currentAttempt
;
private
int
[][]
attempts
;
// Jagged array (array of arrays) to save attempts
/// <summary>
/// Initializes a new game with the specified difficulty level
...
...
@@ -45,6 +46,13 @@ namespace Mastermind
throw
new
ArgumentException
(
"Invalid difficulty level"
);
}
attempts
=
new
int
[
maxAttempt
][];
// 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
;
GenerateCode
();
...
...
@@ -124,6 +132,17 @@ namespace Mastermind
}
/// <summary>
/// Adds a new attempt
/// </summary>
public
void
AddAttempt
(
int
[]
input
)
{
// Saves the user's input to the attempts array
attempts
[
currentAttempt
]
=
input
;
currentAttempt
++;
}
public
bool
StillHasTrials
()
{
return
currentAttempt
<
maxAttempt
;
...
...
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