/// <summary>
        /// Creates the needed requirements for the game, initializes it and starts it.
        /// </summary>
        private void Initialize()
        {
            NumberCommandProcessor numberCommandProcessor = new NumberCommandProcessor();
            HelpCommandProcessor helpCommandProcessor = new HelpCommandProcessor();
            numberCommandProcessor.SetSuccessor(helpCommandProcessor);

            TopCommandProcessor topCommandProcessor = new TopCommandProcessor();
            helpCommandProcessor.SetSuccessor(topCommandProcessor);

            RestartCommandProcessor restartCommandProcessor = new RestartCommandProcessor();
            topCommandProcessor.SetSuccessor(restartCommandProcessor);

            ExitCommandProcessor exitCommandProcessor = new ExitCommandProcessor();
            restartCommandProcessor.SetSuccessor(exitCommandProcessor);

            InvalidCommandProcessor invalidCommandProcessor = new InvalidCommandProcessor();
            exitCommandProcessor.SetSuccessor(invalidCommandProcessor);

            ScoreBoard scoreBoard = new ScoreBoard(MaxPlayersInScoreboard);
            MessageFactory messageFactory = new MessageFactory();
            IPrinter printer = new Printer(messageFactory);
            IRandomNumberProvider randomNumberProvider = RandomNumberProvider.Instance;

            BullsAndCowsGame game = new BullsAndCowsGame(randomNumberProvider, scoreBoard, printer, numberCommandProcessor);

            game.Initialize();
            game.Play();
        }
        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);
        }
Example #3
0
        /// <summary>
        /// Initial new Bulls and cows game
        /// </summary>
        public void StartGame()
        {
            Console.WriteLine(Messages.START_EXPRESSION);

            BullsAndCowsGame bullsAndCows = new BullsAndCowsGame();
            bullsAndCows.Initialize();

            this.helpUsedCount = 0;
            this.attemptsCount = 0;

            while (this.isGameRunning)
            {
                Console.WriteLine(Messages.ENTER_GUESS);
                string command = Console.ReadLine().Trim().ToLower();

                if (command == HelpCommand)
                {
                    string digitsRevealed = bullsAndCows.RevealRandomDigit(ref this.helpUsedCount);
                    Console.WriteLine(digitsRevealed);
                }
                else if (command == TopCommand)
                {
                    Console.WriteLine(this.scoreBoard);
                }
                else if (command == RestartCommand)
                {
                    Console.Clear();
                    this.StartGame();
                }
                else if (command == ExitCommand)
                {
                    this.isGameRunning = false;
                    Console.WriteLine("Good bye!");
                }
                else if (bullsAndCows.IsValidGuess(command))
                {
                    this.ProcessUserInputedDigits(command, bullsAndCows);
                }
                else
                {
                    Console.WriteLine(Messages.WRONG_INPUT);
                }
            }
        }