public int GetPairNumberFromColor(ColorPair pair)
        {
            // Find the major color in the array and get the index
            int            majorIndex = -1;
            DrivingProgram dp         = new DrivingProgram();

            for (int i = 0; i < dp.colorMapMajor.Length; i++)
            {
                if (dp.colorMapMajor[i] == pair.majorColor)
                {
                    majorIndex = i;
                    break;
                }
            }
            // Find the minor color in the array and get the index
            int minorIndex = -1;

            for (int i = 0; i < dp.colorMapMinor.Length; i++)
            {
                if (dp.colorMapMinor[i] == pair.minorColor)
                {
                    minorIndex = i;
                    break;
                }
            }
            // If colors can not be found throw an exception
            if (majorIndex == -1 || minorIndex == -1)
            {
                throw new ArgumentException(
                          string.Format("Unknown Colors: {0}", pair.ToString()));
            }
            // Compute pair number and Return
            // (Note: +1 in compute is because pair number is 1 based, not zero)
            return((majorIndex * dp.colorMapMinor.Length) + (minorIndex + 1));
        }
Example #2
0
        private static void Main(string[] args)
        {
            int            pairNumber = 4;
            DrivingProgram dp         = new DrivingProgram();

            ColorPair testPair1 = dp.GetColorFromPairNumber(pairNumber);

            Console.WriteLine("[In]Pair Number: {0},[Out] Colors: {1}\n", pairNumber, testPair1);
            Debug.Assert(testPair1.majorColor == Color.White);
            Debug.Assert(testPair1.minorColor == Color.Brown);

            pairNumber = 5;
            testPair1  = dp.GetColorFromPairNumber(pairNumber);
            Console.WriteLine("[In]Pair Number: {0},[Out] Colors: {1}\n", pairNumber, testPair1);
            Debug.Assert(testPair1.majorColor == Color.White);
            Debug.Assert(testPair1.minorColor == Color.SlateGray);

            pairNumber = 23;
            testPair1  = dp.GetColorFromPairNumber(pairNumber);
            Console.WriteLine("[In]Pair Number: {0},[Out] Colors: {1}\n", pairNumber, testPair1);
            Debug.Assert(testPair1.majorColor == Color.Violet);
            Debug.Assert(testPair1.minorColor == Color.Green);

            ColorPair testPair2 = new ColorPair()
            {
                majorColor = Color.Yellow, minorColor = Color.Green
            };

            pairNumber = testPair2.GetPairNumberFromColor(testPair2);
            Console.WriteLine("[In]Colors: {0}, [Out] PairNumber: {1}\n", testPair2, pairNumber);
            Debug.Assert(pairNumber == 18);

            testPair2 = new ColorPair()
            {
                majorColor = Color.Red, minorColor = Color.Blue
            };
            pairNumber = testPair2.GetPairNumberFromColor(testPair2);
            Console.WriteLine("[In]Colors: {0}, [Out] PairNumber: {1}", testPair2, pairNumber);
            Debug.Assert(pairNumber == 6);

            DrivingProgram dp1 = new DrivingProgram();

            //The reference manual
            Console.WriteLine("\n");
            Console.WriteLine(dp1);
        }