Example #1
0
        public List <string> TakeTurn(string guess)
        {
            RemainingAttempts--;

            currentResults.Clear();
            messages.Clear();

            messages.AddRange(ValidateGuess(guess));

            if (messages.Count() != 0)
            {
                messages.Add("You lose a turn!");

                if (RemainingAttempts == 0)
                {
                    messages.Add("You are not the Mastermind. Sorry.");
                }
                else
                {
                    messages.Add(string.Format("You have {0} remaining!", RemainingAttempts));
                }
            }
            else
            {
                for (var i = 0; i < guess.Length; i++)
                {
                    currentResults.Add(targetNumber.CheckDigit(i, int.Parse(guess[i].ToString())));
                }

                if (currentResults.All(x => x == PositionFeedback.Correct))
                {
                    IsWon = true;
                    messages.Add("Correct! You are the Mastermind!");
                }
                else
                {
                    currentResults.Sort(); // randomizes enough

                    messages.Add(DisplayResults(currentResults));

                    if (RemainingAttempts == 0)
                    {
                        messages.Add("You are not the Mastermind. Sorry.");
                    }
                    else
                    {
                        messages.Add(string.Format("Not there yet, you have {0} more chances.", RemainingAttempts));
                    }
                }
            }

            return(messages);
        }
Example #2
0
        public void Test_TargetNumber_Creation()
        {
            // won't compile, that's good
            //var tn = new TargetNumber();

            var tn = new TargetNumber(4, 1, 6);

            //4, 1, 6);

            //position too low
            Assert.ThrowsException <ArgumentException>(() => tn.CheckDigit(-1, 4));

            // position too high
            Assert.ThrowsException <ArgumentException>(() => tn.CheckDigit(12, 4));

            // number too low
            Assert.ThrowsException <ArgumentException>(() => tn.CheckDigit(12, -1));

            // number too high
            Assert.ThrowsException <ArgumentException>(() => tn.CheckDigit(12, 8));
        }