Example #1
0
        /// <summary>
        /// Exericse 16.15 in 6th edition
        /// </summary>
        public static void MasterMind()
        {
            Console.WriteLine("Given 4 slots and 4 colors, guess the combination");
            Console.WriteLine("Print number of hits and psuedo-hits");

            string solution = "RGBY";
            string guess    = "GGRR";

            int[] solutionCounts = new int[4];
            int[] guessCounts    = new int[4];

            int hits = 0, pseudoHit = 0;

            for (int i = 0; i < 4; i++)
            {
                if (solution[i] == guess[i])
                {
                    hits++;
                }
                else
                {
                    var solutionColor = Mastermind.CharToColor(solution[i]);
                    var guessColor    = Mastermind.CharToColor(guess[i]);
                    solutionCounts[(int)solutionColor]++;
                    guessCounts[(int)guessColor]++;
                }
            }

            for (int i = 0; i < 4; i++)
            {
                if (guessCounts[i] >= solutionCounts[i])
                {
                    pseudoHit += solutionCounts[i];
                }
            }

            Console.WriteLine("Solution: {0}, Guess: {1}", solution, guess);
            Console.WriteLine("Hits: {0}, Psuedo-Hits: {1}", hits, pseudoHit);
        }