Ejemplo n.º 1
0
        public void PlayerLosesAfter20Tries()
        {
            Mastermind mastermind = new Mastermind();

            mastermind.SetColorSelection(Colors.Red, Colors.Blue, Colors.Green, Colors.Pink, Colors.Purple);

            Response[] response = new Response[5];
            Response[] expected = new Response[] { Response.MATCH_POSITION, Response.MATCH_POSITION, Response.MATCH_POSITION, Response.MATCH_POSITION, Response.MATCH_POSITION };

            bool gameWon    = false;
            int  roundCount = 0;

            for (int i = 0; i < 20 && !gameWon; i++)
            {
                roundCount = i + 1;

                response = mastermind.Guess(Colors.Orange, Colors.Gray, Colors.White, Colors.Yellow, Colors.Black);
                if (response == expected)
                {
                    gameWon = true;
                }
            }

            Assert.True(!gameWon && (roundCount == 20));
        }
Ejemplo n.º 2
0
        public void Should_not_return_any_matches_when_there_is_no_matches()
        {
            var mastermind = new Mastermind("r r r r");

            var result = mastermind.Guess("b b b b");

            result.ShouldEqual(string.Empty);
        }
Ejemplo n.º 3
0
        public void Should_not_match_pegs_more_than_once()
        {
            var mastermind = new Mastermind("r y y y");

            var result = mastermind.Guess("g r r r");

            result.ShouldEqual("m");
        }
Ejemplo n.º 4
0
        public void Should_not_include_partial_matches_if_the_color_is_already_matched_in_spesific_position()
        {
            var mastermind = new Mastermind("r r r y");

            var result = mastermind.Guess("b b y y");

            result.ShouldEqual("p");
        }
Ejemplo n.º 5
0
        public void Should_return_a_specific_match_if_a_match_is_in_correct_location()
        {
            var mastermind = new Mastermind("r r r y");

            var result = mastermind.Guess("b b b y");

            result.ShouldEqual("p");
        }
Ejemplo n.º 6
0
        public void Should_return_only_p_when_guess_is_correct()
        {
            var mastermind = new Mastermind("r r r y");

            var result = mastermind.Guess("r r r y");

            result.ShouldEqual("pppp");
        }
Ejemplo n.º 7
0
        public void Should_return_several_matches_if_there_are_sevral_matches_in_wrong_location()
        {
            var mastermind = new Mastermind("r r y y");

            var result = mastermind.Guess("b b r r");

            result.ShouldEqual("mm");
        }
Ejemplo n.º 8
0
        public void Should_return_a_match_if_there_is_a_match_in_wrong_location()
        {
            var mastermind = new Mastermind("r y y y");

            var result = mastermind.Guess("b r b b");

            result.ShouldEqual("m");
        }
Ejemplo n.º 9
0
        public void ShouldNotMatchAnyPlayerGuesses()
        {
            Mastermind mastermind = new Mastermind();

            mastermind.SetColorSelection(Colors.Red, Colors.Blue, Colors.Green, Colors.Pink, Colors.Purple);

            Response[] response = mastermind.Guess(Colors.Orange, Colors.Gray, Colors.White, Colors.Yellow, Colors.Black);

            Assert.False(response.All <Response>(r => r == Response.MATCH_POSITION));
        }
Ejemplo n.º 10
0
        public void Guess_ThrowsIfAttemptedAfterGameWon()
        {
            // Arrange
            var underTest = new Mastermind(new int[] { 1, 2, 3, 4 });

            underTest.Guess("1234");

            // Act/Assert
            underTest.Invoking(x => x.Guess("1234")).Should().Throw <InvalidOperationException>();
        }
Ejemplo n.º 11
0
        public void PlayerWinsAfter5Tries()
        {
            Mastermind mastermind = new Mastermind();

            mastermind.SetColorSelection(Colors.Red, Colors.Blue, Colors.Green, Colors.Pink, Colors.Purple);

            Response[] response = new Response[5];
            Response[] expected = new Response[] { Response.MATCH_POSITION, Response.MATCH_POSITION, Response.MATCH_POSITION, Response.MATCH_POSITION, Response.MATCH_POSITION };

            bool gameWon    = false;
            int  roundCount = 0;

            for (int i = 0; i < 20 && !gameWon; i++)
            {
                roundCount = i + 1;

                if (roundCount == 5)
                {
                    response = mastermind.Guess(Colors.Red, Colors.Blue, Colors.Green, Colors.Pink, Colors.Purple);
                }
                else
                {
                    response = mastermind.Guess(Colors.Orange, Colors.Gray, Colors.White, Colors.Yellow, Colors.Black);
                }

                bool allMatch = true;

                for (int j = 0; j < 5 && allMatch; j++)
                {
                    if (response[j] != expected[j])
                    {
                        allMatch = false;
                    }
                }

                if (allMatch)
                {
                    gameWon = true;
                }
            }

            Assert.True(gameWon && (roundCount == 5));
        }
Ejemplo n.º 12
0
        public void Guess_WithCorrectSolution_EndsGame()
        {
            // Arrange
            var underTest = new Mastermind(new int[] { 1, 2, 3, 4 });

            // Act
            underTest.Guess("1234");

            // Assert
            underTest.IsComplete.Should().BeTrue();
        }
Ejemplo n.º 13
0
        public void MatchesGuessCorrectly([Range(1, 6)] int first, [Range(1, 6)] int second, [Range(1, 6)] int third, [Range(1, 6)] int fourth)
        {
            // Arrange
            var underTest = new Mastermind(new int[] { first, second, third, fourth });

            // Act
            var result = underTest.Guess($"{first}{second}{third}{fourth}");

            // Assert
            result.Result.Should().BeTrue();
        }
