public static void Main(string[] args)
        {
            SecretGenerator  secretGenerator = new SecretGenerator();
            BullsAndCowsGame game            = new BullsAndCowsGame(secretGenerator);

            while (game.CanContinue)
            {
                var input   = Console.ReadLine();
                var message = game.GetMessage(input);
                int count   = 0;
                if (message == "OK")
                {
                    var output = game.Guess(input);
                    count++;
                    Console.WriteLine(output);
                }
                else
                {
                    Console.WriteLine(message);
                }

                if (count == 6)
                {
                    break;
                }
            }

            Console.WriteLine("Game Over");
        }
        public static void Main(string[] args)
        {
            SecretGenerator  secretGenerator = new SecretGenerator();
            BullsAndCowsGame game            = new BullsAndCowsGame(secretGenerator);
            var input = Console.ReadLine();

            while (!game.IsInputValid(input))
            {
                Console.WriteLine("Wrong Input, input again! \n");
                input = Console.ReadLine();
            }

            while (game.CanContinue())
            {
                var output = game.Judge(input);
                if (output == "4A0B")
                {
                    Console.WriteLine(output);
                    break;
                }
                else
                {
                    Console.WriteLine(output);
                    input = Console.ReadLine();
                }
            }

            Console.WriteLine("Game Over");
        }
        public static void Main(string[] args)
        {
            SecretGenerator  secretGenerator = new SecretGenerator();
            BullsAndCowsGame game            = new BullsAndCowsGame(secretGenerator);
            int countGameTimes = 0;

            while (game.CanContinue)
            {
                var input = Console.ReadLine();
                if (!game.IsValid(input))
                {
                    continue;
                }

                var output = game.Guess(input);
                Console.WriteLine(output);
                countGameTimes++;
                if (output == "4A0B")
                {
                    Console.WriteLine("You win!");
                    break;
                }

                if (countGameTimes == 6)
                {
                    break;
                }
            }

            Console.WriteLine("Game Over");
        }
Ejemplo n.º 4
0
        public static void Main(string[] args)
        {
            SecretGenerator  secretGenerator = new SecretGenerator();
            BullsAndCowsGame game            = new BullsAndCowsGame(secretGenerator);

            while (game.CanContinue)
            {
                var input = Console.ReadLine();
                if (!game.IsInputValid(input))
                {
                    Console.WriteLine("Wrong Input, input again");
                    continue;
                }

                var output = game.Guess(input);
                Console.WriteLine(output);
                if (output == "4A0B")
                {
                    Console.WriteLine("You win");
                    break;
                }

                game.CountInputTimes();
            }

            Console.WriteLine("Game Over");
        }
        public static void Main(string[] args)
        {
            SecretGenerator  secretGenerator = new SecretGenerator();
            BullsAndCowsGame game            = new BullsAndCowsGame(secretGenerator);

            const int maxCount = 6;
            var       count    = 0;

            while (game.CanContinue)
            {
                var input = Console.ReadLine();

                while (!CheckInput(ref input))
                {
                    Console.WriteLine("invalid input");
                    input = Console.ReadLine();
                }

                var output = game.Guess(input);
                Console.WriteLine(output);
                count++;

                if (output == "4A0B" || count == maxCount)
                {
                    break;
                }
            }

            Console.WriteLine("Game Over");
        }
        public void Should_create_0A4B_When_Digital_Right_But_Position_Wrong(string guess)
        {
            var    testSecretGenerator = new TestSecretGernerate();
            var    game   = new BullsAndCowsGame(testSecretGenerator);
            string answer = game.Guess(guess);

            Assert.Equal($"0A4B", answer);
        }
        public void Should_create_0A0B_When_All_Digital_And_Position_Wrong()
        {
            var    testSecretGenerator = new TestSecretGernerate();
            var    game   = new BullsAndCowsGame(testSecretGenerator);
            string answer = game.Guess("5678");

            Assert.Equal($"0A0B", answer);
        }
        public void Should_create_BullsAndCowsGame()
        {
            var secretGenerator = new TestSecretGenerator();
            var game            = new BullsAndCowsGame(secretGenerator);

            Assert.NotNull(game);
            Assert.True(game.CanContinue);
        }
Ejemplo n.º 9
0
        public void Should_create_BullsAndCowsGame()
        {
            var mockSecretGenerator = new Mock <SecretGenerator>();
            var game = new BullsAndCowsGame(mockSecretGenerator.Object);

            Assert.NotNull(game);
            Assert.True(game.CanContinue());
        }
