Untitled
unknown
plain_text
2 years ago
4.4 kB
8
Indexable
namespace LaborationCC.Classes
{
internal class MainGame : IGame
{
private readonly IGoalGeneratorFactory goalGeneratorFactory;
private readonly IGuessCheckerFactory guessCheckerFactory;
private readonly IPlayerDataStorage playerDataStorage;
private readonly IIO _io;
private readonly Action userInputHandler;
public MainGame(IGoalGeneratorFactory generatorFactory, IGuessCheckerFactory checkerFactory, IPlayerDataStorage storage, IIO io)
{
goalGeneratorFactory = generatorFactory;
guessCheckerFactory = checkerFactory;
playerDataStorage = storage;
_io = io;
userInputHandler = HandleUserInput;
}
public void Play()
{
_io.WriteLine("Welcome to the Number Guessing Game!");
_io.WriteLine("=====================================\n");
bool playOn = true;
while (playOn)
{
HandleUserInput();
PlayGame();
int numberOfGuess = 1;
_io.WriteLine("Correct, it took " + numberOfGuess + " guesses.\nContinue?\n \"n\" for no or just press any key to continue"); //Den här används tydligen inte överhuvutaget
string answer = _io.ReadLine();
if (answer != null && answer != "" && answer.Substring(0, 1) == "n")
{
playOn = false;
}
}
}
private void PlayGame()
{
_io.WriteLine("Enter your username:\n");
string name = _io.ReadLine();
IGoalGenerator goalGenerator = goalGeneratorFactory.CreateGoalGenerator();
IGuessChecker guessChecker = guessCheckerFactory.CreateGuessChecker();
string goal = goalGenerator.Generate();
_io.WriteLine("New game:\n");
// Comment out or remove the next line to play real games!
_io.WriteLine("For practice, the number is: " + goal + "\n");
string guess = _io.ReadLine();
int nGuess = 1;
string bbcc = guessChecker.Check(goal, guess);
_io.WriteLine(bbcc + "\n");
while (bbcc != "BBBB,")
{
nGuess++;
guess = _io.ReadLine();
_io.WriteLine(guess + "\n");
bbcc = guessChecker.Check(goal, guess);
_io.WriteLine(bbcc + "\n");
}
playerDataStorage.Write(name, nGuess);
playerDataStorage.ShowTopList();
}
private void HandleUserInput()
{
while (true)
{
_io.WriteLine("Select an option:");
_io.WriteLine("1. Play");
_io.WriteLine("2. Clear Player Data");
_io.WriteLine("3. Exit");
string choice = _io.ReadLine();
switch (choice)
{
case "1":
PlayGame();
break;
case "2":
playerDataStorage.ClearData();
_io.WriteLine("Player data cleared.");
break;
case "3":
_io.WriteLine("Exiting the game...");
_io.Exit();
break;
default:
_io.WriteLine("Invalid option. Please try again.");
break;
}
}
}
//private int GetUserInput() //Vi skulle använda den här istället för HandleUserInput
//{
// while (true)
// {
// _io.WriteLine("Select an option:");
// _io.WriteLine("1. Play");
// _io.WriteLine("2. Clear Player Data");
// _io.WriteLine("3. Exit");
// string input = _io.ReadLine();
// int choice = int.Parse(input);
// if (choice >= 1 && 3 >= choice)
// {
// return choice;
// }
// _io.WriteLine("Invalid choice");
// }
//}
}
}
Editor is loading...