/// <summary> /// USED ONLY FOR TESTING! /// Creates a SecretNumber object having the passed value /// </summary> /// <param name="mockValue">Value for the secret number</param> /// <returns>Mock secret number with predetermined value</returns> public static SecretNumber GetMockNumber(string mockValue) { ValidateNumber(mockValue); SecretNumber mockNumber = new SecretNumber(); mockNumber.FirstDigit = int.Parse(mockValue[0].ToString()); mockNumber.SecondDigit = int.Parse(mockValue[1].ToString()); mockNumber.ThirdDigit = int.Parse(mockValue[2].ToString()); mockNumber.FourthDigit = int.Parse(mockValue[3].ToString()); return mockNumber; }
/// <summary> /// Represents the Cows And Bulls game UI /// </summary> public static void Main() { SecretNumber bullsAndCowsNumber = new SecretNumber(); Scoreboard scoreBoard = new Scoreboard(SCORES_FILE); Console.WriteLine(WELCOME_MESSAGE); string command = string.Empty; while (true) { Console.Write(INPUT_MESSAGE); command = Console.ReadLine(); if (command == EXIT_COMMAND) //this stays here, not in the swich, because break the while loop { Console.WriteLine(GOOD_BYE_MESSAGE); break; } switch (command) { case TOP_COMMAND: Console.Write(scoreBoard); break; case RESTART_COMMAND: Console.WriteLine(); Console.WriteLine(WELCOME_MESSAGE); bullsAndCowsNumber = new SecretNumber(); break; case HELP_COMMAND: Console.WriteLine("The number looks like {0}.", bullsAndCowsNumber.GetCheat()); break; default: try { Result guessResult = bullsAndCowsNumber.CheckUserGuess(command); if (guessResult.Bulls == 4) { string attempt = bullsAndCowsNumber.GuessesCount == 1 ? "attempt" : "attempts"; string cheat = bullsAndCowsNumber.CheatsCount == 1 ? "cheat" : "cheats"; if (bullsAndCowsNumber.CheatsCount == 0) { Console.Write(NUMBER_GUESSED_WITHOUT_CHEATS, bullsAndCowsNumber.GuessesCount, attempt); string name = Console.ReadLine(); scoreBoard.AddScore(name, bullsAndCowsNumber.GuessesCount); } else { Console.WriteLine(NUMBER_GUESSED_WITH_CHEATS, bullsAndCowsNumber.GuessesCount, attempt, bullsAndCowsNumber.CheatsCount, cheat); } Console.Write(scoreBoard); Console.WriteLine(); Console.WriteLine(WELCOME_MESSAGE); bullsAndCowsNumber = new SecretNumber(); } else { Console.WriteLine("{0} {1}", WRONG_NUMBER_MESSAGE, guessResult); } } catch (Exception ex) { //Modified by KrisNickson => this way it will catch all //expcetions the new validation in CheckUserGuess throws if (ex is ArgumentException || ex is FormatException) { Console.WriteLine(INVALID_COMMAND_MESSAGE); } } break; } } scoreBoard.SaveToFile(); }
public void Test_NumberGeneration() { for (int counter = 1; counter <= 100000; counter++) { SecretNumber secretNum = new SecretNumber(); int parsedSecretNum = int.Parse(secretNum.ToString()); bool isValidSecretNum = (parsedSecretNum >= 0 && parsedSecretNum <= 9999); bool expectedValue = true; Assert.AreEqual(expectedValue, isValidSecretNum); } }
public void Test_CheckUserGuess_InputValidation_Under0() { SecretNumber secretNum = new SecretNumber(); string invalidGuess = "-122"; secretNum.CheckUserGuess(invalidGuess); }
public void Test_CheckUserGuess_InputValidation_Over9999() { SecretNumber secretNum = new SecretNumber(); string invalidGuess = "99999"; secretNum.CheckUserGuess(invalidGuess); }
public void Test_CheckUserGuess_InputValidation_NullString() { SecretNumber secretNum = new SecretNumber(); string invalidGuess = null; secretNum.CheckUserGuess(invalidGuess); }
public void Test_CheckUserGuess_CorrectGuess() { SecretNumber sNum = new SecretNumber(); string guess = sNum.ToString(); Result guessResult = sNum.CheckUserGuess(guess); Assert.AreEqual(4, guessResult.Bulls, "Wrong bullsCount count"); Assert.AreEqual(0, guessResult.Cows, "Wrong cowsCount count"); }