コード例 #1
0
        public void InputFourBulls()
        {
            // fix random generator to deliver same digits each time -> 3226
            Random fixedGenerator = new Random(5);
            BullsAndCowsGame game = new BullsAndCowsGame();
            Type type = typeof(BullsAndCowsGame);
            var fieldValue = type.GetField("randomNumberGenerator", BindingFlags.Instance | BindingFlags.NonPublic);
            fieldValue.SetValue(game, fixedGenerator);
            game.Initialize();
            int bullsCount;
            int cowsCount;
            string guess = "3226";
            game.CountBullsAndCows(guess, out bullsCount, out cowsCount);

            Assert.AreEqual(4, bullsCount);
            Assert.AreEqual(0, cowsCount);
        }
コード例 #2
0
        private void ProcessUserInputedDigits(string guess, BullsAndCowsGame bullsAndCows)
        {
            int bullsCount = 0;
            int cowsCount = 0;

            bullsAndCows.CountBullsAndCows(guess, out bullsCount, out cowsCount);

            this.attemptsCount++;

            if (bullsCount == bullsAndCows.NumberOfDigits)
            {
                if (helpUsedCount == 0)
                {
                    Console.WriteLine(Messages.WIN_WITH_CHEATS, this.attemptsCount, this.helpUsedCount);
                }
                else
                {
                    Console.WriteLine(Messages.WIN_WITHOUT_CHEATS, this.attemptsCount);
                }

                Console.WriteLine(new string('-', 80));

                if (this.helpUsedCount == 0 && this.scoreBoard.IsHighScore(this.attemptsCount))
                {
                    Console.WriteLine(Messages.IN_SCOREBOARD);
                    string name = Console.ReadLine();
                    while (string.IsNullOrWhiteSpace(name))
                    {
                        Console.WriteLine(Messages.INVALID_NAME);
                        name = Console.ReadLine();
                    }
                    this.scoreBoard.Add(new PlayerScore(name, attemptsCount));
                }
                else
                {
                    Console.WriteLine(Messages.OUT_SCOREBOARD);
                }

                Console.WriteLine(this.scoreBoard);
                this.StartGame();
            }
            else
            {
                Console.WriteLine("{0} Bulls: {1}, Cows: {2}",
                        Messages.WRONG_GUESS, bullsCount, cowsCount);
            }
        }