Ejemplo n.º 14
0
        public void Guess_DecrementsGuessesRemaining()
        {
            // Arrange
            var underTest = new Mastermind(new int[] { 1, 2, 3, 4 });

            // Act
            underTest.Guess("1231");

            // Assert
            underTest.GuessesRemaining.Should().Be(9);
        }
Ejemplo n.º 15
0
        private void HintTestBody(int[] solution, string guess, string expectedHint, bool expectedResult = false)
        {
            // Arrange
            var underTest = new Mastermind(solution);

            // Act
            var(result, hint) = underTest.Guess(guess);

            // Assert
            result.Should().Be(expectedResult);
            hint.Should().Be(expectedHint);
        }
Ejemplo n.º 16
0
        public void Guess_ThrowsIfAttemptedAfterGameLost()
        {
            // Arrange
            var underTest = new Mastermind(new int[] { 1, 2, 3, 4 });

            for (int i = 0; i < 10; i++)
            {
                underTest.Guess("1111");
            }

            // Act/Assert
            underTest.Invoking(x => x.Guess("1111")).Should().Throw <InvalidOperationException>();
        }
Ejemplo n.º 17
0
        public void OnlyFourGuesses()
        {
            Mastermind mastermind = new Mastermind();

            Colors[] selectedColors = new Colors[5] {
                Colors.Red, Colors.Blue, Colors.Green, Colors.Pink, Colors.Red
            };
            mastermind.SetColorSelection(selectedColors);

            Response[] response = mastermind.Guess(Colors.Red, Colors.Gray, Colors.Yellow, Colors.Orange);
            Response[] expected = new Response[] { Response.MATCH_POSITION, Response.NO_MATCH, Response.NO_MATCH, Response.NO_MATCH, Response.NO_MATCH };

            Assert.AreEqual(expected, response);
        }
Ejemplo n.º 18
0
        public void ShouldMatchPlayerFiveGuesses()
        {
            Mastermind mastermind = new Mastermind();

            Colors[] selectedColors = new Colors[5] {
                Colors.Red, Colors.Blue, Colors.Green, Colors.Pink, Colors.Purple
            };
            mastermind.SetColorSelection(selectedColors);

            Response[] response = mastermind.Guess(selectedColors);
            Response[] expected = new Response[] { Response.MATCH_POSITION, Response.MATCH_POSITION, Response.MATCH_POSITION, Response.MATCH_POSITION, Response.MATCH_POSITION };

            Assert.AreEqual(expected, response);
        }
Ejemplo n.º 19
0
        public void Guess_DoesNotEndGameUntilTenthIncorrectAttempt()
        {
            // Arrange
            Mastermind underTest1 = new Mastermind(new int[] { 1, 2, 3, 4 }),
                       underTest2 = new Mastermind(new int[] { 2, 3, 4, 5 });

            for (int i = 0; i < 9; i++)
            {
                underTest1.Guess("1111");
            }

            // Act
            for (int i = 0; i < 9; i++)
            {
                underTest2.Guess("1111");
            }

            underTest1.Guess("1111");

            // Assert
            underTest1.IsComplete.Should().BeTrue();
            underTest2.IsComplete.Should().BeFalse();
        }
Ejemplo n.º 20
0
        public void MatchAllColorsWrongPositions()
        {
            Mastermind mastermind = new Mastermind();

            Colors[] selectedColors = new Colors[5] {
                Colors.Red, Colors.Blue, Colors.Green, Colors.Pink, Colors.Purple
            };
            mastermind.SetColorSelection(selectedColors);

            Response[] response = mastermind.Guess(Colors.Blue, Colors.Green, Colors.Pink, Colors.Purple, Colors.Red);
            Response[] expected = new Response[] { Response.MATCH_COLOR, Response.MATCH_COLOR, Response.MATCH_COLOR, Response.MATCH_COLOR, Response.MATCH_COLOR };

            Assert.AreEqual(expected, response);
        }
Ejemplo n.º 21
0
        public void Match2Positions2Colors()
        {
            Mastermind mastermind = new Mastermind();

            Colors[] selectedColors = new Colors[5] {
                Colors.Red, Colors.Blue, Colors.Green, Colors.Pink, Colors.Purple
            };
            mastermind.SetColorSelection(selectedColors);

            Response[] response = mastermind.Guess(Colors.Red, Colors.Green, Colors.Pink, Colors.Black, Colors.Purple);
            Response[] expected = new Response[] { Response.MATCH_POSITION, Response.MATCH_COLOR, Response.MATCH_COLOR, Response.NO_MATCH, Response.MATCH_POSITION };

            Assert.AreEqual(expected, response);
        }
Ejemplo n.º 22
0
        public void SameColorTwiceOnePositionIsCorrectBothColorsCorrect()
        {
            Mastermind mastermind = new Mastermind();

            Colors[] selectedColors = new Colors[5] {
                Colors.Red, Colors.Blue, Colors.Green, Colors.Pink, Colors.Red
            };
            mastermind.SetColorSelection(selectedColors);

            Response[] response = mastermind.Guess(Colors.Gray, Colors.Red, Colors.Yellow, Colors.Orange, Colors.Red);
            Response[] expected = new Response[] { Response.NO_MATCH, Response.MATCH_COLOR, Response.NO_MATCH, Response.NO_MATCH, Response.MATCH_POSITION };

            Assert.AreEqual(expected, response);
        }