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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
seilenthalma100744
Mastermind
Commits
9ceca470
Commit
9ceca470
authored
9 months ago
by
Seilenthal
Browse files
Options
Downloads
Patches
Plain Diff
Adding Menus
parent
329b5af4
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
MastermindController.cs
+35
-3
35 additions, 3 deletions
MastermindController.cs
MastermindGame.cs
+14
-8
14 additions, 8 deletions
MastermindGame.cs
with
49 additions
and
11 deletions
MastermindController.cs
+
35
−
3
View file @
9ceca470
...
...
@@ -26,6 +26,7 @@ namespace Controller
Console
.
WriteLine
(
"Difficulty: \t[E]asy \nDescription: \tRecommended for beginners.\n"
);
Console
.
WriteLine
(
"Difficulty: \t[M]edium \nDescription: \tRecommended for advanced users.\n"
);
Console
.
WriteLine
(
"Difficulty: \t[H]ard \nDescription: \tRecommended for those who are looking for a challenge.\n"
);
Console
.
WriteLine
(
"Press \"R\" to return to the main menu. \n"
);
bool
checkMenu
=
false
;
do
...
...
@@ -44,8 +45,11 @@ namespace Controller
difficulty
=
"hard"
;
checkMenu
=
true
;
break
;
case
"r"
:
Program
.
MainMenu
();
return
;
default
:
Console
.
WriteLine
(
"Something went wrong! Try again. Only the letters \"E\", \"M\", \"H\" are allowed. "
);
Console
.
WriteLine
(
"Something went wrong! Try again. Only the letters \"E\", \"M\", \"H\"
. \"R\"
are allowed. "
);
break
;
}
...
...
@@ -55,7 +59,9 @@ namespace Controller
Console
.
WriteLine
(
$"Game starts with the difficulty:
{
difficulty
}
"
);
game
=
new
MastermindGame
(
difficulty
);
int
generatedCodeLength
=
game
.
GetCodeLength
();
while
(
game
.
StillHasTrials
())
bool
breaker
=
false
;
while
(
game
.
StillHasTrials
()
&&
!
breaker
)
{
// User input Code
int
[]
userInput
=
ReadInput
(
generatedCodeLength
);
...
...
@@ -69,7 +75,33 @@ namespace Controller
game
.
AddAttempt
(
userInput
);
game
.
DisplayBoard
();
Console
.
WriteLine
(
"Hier ist gerade Ende. Ich bin ein Star holt mich hier raus (Zitat Fabian)!"
);
if
(
game
.
CheckWin
(
userInput
))
{
Console
.
WriteLine
(
"Congratulations! You have guessed the code."
);
breaker
=
true
;
}
}
if
(!
breaker
)
{
Console
.
WriteLine
(
"You have used all of your attempts. Next time you will win!"
);
}
Console
.
Clear
();
Console
.
WriteLine
(
"=========================="
);
Console
.
WriteLine
(
"Do you want to play again?"
);
Console
.
WriteLine
(
"[Y]es or [N]o"
);
Console
.
WriteLine
(
"=========================="
);
switch
(
Console
.
ReadLine
().
ToLower
())
{
case
"Y"
:
Console
.
WriteLine
(
"A new game is starting..."
);
Play
();
return
;
case
"N"
:
Console
.
WriteLine
(
"Returning to main menu"
);
Program
.
MainMenu
();
return
;
}
}
...
...
This diff is collapsed.
Click to expand it.
MastermindGame.cs
+
14
−
8
View file @
9ceca470
using
System
;
using
PseudoRandomGenerator
;
public
enum
Hints
{
None
,
Yellow
,
Green
}
namespace
Mastermind
{
...
...
@@ -15,7 +21,7 @@ namespace Mastermind
private
readonly
int
maxAttempt
;
private
int
currentAttempt
;
private
int
[][]
attempts
;
// Jagged array (array of arrays) to save attempts
private
(
int
guess
,
string
hint
)[,]
feedback
;
private
(
int
guess
,
Hints
hint
)[,]
feedback
;
/// <summary>
/// Initializes a new game with the specified difficulty level
...
...
@@ -48,7 +54,7 @@ namespace Mastermind
}
attempts
=
new
int
[
maxAttempt
][];
feedback
=
new
(
int
,
string
)[
code_length
,
1
];
feedback
=
new
(
int
,
Hints
)[
code_length
,
1
];
// Initializes each inner array to avoid null reference later
for
(
int
i
=
0
;
i
<
maxAttempt
;
i
++)
...
...
@@ -123,7 +129,7 @@ namespace Mastermind
// Checks if the number is in the right place
if
(
i
==
code
[
counter
])
{
feedback
[
counter
,
0
]
=
(
i
,
"g
reen
"
);
feedback
[
counter
,
0
]
=
(
i
,
Hints
.
G
reen
);
counter
++;
check
=
true
;
...
...
@@ -135,7 +141,7 @@ namespace Mastermind
{
if
(
i
==
j
)
{
feedback
[
counter
,
0
]
=
(
i
,
"y
ellow
"
);
feedback
[
counter
,
0
]
=
(
i
,
Hints
.
Y
ellow
);
counter
++;
}
}
...
...
@@ -143,7 +149,7 @@ namespace Mastermind
// Number is not present in the code
else
{
feedback
[
counter
,
0
]
=
(
i
,
"n
one
"
);
feedback
[
counter
,
0
]
=
(
i
,
Hints
.
N
one
);
counter
++;
}
}
...
...
@@ -151,7 +157,7 @@ namespace Mastermind
// Sets numbers to -1 if hint is not green or yellow
for
(
int
i
=
0
;
i
<
feedback
.
GetLength
(
0
);
i
++)
{
if
(
feedback
[
i
,
0
].
hint
!=
"g
reen
"
&&
feedback
[
i
,
0
].
hint
!=
"y
ellow
"
)
if
(
feedback
[
i
,
0
].
hint
!=
Hints
.
G
reen
&&
feedback
[
i
,
0
].
hint
!=
Hints
.
Y
ellow
)
{
feedback
[
i
,
0
].
guess
=
-
1
;
}
...
...
@@ -205,13 +211,13 @@ namespace Mastermind
for
(
int
i
=
0
;
i
<
feedback
.
GetLength
(
0
);
i
++)
{
if
(
feedback
[
i
,
0
].
hint
==
"g
reen
"
)
if
(
feedback
[
i
,
0
].
hint
==
Hints
.
G
reen
)
{
Console
.
ForegroundColor
=
ConsoleColor
.
Green
;
Console
.
Write
(
"X "
);
Console
.
ForegroundColor
=
ConsoleColor
.
Gray
;
}
else
if
(
feedback
[
i
,
0
].
hint
==
"y
ellow
"
)
else
if
(
feedback
[
i
,
0
].
hint
==
Hints
.
Y
ellow
)
{
Console
.
ForegroundColor
=
ConsoleColor
.
Yellow
;
Console
.
Write
(
"Y "
);
...
...
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
sign in
to comment