예제 #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));
        }
예제 #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);
        }
예제 #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");
        }
예제 #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");
        }
예제 #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");
        }
예제 #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");
        }
예제 #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");
        }
예제 #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");
        }
예제 #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));
        }
예제 #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>();
        }
예제 #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));
        }
예제 #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();
        }
예제 #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();
        }
예제 #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);
        }
예제 #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);
        }
예제 #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>();
        }
예제 #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);
        }
예제 #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);
        }
예제 #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();
        }
예제 #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);
        }
예제 #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);
        }
예제 #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);
        }