/// <summary> /// Checks the output from previous guess and generates a text information for the player. /// </summary> /// <param name="output">Output from the previous round.</param> /// <returns>Information text about previous guess.</returns> private string GetStatus(char[] output) { int correctAnswersCount = output.Count(c => c == Answers.CORRECT_ANSWER); int correctColorsCount = output.Count(c => c == Answers.COLOR_EXISTS); return(DynamicConsoleTexts.GetAnswerOutput(correctAnswersCount, correctColorsCount, gameType)); }
/// <summary> /// Take the user's input and check if it's correct. /// </summary> private void TakeInput() { string input = Console.ReadLine(); bool doCharactersExist = !Regex.IsMatch(input, $"[^{availableCharacters}]"); if (!doCharactersExist || input.Length != game.CodeLength) { Console.WriteLine(StaticConsoleTexts.WrongCode); AskForInput(); return; } game.CheckCode(input); if (game.Status == GameStatus.Won) { Console.WriteLine(DynamicConsoleTexts.GetWinningInformation(game.Round)); AskForNewGame(); } else if (game.Status == GameStatus.Lost) { Console.WriteLine(DynamicConsoleTexts.GetLosingInformation(game.Round, game.Code)); AskForNewGame(); } else { AskForInput(); } }
/// <summary> /// Asks the AI for a guess. /// </summary> /// <param name="combinations">Combinations left.</param> /// <param name="code">Secret code that user came up with.</param> private void AskAI(List <string> combinations, string code) { computerRound++; if (computerRound == timeLimit) { Console.WriteLine(DynamicConsoleTexts.GetComputerLostInformation(computerRound, code)); Console.ReadKey(); DisplayStartingWindow(); } Tuple <List <string>, string> answer = AI.PlayWithAI(combinations, code); Console.Beep(); Console.Clear(); Console.WriteLine(StaticConsoleTexts.ComputerAnswer); foreach (char color in answer.Item2.ToCharArray()) { ConsoleColorHelper.WriteColor(color); } Console.WriteLine($"\n{StaticConsoleTexts.IsCorrect}"); foreach (char color in code.ToCharArray()) { ConsoleColorHelper.WriteColor(color); } AskUserIfTheAnswerIsCorrect(answer, code); }
/// <summary> /// Ask user for his answer. /// </summary> private void AskForInput() { int currentRound = game.TimeLimit - game.Round; Console.Clear(); Console.WriteLine(DynamicConsoleTexts.GetInformationText(game.CodeLength)); Console.WriteLine(DynamicConsoleTexts.GetNextRoundInformation(game.CodeLength, currentRound)); GenerateGameBoard(game.User.Rounds); TakeInput(); }
/// <summary> /// Ask the user to come up with a code. /// </summary> private void AskForCode() { Console.Clear(); Console.WriteLine(DynamicConsoleTexts.GetAskForCodeInformation(4, availableCharacters)); AI = new HumanStrategy(codeLength); string code = Console.ReadLine(); bool doCharactersExist = !Regex.IsMatch(code, $"[^{availableCharacters}]"); if (doCharactersExist || code.Length == codeLength) { List <string> combinations = new List <string>(); AskAI(combinations, code); } else { Console.WriteLine($"{StaticConsoleTexts.WrongCode}\n"); AskForCode(); } }
/// <summary> /// Ask the user whether the answer computer came up with was correct. /// </summary> /// <param name="computerAnswer">Contains computer guess and a list of combinations left.</param> /// <param name="code">Secret code that user came up with.</param> private void AskUserIfTheAnswerIsCorrect(Tuple <List <string>, string> computerAnswer, string code) { Console.WriteLine("\n\n"); char userInput = Console.ReadKey().KeyChar; if (userInput == YesOption) { Console.WriteLine(DynamicConsoleTexts.GetComputerWonInformation(computerRound)); Console.ReadKey(); DisplayStartingWindow(); } else if (userInput == NoOption) { AskForScore(); AskAI(computerAnswer.Item1, code); } else { AskUserIfTheAnswerIsCorrect(computerAnswer, code); } }