Ejemplo n.º 10
0
        public void Should_return_wrong_input_tip_when_digits_not_separated_by_space(string guess)
        {
            var secretGenerator = new TestSecretGenerator();
            var game            = new BullsAndCowsGame(secretGenerator);

            string answer = game.Guess(guess);

            Assert.Equal("Wrong Input, input again", answer);
        }
        public void Should_return_0A0B_when_all_wrong()
        {
            //given
            var secretGenerator = new TestSecretGenerator();
            var game            = new BullsAndCowsGame(secretGenerator);
            //when
            string answerWithMessage = game.Guess("5 6 7 8");

            //then
            Assert.Equal("0A0B", answerWithMessage.Split(" ")[0]);
        }
        public void Should_create_1A2B_When_Digital_Not_All_Right_And_Position_Are_all_Wrong(string guess, string secret)
        {
            var mockSecretGenerator = new Mock <SecretGenerator>();

            mockSecretGenerator.Setup(mock => mock.GenerateSecret()).Returns(secret);
            //var testSecretGenerator = new TestSecretGernerate();
            var    game   = new BullsAndCowsGame(mockSecretGenerator.Object);
            string answer = game.Guess(guess);

            Assert.Equal($"0A2B", answer);
        }
Ejemplo n.º 13
0
        public void Should_return_0A4B_when_no_digital_right_and__all_positions_wrong()
        {
            //var secretGenerator = new SecretGenerator();
            var secretGenerator = new TestSecretGenerator();
            var game            = new BullsAndCowsGame(secretGenerator);
            //when
            string answer = game.Guess("8 7 6 5");

            //then
            Assert.Equal("0A4B", answer);
        }
        public void Should_Return_0A4B_when_all_digit_correct_all_position_wrong(string guess)
        {
            //given
            var testSecretGenerator = new TestSecretGenerator();
            var game = new BullsAndCowsGame(testSecretGenerator);
            //when
            string answer = game.Guess(guess);

            //then
            Assert.Equal("0A4B", answer);
        }
Ejemplo n.º 15
0
        public void Should_return_0A4B_when_digit_wrong_and_position_right(string guess)
        {
            // given
            var secretGenerator = new TestSecretGenerator();
            var game            = new BullsAndCowsGame(secretGenerator);

            // when
            var answer = game.Guess(guess);

            // then
            Assert.Equal("0A4B", answer);
        }
Ejemplo n.º 16
0
        public void Should_return_0A0B_when_all_digit_and_position_wrong()
        {
            // given
            var secretGenerator = new TestSecretGenerator();
            var game            = new BullsAndCowsGame(secretGenerator);

            // when
            var answer = game.Guess("5 6 7 8");

            // then
            Assert.Equal("0A0B", answer);
        }
Ejemplo n.º 17
0
        public void Should_IsInputValid_Show_false(string guess)
        {
            //given
            var mockSecretGenerator = new Mock <SecretGenerator>();
            var game = new BullsAndCowsGame(mockSecretGenerator.Object);

            //when
            var answer = game.IsInputValid(guess);

            //then
            Assert.Equal(false, answer);
        }
Ejemplo n.º 18
0
        public void Should_return_1A1B_when_1_digital_right_and__1_positions_wrong(string guess, string secret)
        {
            var mockSecretGenerator = new Mock <SecretGenerator>();

            mockSecretGenerator.Setup(mock => mock.GenerateSecret()).Returns(secret);
            var game = new BullsAndCowsGame(mockSecretGenerator.Object);
            //when
            string answer = game.Guess(guess);

            //then
            Assert.Equal("1A1B", answer);
        }
        public void Should_return_1A2B_given_secret_1234_guess_1425()
        {
            //given
            var secretGenerator = new TestSecretGenerator();
            var game            = new BullsAndCowsGame(secretGenerator);
            var expect          = "1A2B";
            //when
            var actual = game.Guess("1 4 2 5");

            //then
            Assert.Equal(expect, actual);
        }
        public void Should_return_0A4B_given_secret_1234_guess_4321_and_3421(string guess)
        {
            //given
            var secretGenerator = new TestSecretGenerator();
            var game            = new BullsAndCowsGame(secretGenerator);
            var expect          = "0A4B";
            //when
            var actual = game.Guess(guess);

            //then
            Assert.Equal(expect, actual);
        }
