Skip to content
Snippets Groups Projects
Commit ad30d032 authored by Seilenthal's avatar Seilenthal
Browse files

Start of adding Attempts

parent a74c520e
No related branches found
No related tags found
No related merge requests found
......@@ -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
// Converts 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;
......
......@@ -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;
......
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