using System; namespace Mastermind { enum Colour { Red, Yellow, Orange, Blue, Green, White} class Class1 { public static void MarkPegs ( string master, string guess, out int outNoOfRedPegs, out int outNoOfWhitePegs ) { outNoOfRedPegs = 0; outNoOfWhitePegs = 0; for ( int i=0 ; i < master.Length ; i = i + 1 ) { if ( master[i] == guess[i] ) { outNoOfRedPegs = outNoOfRedPegs + 1; } } } /// Performs a test of the MarkPegs method. The method prints out the number of /// red and white pegs earned by a guess against a master string /// master string /// guess string public static void RunTest ( string master, string guess ) { int NoOfWhitePegs = 0; int NoOfRedPegs = 0; Console.Write ( "Master : " + master + " Guess : " + guess ) ; MarkPegs ( master, guess, out NoOfRedPegs, out NoOfWhitePegs); Console.WriteLine ( " Red pegs : " + NoOfRedPegs + " White pegs : " + NoOfWhitePegs ) ; } /// /// Summary description for Class1. /// /// /// The main entry point for the application. /// static void Main(string[] args) { Console.WriteLine ( "Welcome to MasterMind" ); Console.WriteLine ( "Would you like to see the rules? (enter 0 for no, 1 for yes)" ); string inputString = Console.ReadLine(); int answer = int.Parse(inputString); if (answer == 1) { Console.WriteLine ( "Here are the ruels of the game" ); Console.WriteLine ( "The computer will pick four colours from a group of six" ); Console.WriteLine ( "Your job is to guess the correct sequence of colours" ); Console.WriteLine ( "Try to do it in the least number of guesses possible" ); Console.WriteLine ( "The sequnece can contain pegs of red, yellow, green, blue, white or black" ); Console.WriteLine ( "The same colour can not be used more than once in a sequence" ); } Console.WriteLine ( "You have ten guesses to break the code" ); Console.WriteLine ( "The game is about to begin" ); Console.WriteLine ( "Remember, you are trying to break the code \n" ); //Rules of the game - allows the user to view them or not for faser play for the experienced mastermind players System.Random rand = new System.Random(); Colour[] pegs = new Colour[4]; for ( int i=0 ; i < 4 ; i = i + 1 ) { pegs [i] = (Colour) rand.Next (6); Console.WriteLine("Peg {0} = {1}", i, pegs[i]); } RunTest ( "RBBO", "YROW" ); RunTest ( "RRRR", "RRRR" ); RunTest ( "OOBR", "RBOO" ); Console.WriteLine ( "Press Enter To Exit The Game" ); Console.ReadLine (); } } }