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

Added User Input

parent c3d455a6
No related branches found
No related tags found
No related merge requests found
using Mastermind;
using System;
using System.Net.Http.Headers;
namespace Controller
{
......@@ -53,6 +54,49 @@ namespace Controller
// Starts Game
Console.WriteLine($"Game starts with the difficulty: {difficulty}");
spiel = new MastermindGame(difficulty);
while(spiel.StillHasTrials())
{
// User input Code
int[] userInput = ReadInput();
foreach (int i in userInput)
{
Console.WriteLine(i);
}
}
}
private int[] ReadInput()
{
int generatedCodeLength = spiel.GetCodeLength();
int[] input = new int[generatedCodeLength];
bool check = false;
while (!check)
{
Console.WriteLine($"Please enter {generatedCodeLength} numbers (0 - 9) according to this scheme (e.g.: 0 1 2 3) ");
string inputString = Console.ReadLine();
string[] parts = inputString.Split(' ');
if(parts.Length == generatedCodeLength)
{
// Convert string to int in array
for (int i = 0; i < input.Length; i++)
{
if (int.TryParse(parts[i], out int tmp))
{
input[i] = tmp;
check = true;
}
else
{
Console.WriteLine("Error! Something went wrong with the formatting. Try again");
check = false;
}
}
}
}
return input;
}
}
}
......@@ -12,6 +12,8 @@ namespace Mastermind
private int[] code;
private readonly int code_length;
private readonly bool duplicates;
private readonly int maxAttempt;
private int currentAttempt;
/// <summary>
/// Initializes a new game with the specified difficulty level
......@@ -27,20 +29,27 @@ namespace Mastermind
case "easy":
code_length = 4;
duplicates = false;
maxAttempt = 6;
break;
case "medium":
code_length = 6;
duplicates = false;
maxAttempt = 4;
break;
case "hard":
code_length = 8;
duplicates = true;
maxAttempt = 3;
break;
default:
throw new ArgumentException("Invalid difficulty level");
}
currentAttempt = 0;
GenerateCode();
// Debugging (Gets removed later)
foreach(int zahl in code)
{
Console.WriteLine($"{zahl} ");
......@@ -115,5 +124,15 @@ namespace Mastermind
}
public bool StillHasTrials()
{
return currentAttempt < maxAttempt;
}
public int GetCodeLength()
{
return code.Length;
}
}
}
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