Exemplo n.º 1
0
        public void number_of_guesses_should_not_change()
        {
            var x = new GuessingGame(73, 5);

            Assert.AreEqual(5, x.GuessesLeft);

            x.Guess(10);
            Assert.AreEqual(4, x.GuessesLeft);

            x.Guess(10);
            Assert.AreEqual(4, x.GuessesLeft);

            x.Guess(10);
            Assert.AreEqual(4, x.GuessesLeft);
        }
Exemplo n.º 2
0
        public void normal_play_that_ends_with_correct_guess()
        {
            var x = new GuessingGame(73, 4);

            Assert.AreEqual(4, x.GuessesLeft);

            Assert.AreEqual(GuessResult.Higher, x.Guess(50));
            Assert.AreEqual(3, x.GuessesLeft);

            Assert.AreEqual(GuessResult.Lower, x.Guess(80));
            Assert.AreEqual(2, x.GuessesLeft);

            Assert.AreEqual(GuessResult.Success, x.Guess(73));
            Assert.AreEqual(1, x.GuessesLeft);
        }
Exemplo n.º 3
0
        public void normal_play_that_ends_with_game_over()
        {
            var x = new GuessingGame(46, 4);

            Assert.AreEqual(4, x.GuessesLeft);

            Assert.AreEqual(GuessResult.Lower, x.Guess(66));
            Assert.AreEqual(3, x.GuessesLeft);

            Assert.AreEqual(GuessResult.Lower, x.Guess(50));
            Assert.AreEqual(2, x.GuessesLeft);

            Assert.AreEqual(GuessResult.Higher, x.Guess(30));
            Assert.AreEqual(1, x.GuessesLeft);

            Assert.AreEqual(GuessResult.Fail, x.Guess(35));
            Assert.AreEqual(0, x.GuessesLeft);
        }
Exemplo n.º 4
0
    private int PlayTheGuessingGame()
    {
        var guesser    = new PsychicGuesser();
        var game       = new GuessingGame();
        var lastResult = -1;

        while (lastResult != 0)
        {
            lastResult = game.Guess(guesser.MakeAGuess(lastResult));
        }
        return(game.Guesses);
    }
        private void Guess(object obj)
        {
            int result = game.Guess(int.Parse(Input));

            if (result == 0)
            {
                SuccessfullGuess();
            }
            else
            {
                Message = result > 0 ? "You need to guess lower" : "You need to guess higher";
            }
        }
Exemplo n.º 6
0
        public void after_a_lot_of_correct_guesses_all_remaining_result_should_be_game_over()
        {
            var x = new GuessingGame(73, 4);

            Assert.AreEqual(GuessResult.Higher, x.Guess(5));
            Assert.AreEqual(GuessResult.Higher, x.Guess(50));
            Assert.AreEqual(GuessResult.Higher, x.Guess(52));
            Assert.AreEqual(GuessResult.Success, x.Guess(73));

            Assert.ThrowsException <Exception>(() => x.Guess(5));
            Assert.ThrowsException <Exception>(() => x.Guess(5));
        }
Exemplo n.º 7
0
        public IActionResult OnPost()
        {
            game = new GuessingGame(
                (int)HttpContext.Session.GetInt32(RAND_NUMBER)
                );

            //get user's guess from form
            string guess = Request.Form["UserGuess"];
            int    guessInt;

            if (int.TryParse(guess, out guessInt))
            {
                Result = game.Guess(guessInt);
            }
            else
            {
                Result = "Please enter a valid integer";
            }
            return(Page());
        }
Exemplo n.º 8
0
        public IActionResult Index(int guessNumber, string autoRestart)
        {
            var rndNumber  = HttpContext.Session.GetInt32(SessionKeyRndNumber);
            var tryNumbers = HttpContext.Session.GetString(SessionKeyTryNumber);

            string tryingNumbers = "";
            string startAgain    = "";

            tryNumbers = GuessingGame.TryNumbers(tryNumbers, guessNumber);

            HttpContext.Session.SetString(SessionKeyTryNumber, tryNumbers);

            GuessingGame guessingGame = new GuessingGame();

            string guessResult = guessingGame.Guess(guessNumber, (int)rndNumber);

            if (guessResult == "Congratulation!")
            {
                tryingNumbers = "The number was " + rndNumber + " and your guessing numbers are: " + tryNumbers;

                // Button when finish guessing
                ViewBag.Restart = "Show";

                // Autorestart when finish guessing
                startAgain = "You can now continue guessing on a new number.";
                int getRndNumber = GuessingGame.RandomNumber();
                HttpContext.Session.SetInt32(SessionKeyRndNumber, getRndNumber);
                HttpContext.Session.SetString(SessionKeyTryNumber, "");

                // Redirect to run HttpGet
                //return RedirectToAction("Index", "GuessingGame");
            }

            ViewBag.Index   = guessResult + autoRestart;
            ViewBag.Numbers = tryingNumbers;
            ViewBag.Again   = startAgain;
            ViewBag.Rnd     = rndNumber;

            return(View());
        }
Exemplo n.º 9
0
        public void should_return_gameover_when_no_guesses_left()
        {
            var x = new GuessingGame(73, 1);

            Assert.AreEqual(GuessResult.Fail, x.Guess(5));
        }
Exemplo n.º 10
0
        public void should_return_correct_when_correct_guess()
        {
            var x = new GuessingGame(73, 4);

            Assert.AreEqual(GuessResult.Success, x.Guess(73));
        }