Ejemplo n.º 21
0
        public void Should_Guess_return_0A0B_when_all_digit_position_Wrong(string guess)
        {
            // given
            var secretGenerator = new TestSecretGenerator();
            var game            = new BullsAndCowsGame(secretGenerator);

            // when
            string answer = game.Guess(guess);

            // then
            Assert.Equal("0A0B", answer);
        }
        public void Should_return_error_message_for_invalid_guess(string guess)
        {
            //given
            var mockSecretGenerator = new Mock <SecretGenerator>();

            mockSecretGenerator.Setup(m => m.GenerateSecret()).Returns("1234");
            var game = new BullsAndCowsGame(mockSecretGenerator.Object);
            //when
            string answer = game.Guess(guess);

            //then
            Assert.Equal("Wrong Input, input again", answer);
        }
        public void ShouldReturn4A0BGivenAllDigitAndRightPosition()
        {
            // given
            // var secretGenerator = new SecretGenerator();
            var secretGenerator = new TestSecretGenerator();
            var game            = new BullsAndCowsGame(secretGenerator);

            // when
            string answer = game.Guess("1 2 3 4");

            // then
            Assert.Equal("4A0B", answer);
        }
Ejemplo n.º 24
0
        public void Should_return_0A0B_when_all_digit_and_position_wrong()
        {
            // given
            var mockSecretGenerator = new Mock <SecretGenerator>();

            mockSecretGenerator.Setup(mock => mock.GenerateSecret()).Returns("1234");
            var game = new BullsAndCowsGame(mockSecretGenerator.Object);
            // when
            string answer = game.Guess("5 6 7 8");

            // then
            Assert.Equal("0A0B", answer);
        }
Ejemplo n.º 25
0
        public void Should_return_0A2B_when_two_digit_right_and_no_position_right(string guess, string secret)
        {
            // given
            var mockSecretGenerator = new Mock <SecretGenerator>();

            mockSecretGenerator.Setup(mock => mock.GenerateSecret()).Returns(secret);
            var game = new BullsAndCowsGame(mockSecretGenerator.Object);
            // when
            string answer = game.Guess(guess);

            // then
            Assert.Equal("0A2B", answer);
        }
        public void Should_return_0A4B_when_all_digits_are_right(string guess, string secret)
        {
            //given
            var mockSecretGenerator = new Mock <SecretGenerator>();

            mockSecretGenerator.Setup(m => m.GenerateSecret()).Returns(secret);
            var game = new BullsAndCowsGame(mockSecretGenerator.Object);
            //when
            string answerWithMessage = game.Guess(guess);

            //then
            Assert.Equal("0A4B", answerWithMessage.Split(" ")[0]);
        }
        public void Should_return_1A2B_for_certain_partially_right_cases(string guess, string secret)
        {
            //given
            var mockSecretGenerator = new Mock <SecretGenerator>();

            mockSecretGenerator.Setup(m => m.GenerateSecret()).Returns(secret);
            var game = new BullsAndCowsGame(mockSecretGenerator.Object);
            //when
            string answerWithMessage = game.Guess(guess);

            //then
            Assert.Equal("1A2B", answerWithMessage.Split(" ")[0]);
        }
        public void Should_Return_2A1B_when_digit_partly_correct_position_partly_wrong(string guess, string secrete)
        {
            //given
            var mockSecretGenerator = new Mock <SecretGenerator>();

            mockSecretGenerator.Setup(mock => mock.GenerateSecret()).Returns(secrete);
            var game = new BullsAndCowsGame(mockSecretGenerator.Object);
            //when
            string answer = game.Guess(guess);

            //then
            Assert.Equal("2A1B", answer);
        }
        public void ShouldReturn0A4BGivenAllDigitCorrectAndAllPositionWrong(string guess)
        {
            // given
            // var secretGenerator = new SecretGenerator();
            var secretGenerator = new TestSecretGenerator();
            var game            = new BullsAndCowsGame(secretGenerator);

            // when
            string answer = game.Guess(guess);

            // then
            Assert.Equal("0A4B", answer);
        }
