/// <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();
        }
Пример #2
0
        public static void Main()
        {
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            BullsAndCowsGame bullsAndCowsGame = new BullsAndCowsGame();

            bullsAndCowsGame.Play();
        }
Пример #3
0
        public void Play()
        {
            Scoreboard scoreBoard = new Scoreboard();

            DisplayWelcomeMessage();
            while (true)
            {
                string command = BullsAndCowsGame.ReadUserCommand();
                if (command == "exit")
                {
                    Console.WriteLine(GOOD_BYE_MSG);
                    return;
                }
                this.ExecuteCommand(scoreBoard, command);
            }
        }
Пример #4
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);
            }
        }
        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);
        }
Пример #6
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);
                }
            }
        }
 public void TestValidGuessWithWhiteThreeDigits()
 {
     BullsAndCowsGame game = new BullsAndCowsGame();
     bool result = game.IsValidGuess("123");
     Assert.IsFalse(result);
 }
 public void TestValidGuessWithWhiteSpace()
 {
     BullsAndCowsGame game = new BullsAndCowsGame();
     bool result = game.IsValidGuess("\n\n\t\t");
     Assert.IsFalse(result);
 }
 public void TestValidGuessWithFourZeros()
 {
     BullsAndCowsGame game = new BullsAndCowsGame();
     bool result = game.IsValidGuess("0000");
     Assert.IsTrue(result);
 }
 public void TestValidGuessWithEmptyString()
 {
     BullsAndCowsGame game = new BullsAndCowsGame();
     bool result = game.IsValidGuess("");
     Assert.IsFalse(result);
 }
 public void TestValidGuessWithDuplicatedDigits()
 {
     BullsAndCowsGame game = new BullsAndCowsGame();
     bool result = game.IsValidGuess("8383");
     Assert.IsTrue(result);
 }
 public void TestIsValidGuessWithLetters()
 {
     BullsAndCowsGame game = new BullsAndCowsGame();
     bool result = game.IsValidGuess("abced");
     Assert.IsFalse(result);
 }
        public void TestDigitsCountGetter()
        {
            BullsAndCowsGame game = new BullsAndCowsGame();

            int expected = game.NumberOfDigits;
            int actual = 4;
            Assert.AreEqual(expected, actual);
        }
        public void IsValidGuess_WrongInputExactlyFourSymbolsLength_WithNumbers_IsFalse()
        {
            BullsAndCowsGame demo = new BullsAndCowsGame();
            var guess = demo.IsValidGuess("12ab");

            Assert.IsFalse(guess);
        }
 public void TestWithNullGuess()
 {
     BullsAndCowsGame game = new BullsAndCowsGame();
     bool result = game.IsValidGuess(null);
 }
        public void IsValidGuess_TwoSymbols_OnlyNumbers_IsFalse()
        {
            BullsAndCowsGame demo = new BullsAndCowsGame();
            var guess = demo.IsValidGuess("10");

            Assert.IsFalse(guess);
        }
 public void TestValidGuess()
 {
     BullsAndCowsGame game = new BullsAndCowsGame();
     bool result = game.IsValidGuess("1234");
     Assert.IsTrue(result);
 }