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
52b1eb80
Commit
52b1eb80
authored
3 months ago
by
steinti100434
Browse files
Options
Downloads
Patches
Plain Diff
Added Multiplayer , deleted class check
parent
97e84e76
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
Check.cs
+0
-17
0 additions, 17 deletions
Check.cs
MastermindController.cs
+183
-64
183 additions, 64 deletions
MastermindController.cs
MastermindGame.cs
+30
-5
30 additions, 5 deletions
MastermindGame.cs
with
213 additions
and
86 deletions
Check.cs
deleted
100644 → 0
+
0
−
17
View file @
97e84e76
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
Mastermind
{
internal
class
Check
{
public
void
CheckGame
(
int
[]
input
)
{
}
}
}
This diff is collapsed.
Click to expand it.
MastermindController.cs
+
183
−
64
View file @
52b1eb80
...
...
@@ -92,89 +92,208 @@ namespace Controller
switch
(
gammode
)
{
case
"s"
:
Console
.
Clear
();
Console
.
WriteLine
(
"Sibgleplayer mode selected."
);
StartSingleplayerGame
();
break
;
// Starts Game
case
"m"
:
Console
.
Clear
();
Console
.
WriteLine
(
$"Game starts with the difficulty:
{
difficulty
}
"
);
// Implement Multiplayer functionality here
Console
.
WriteLine
(
"Multiplayer mode selected."
);
StartMultiplayerGame
();
// Placeholder, can be expanded based on the game logic for multiplayer
break
;
// Load alt game or start a new one: true -> New game, false -> Load game
bool
newGame
=
UserDecision
();
game
=
new
MastermindGame
(
difficulty
,
newGame
);
int
generatedCodeLength
=
game
.
GetCodeLength
();
}
}
bool
breaker
=
false
;
while
(
game
.
StillHasTrials
()
&&
!
breaker
)
{
// User input Code
int
[]
userInput
=
ReadInput
(
generatedCodeLength
);
private
void
StartSingleplayerGame
()
{
// Starts Game
Console
.
Clear
();
Console
.
WriteLine
(
$"Game starts with the difficulty:
{
difficulty
}
"
);
// Debugging (Gets removed later)
foreach
(
int
i
in
userInput
)
{
Console
.
WriteLine
(
i
);
}
// Load alt game or start a new one: true -> New game, false -> Load game
bool
newGame
=
UserDecision
();
game
=
new
MastermindGame
(
difficulty
,
newGame
);
int
generatedCodeLength
=
game
.
GetCodeLength
(
);
bool
checkMenu
=
false
;
game
.
AddAttempt
(
userInput
);
game
.
DisplayBoard
();
bool
breaker
=
false
;
while
(
game
.
StillHasTrials
()
&&
!
breaker
)
{
// User input Code
int
[]
userInput
=
ReadInput
(
generatedCodeLength
);
if
(
game
.
CheckWin
(
userInput
))
{
Console
.
ForegroundColor
=
ConsoleColor
.
DarkMagenta
;
Console
.
WriteLine
(
"Congratulations! You have guessed the code."
);
Console
.
ForegroundColor
=
ConsoleColor
.
Gray
;
game
.
SaveGame
(
true
);
game
.
DeleteAutoSaveFile
();
SaveLeaderboard
();
breaker
=
true
;
}
// Debugging (Gets removed later)
foreach
(
int
i
in
userInput
)
{
Console
.
WriteLine
(
i
);
}
game
.
AddAttempt
(
userInput
);
game
.
DisplayBoard
();
if
(
game
.
CheckWin
(
userInput
))
{
Console
.
ForegroundColor
=
ConsoleColor
.
DarkMagenta
;
Console
.
WriteLine
(
"Congratulations! You have guessed the code."
);
Console
.
ForegroundColor
=
ConsoleColor
.
Gray
;
game
.
SaveGame
(
true
);
game
.
DeleteAutoSaveFile
();
SaveLeaderboard
();
breaker
=
true
;
}
}
if
(!
breaker
)
{
Console
.
WriteLine
(
"You have used all of your attempts. Next time you will win!"
);
game
.
SaveGame
(
false
);
game
.
DeleteAutoSaveFile
();
}
Console
.
WriteLine
(
"\n=========================="
);
Console
.
WriteLine
(
"Do you want to play again?"
);
Console
.
WriteLine
(
"[Y]es or [N]o"
);
Console
.
WriteLine
(
"==========================\n"
);
checkMenu
=
false
;
while
(!
checkMenu
)
{
switch
(
Console
.
ReadLine
().
ToLower
())
{
case
"y"
:
Console
.
WriteLine
(
"A new game is starting..."
);
checkMenu
=
true
;
Console
.
Clear
();
Play
();
return
;
case
"n"
:
Console
.
WriteLine
(
"Returning to main menu"
);
checkMenu
=
true
;
Console
.
Clear
();
Program
.
MainMenu
(
true
);
return
;
default
:
Console
.
WriteLine
(
"Something went wrong! Try again! Only the letters \"Y\" and \"N\" are allowed."
);
break
;
}
}
}
private
void
StartMultiplayerGame
()
{
Console
.
Clear
();
Console
.
WriteLine
(
"Multiplayer mode selected."
);
Console
.
WriteLine
(
"Player 1, please enter the code for Player 2 to guess:"
);
int
[]
code1
=
ReadInput
(
difficulty
==
"easy"
?
4
:
difficulty
==
"medium"
?
6
:
8
);
Console
.
Clear
();
Console
.
WriteLine
(
"Player 2, please enter the code for Player 1 to guess:"
);
int
[]
code2
=
ReadInput
(
difficulty
==
"easy"
?
4
:
difficulty
==
"medium"
?
6
:
8
);
game
=
new
MastermindGame
(
difficulty
);
game
.
SetCode
(
code1
);
bool
breaker
=
false
;
while
(
game
.
StillHasTrials
()
&&
!
breaker
)
{
Console
.
Clear
();
Console
.
WriteLine
(
"Player 2, try to guess the code!"
);
int
[]
userInput
=
ReadInput
(
game
.
GetCodeLength
());
game
.
AddAttempt
(
userInput
);
game
.
DisplayBoard
();
if
(
game
.
CheckWin
(
userInput
))
{
Console
.
ForegroundColor
=
ConsoleColor
.
DarkMagenta
;
Console
.
WriteLine
(
"Congratulations! Player 2 has guessed the code."
);
Console
.
ForegroundColor
=
ConsoleColor
.
Gray
;
game
.
SaveGame
(
true
);
breaker
=
true
;
}
if
(!
breaker
&&
!
game
.
StillHasTrials
())
{
Console
.
WriteLine
(
"Player 2 has used all of their attempts. Next time you will win!"
);
game
.
SaveGame
(
false
);
breaker
=
true
;
}
if
(!
breaker
)
{
Console
.
WriteLine
(
"Press [Enter] to switch to Player 1."
);
Console
.
ReadLine
();
game
.
SetCode
(
code2
);
game
.
ResetAttempts
();
Console
.
Clear
();
Console
.
WriteLine
(
"Player 1, try to guess the code!"
);
userInput
=
ReadInput
(
game
.
GetCodeLength
());
game
.
AddAttempt
(
userInput
);
game
.
DisplayBoard
();
if
(
game
.
CheckWin
(
userInput
))
{
Console
.
ForegroundColor
=
ConsoleColor
.
DarkMagenta
;
Console
.
WriteLine
(
"Congratulations! Player 1 has guessed the code."
);
Console
.
ForegroundColor
=
ConsoleColor
.
Gray
;
game
.
SaveGame
(
true
);
breaker
=
true
;
}
if
(!
breaker
)
if
(!
breaker
&&
!
game
.
StillHasTrials
()
)
{
Console
.
WriteLine
(
"
You
ha
ve
used all of
you
r attempts. Next time you will win!"
);
Console
.
WriteLine
(
"
Player 1
ha
s
used all of
thei
r attempts. Next time you will win!"
);
game
.
SaveGame
(
false
);
game
.
DeleteAutoSaveFile
()
;
breaker
=
true
;
}
Console
.
WriteLine
(
"\n=========================="
);
Console
.
WriteLine
(
"Do you want to play again?"
);
Console
.
WriteLine
(
"[Y]es or [N]o"
);
Console
.
WriteLine
(
"==========================\n"
);
Console
.
WriteLine
(
"Press [Enter] to switch to Player 2."
);
Console
.
ReadLine
();
checkMenu
=
false
;
while
(!
checkMenu
)
{
switch
(
Console
.
ReadLine
().
ToLower
())
{
case
"y"
:
Console
.
WriteLine
(
"A new game is starting..."
);
checkMenu
=
true
;
Console
.
Clear
();
Play
();
return
;
case
"n"
:
Console
.
WriteLine
(
"Returning to main menu"
);
checkMenu
=
true
;
Console
.
Clear
();
Program
.
MainMenu
(
true
);
return
;
default
:
Console
.
WriteLine
(
"Something went wrong! Try again! Only the letters \"Y\" and \"N\" are allowed."
);
break
;
}
}
break
;
game
.
SetCode
(
code1
);
game
.
ResetAttempts
();
}
}
case
"m"
:
Console
.
Clear
();
// Implement Multiplayer functionality here
Console
.
WriteLine
(
"Multiplayer mode selected."
);
// Placeholder, can be expanded based on the game logic for multiplayer
break
;
Console
.
WriteLine
(
"\n=========================="
);
Console
.
WriteLine
(
"Do you want to play again?"
);
Console
.
WriteLine
(
"[Y]es or [N]o"
);
Console
.
WriteLine
(
"==========================\n"
);
bool
checkMenu
=
false
;
while
(!
checkMenu
)
{
switch
(
Console
.
ReadLine
().
ToLower
())
{
case
"y"
:
Console
.
WriteLine
(
"A new game is starting..."
);
checkMenu
=
true
;
Console
.
Clear
();
Play
();
return
;
case
"n"
:
Console
.
WriteLine
(
"Returning to main menu"
);
checkMenu
=
true
;
Console
.
Clear
();
Program
.
MainMenu
(
true
);
return
;
default
:
Console
.
WriteLine
(
"Something went wrong! Try again! Only the letters \"Y\" and \"N\" are allowed."
);
break
;
}
}
}
public
int
[]
ReadInput
(
int
generatedCodeLength
)
{
int
[]
input
=
new
int
[
generatedCodeLength
];
...
...
This diff is collapsed.
Click to expand it.
MastermindGame.cs
+
30
−
5
View file @
52b1eb80
...
...
@@ -35,6 +35,14 @@ namespace Game
{
DifficultySetup
(
difficulty
,
out
code_length
,
out
duplicates
,
out
yellowHints
,
out
maxAttempt
,
out
usedTipp
);
currentAttempt
=
0
;
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
}
}
/// <summary>
...
...
@@ -97,6 +105,8 @@ namespace Game
}
}
private
void
DifficultySetup
(
string
difficulty
,
out
int
code_length
,
out
bool
duplicates
,
out
bool
yellowHints
,
out
int
maxAttempt
,
out
bool
usedTipp
)
{
switch
(
difficulty
.
ToLower
())
...
...
@@ -244,7 +254,7 @@ namespace Game
}
}
// Number is not present in the code
if
(!
check
)
if
(!
check
)
{
feedback
[
counter
]
=
(
i
,
Hints
.
None
);
counter
++;
...
...
@@ -263,12 +273,13 @@ namespace Game
// Tipp-Mechanik
if
(!
usedTipp
)
{
if
(
correct
>=
3
)
if
(
correct
>=
3
)
{
showTipp
=
true
;
usedTipp
=
true
;
}
}
else
}
else
{
Console
.
WriteLine
(
"You used your Tipp"
);
showTipp
=
false
;
...
...
@@ -364,10 +375,10 @@ namespace Game
}
// Checks if Tipp should be shown, only when 3 numbers where guessed correctly in one attempt (One-Time)
if
(
showTipp
)
if
(
showTipp
)
{
int
position
=
GiveTipp
();
Console
.
WriteLine
(
$"Tipp: At position
{
position
+
1
}
is the number
{
code
[
position
]}
"
);
Console
.
WriteLine
(
$"Tipp: At position
{
position
+
1
}
is the number
{
code
[
position
]}
"
);
}
// Shows how many attempts are left
...
...
@@ -570,5 +581,19 @@ namespace Game
{
return
maxAttempt
-
currentAttempt
;
}
public
void
SetCode
(
int
[]
newCode
)
{
code
=
newCode
;
}
public
void
ResetAttempts
()
{
currentAttempt
=
0
;
attempts
=
new
int
[
maxAttempt
][];
for
(
int
i
=
0
;
i
<
maxAttempt
;
i
++)
{
attempts
[
i
]
=
new
int
[
code_length
];
}
}
}
}
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