Ejemplo n.º 30
0
        public void Should_return_1A3B_when_3_digit_right_and_1_position_wrong(string guess, string secret)
        {
            //given
            var mockSecretGenerator = new Mock <SecretGenerator>();

            mockSecretGenerator.Setup(mock => mock.GenerateSecret()).Returns(secret);
            var game = new BullsAndCowsGame(mockSecretGenerator.Object);

            //when
            var answer = game.Judge(guess);

            //then
            Assert.Equal("1A3B", answer);
        }
 /// <summary>
 /// Tells the printer to print the scoreboard.
 /// </summary>
 /// <param name="command">The command.</param>
 /// <param name="game">The game which is used to access the printer.</param>
 public override void ProcessCommand(string command, BullsAndCowsGame game)
 {
     if (command == ScoreBoardPrint)
     {
         game.Printer.PrintLeaderBoard(game.ScoreBoard.LeaderBoard);
     }
     else if (this.Successor != null)
     {
         this.Successor.ProcessCommand(command, game);
     }
     else
     {
         throw new ArgumentNullException(NullExceptionText);
     }
 }
 /// <summary>
 /// Decides whether it can process the command or passes it to the next Successor.
 /// </summary>
 /// <param name="command">The command.</param>
 /// <param name="game">Used to access the printer.</param>
 public override void ProcessCommand(string command, BullsAndCowsGame game)
 {
     if (command == ExitCommand)
     {
         game.Printer.PrintMessage(MessageType.Exit);
         Environment.Exit(1);
     }
     else if (this.Successor != null)
     {
         this.Successor.ProcessCommand(command, game);
     }
     else
     {
         throw new ArgumentNullException(NullExceptionText);
     }
 }
 /// <summary>
 /// Disposes the game settings and starts a new game.
 /// </summary>
 /// <param name="command">The command.</param>
 /// <param name="game">The game, whose methods Dispose and Play are used.</param>
 public override void ProcessCommand(string command, BullsAndCowsGame game)
 {
     if (command == RestartCommand)
     {
         game.Dispose();
         game.Play();
     }
     else if (this.Successor != null)
     {
         this.Successor.ProcessCommand(command, game);
     }
     else
     {
         throw new ArgumentNullException(NullExceptionText);
     }
 }
 /// <summary>
 /// Calls the RevealDigit method on the game or passes to the next Successor.
 /// </summary>
 /// <param name="command">The command.</param>
 /// <param name="game">The game on which the RevealDigit method is called.</param>
 public override void ProcessCommand(string command, BullsAndCowsGame game)
 {
     if (command == HelpCommand)
     {
         game.RevealDigit();
         game.CheatAttemptCounter++;
     }
     else if (this.Successor != null)
     {
         this.Successor.ProcessCommand(command, game);
     }
     else
     {
         throw new ArgumentNullException(NullExceptionText);
     }
 }
        /// <summary>
        /// Checks if the number is guessed correctly, if it's of valid length and calculates the results.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <param name="game">The game used to access the printer and other needed properties.</param>
        public override void ProcessCommand(string command, BullsAndCowsGame game)
        {
            if (BullsAndCowsCounter == null)
            {
                BullsAndCowsCounter = new BullsAndCowsCounter(game.NumberForGuess);
            }

            int commandAsNumber;

            if (int.TryParse(command, out commandAsNumber))
            {
                if (command.Length != game.NumberForGuess.Length)
                {
                    game.Printer.PrintMessage(MessageType.InvalidNumberLength);
                    return;
                }

                if (!this.IsGuessedCorrectly(command, game))
                {
                    BullsAndCowsCounter.Dispose();
                    this.BullsAndCowsResult = BullsAndCowsCounter.CountBullsAndCows(command);
                    game.Printer.PrintMessage(MessageType.WrongNumber, this.BullsAndCowsResult.Bulls, this.BullsAndCowsResult.Cows);
                }
                else
                {
                    game.Printer.PrintCongratulationMessage(game.CheatAttemptCounter, game.GuessAttemptCounter);
                    game.IsGuessed = true;
                }
            }
            else if (this.Successor != null)
            {
                this.Successor.ProcessCommand(command, game);
            }
            else
            {
                throw new ArgumentNullException(NullExceptionText);
            }
        }
        /// <summary>
        /// Checks whether the number is guessed correctly.
        /// </summary>
        /// <param name="command">The number to be checked.</param>
        /// <param name="game">The game used to retrieve the NumberToGuess.</param>
        /// <returns>True or false whether the number is guessed.</returns>
        private bool IsGuessedCorrectly(string command, BullsAndCowsGame game)
        {
            if (command == game.NumberForGuess)
            {
                return true;
            }

            return false;
        }