· 7 years ago · Nov 04, 2018, 06:44 PM
1 bool goAgain = true;
2 while (goAgain == true)
3 {
4 int num_guess = 0; //a variable for the number of guesses the user takes
5 string guess_1; //one quarter of the full guess
6 string guess_2;
7 string guess_3;
8 string guess_4;
9 string guess = "";
10 Console.WriteLine("Welcome to the game Mastermind.\nYou have 8 different colours (red, green, blue, yellow, brown, orange, black and white).\nPick between these for a guess out of four colours and see if you got it correct.\nYou have 10 guesses. ");
11 Random rnd = new Random();
12 string[] possible_colours = new string[] { "red", "green", "blue", "yellow", "brown", "orange", "black", "white" };
13 int secret_key1 = rnd.Next(possible_colours.Length);
14 int secret_key2 = rnd.Next(possible_colours.Length);
15 int secret_key3 = rnd.Next(possible_colours.Length);
16 int secret_key4 = rnd.Next(possible_colours.Length);
17 //while (secret_key1 == secret_key2 || secret_key1 == secret_key3 || secret_key1 == secret_key4 || secret_key2 == secret_key3 || secret_key2 == secret_key4 || secret_key3 == secret_key4)
18 //{
19 // int secret_key1 = rnd.Next(possible_colours.Length);
20 // int secret_key2 = rnd.Next(possible_colours.Length);
21 // int secret_key3 = rnd.Next(possible_colours.Length);
22 // int secret_key4 = rnd.Next(possible_colours.Length);
23 //}
24 string secret_key = Convert.ToString(possible_colours[secret_key1] + possible_colours[secret_key2] + possible_colours[secret_key3] + possible_colours[secret_key4]);
25 Console.WriteLine(secret_key);
26 while (num_guess != 10)
27 {
28 int correctguess = 0;
29 int halfcorrectguess = 0;
30 while (true)
31 {
32 Console.WriteLine("Enter your first guess:");
33 guess_1 = (Console.ReadLine());
34 int colour_present = Array.IndexOf(possible_colours, guess_1, 0, 7);
35 if (guess_1 == possible_colours[secret_key1])
36 {
37 correctguess++;
38 break;
39 }
40 else if (guess_1 == possible_colours[secret_key2] || guess_1 == possible_colours[secret_key3] || guess_1 == possible_colours[secret_key4])
41 {
42 halfcorrectguess++;
43 break;
44 }
45 else if (colour_present == -1 || string.IsNullOrEmpty(guess_1))
46 {
47 Console.WriteLine("Please enter a correct colour (Red, Blue, Green, Yellow, Brown, Orange, Black Or White)");
48 Thread.Sleep(1000);
49 continue;
50 }
51 else
52 {
53 break;
54 }
55
56 }
57 while (true)
58 {
59 Console.WriteLine("Enter your second guess:");
60 guess_2 = (Console.ReadLine());
61 int colour_present2 = Array.IndexOf(possible_colours, guess_2, 0, 7);
62 if (guess_2 == possible_colours[secret_key2])
63 {
64 correctguess++;
65 break;
66 }
67 else if (guess_2 == possible_colours[secret_key1] || guess_2 == possible_colours[secret_key3] || guess_2 == possible_colours[secret_key4])
68 {
69 halfcorrectguess++;
70 break;
71 }
72 else if (colour_present2 == -1 || string.IsNullOrEmpty(guess_2))
73 {
74 Console.WriteLine("Please enter a correct colour (Red, Blue, Green, Yellow, Brown, Orange, Black Or White)");
75 Thread.Sleep(1000);
76 continue;
77 }
78 else
79 {
80 break;
81 }
82 }
83 while (true)
84 {
85 Console.WriteLine("Enter your third guess:");
86 guess_3 = (Console.ReadLine());
87 int colour_present3 = Array.IndexOf(possible_colours, guess_3, 0, 7);
88 if (guess_3 == possible_colours[secret_key3])
89 {
90 correctguess++;
91 break;
92 }
93 else if (guess_3 == possible_colours[secret_key1] || guess_3 == possible_colours[secret_key2] || guess_3 == possible_colours[secret_key4])
94 {
95 halfcorrectguess++;
96 break;
97 }
98 else if (colour_present3 == -1 || string.IsNullOrEmpty(guess_3))
99 {
100 Console.WriteLine("Please enter a correct colour (Red, Blue, Green, Yellow, Brown, Orange, Black Or White)");
101 Thread.Sleep(1000);
102 continue;
103 }
104 else
105 {
106 break;
107 }
108 }
109 while (true)
110 {
111 Console.WriteLine("Enter your fourth guess:");
112 guess_4 = (Console.ReadLine());
113 int colour_present4 = Array.IndexOf(possible_colours, guess_4, 0, 7);
114 if (guess_4 == possible_colours[secret_key4])
115 {
116 correctguess++;
117 break;
118 }
119 else if (guess_4 == possible_colours[secret_key1] || guess_4 == possible_colours[secret_key2] || guess_4 == possible_colours[secret_key3])
120 {
121 halfcorrectguess++;
122 break;
123 }
124 else if (colour_present4 == -1 || string.IsNullOrEmpty(guess_4))
125 {
126 Console.WriteLine("Please enter a correct colour (Red, Blue, Green, Yellow, Brown, Orange, Black Or White)");
127 Thread.Sleep(1000);
128 continue;
129 }
130 else
131 {
132 break;
133 }
134 }
135
136 guess = (guess_1 + guess_2 + guess_3 + guess_4);
137 if (guess == secret_key)
138 {
139 num_guess = num_guess + 1;
140 break;
141 }
142 else if (guess != secret_key)
143 {
144 Console.WriteLine("Guess again: You have guessed " + correctguess + " in the correct position and " + halfcorrectguess + " that aren't in the correct position but in the secret code");
145 num_guess = num_guess + 1;
146 Thread.Sleep(500);
147 continue;
148 }
149 }
150 if (num_guess == 10 && secret_key != guess)
151 {
152 Console.WriteLine("Sorry you guessed incorrect");
153 Thread.Sleep(1000);
154 }
155 else
156 {
157 Console.WriteLine("Congratulations, you guessed correctly, it took you "+num_guess+" guesses");
158 Thread.Sleep(1000);
159 }
160 Console.WriteLine("Would you like another go?");
161 string goAgainAnswer = (Console.ReadLine());
162 if (goAgainAnswer.Contains("yes"))
163 {
164 goAgain = true;
165 Console.Clear();
166 }
167 else if (goAgainAnswer.Contains("no"))
168 {
169 goAgain = false;
170 Console.Clear();
171 Console.WriteLine("Thank you for playing");
172 Thread.Sleep(1000);
173 Environment.Exit(0);
174 }
175 }
176 Console.ReadKey();