The first round of the game seems okay. The game will generate the result but >1 round , the result wont be show.
And the is another mistake I found.
For example, when the random no. is 5134 and my guess is 5143
the game will generate the code O O #, which is wrong, it should be O O # #.
But I cannot find out the reason.
Thank you so much!!
Task :
1. Create a New Project and give your project the name Lab3b1.
2. Add a source file to your project, called MasterMind.cpp.
3. Write a C++ program to implement the game MasterMind. Your code should perform the
following.
i. Randomly place different values to an array of 4 integer values. The values placed
should be in the range 1 to 6.
ii. Allow a user to guess the number and computer should respond to the guess with clues.
• A “O” symbol means “right value, right position”. That is one of your digits has the
right value in the right position.
• A “#” symbol means “right value, wrong position”. That is one of your digits has
the right value, but is in the wrong position.
• If the computer responds with no pegs, that means everything is wrong.
(Note: You shouldn’t give any cues to user about their input integers. What you need
to do is to let him/her know how many integers are “right value, right position” and
how many are in “right value, but in wrong position”.
REQUIREMENT: “O” symbol has to be placed before any “#” symbol)
iii. The output of the game should look like below:
MasterMind (For checking: 6 4 3 1)
Enter four digits (1-6) separated by a space
--------------------------------------------
Round 1:
Enter Guess: 1 2 3 4
O # #
--------------------------------------------
Round 2:
Enter Guess: 4 6 3 1
O O # #
--------------------------------------------
...
--------------------------------------------
Round 8:
Enter Guess: 6 4 3 1
O O O O
--------------------------------------------
Congratulations! You win the game in 8 steps
The input which is underlined is the user’s input to a question.
4. Compile your program and test it by executing your program.
and these and my code.
#include <iostream>
#include <ctime>
#include <limits>
using namespace std;
int main()
{
// Declare variable numbers to store four integers for the random output
// Declare variable numbers to store four integers for the user input
int input[4],number[4];
int counter = 1;
int counter2;
int roundN = 1;
int rightnumber = 0;
int rightpostion = 0;
int wrongnumber = 0;
// Initialize random seed
srand( (unsigned int)time(NULL) );
// Generate four random numbers between 1 to 6
// and the four outputs are all distinct and in valid range
// because of the SWITCH
// if the numbers are the same then it will return to the FOR loop and SWITCH
// and get the new random number from the beginning
for(counter>0; counter<=4; counter++)
switch(counter)
{
case 1:
number[0] = rand() % 6 + 1;
break;
case 2:
number[1] = rand() % 6 + 1;
if (number[1] == number[0]){
counter=1;
continue;}
case 3:
number[2] = rand() % 6 + 1;
if (number[2] == number[0] || number[2] == number[1]){
counter=2;
continue;}
case 4:
number[3] = rand() % 6 + 1;
if (number[3] == number[0] || number[3] == number[1] || number[3] == number[2]){
counter=3;
continue;}
}
// Display the random four numbers
cout << "MasterMind (For checking: " << number[0] << " " << number[1] << " " << number[2] << " " << number[3] <<")" <<endl;
cout << "Enter four digits (1-6) separated by a space" <<endl;
cout << "--------------------------------------------" <<endl;
do
{
for(counter2=1; counter2<=1; counter2++){
cout << "Round "<< roundN <<":"<<endl;
roundN++;
cout << "Enter Guess: ";
cin >> input[0];
cin >> input[1];
cin >> input[2];
cin >> input[3];
if (input[0] == input[1] || input[0] == input[2] || input[0] == input[3] ||
input[1] == input[2] || input[1] == input[3] || input[2] == input[3])
{
counter2 = 0;
continue;
}
else if (input[0] > 6 || input[0] < 1 || input[1] > 6 || input[1] < 1 || input[2] > 6 || input[2] < 1 || input[3] > 6 || input[3] < 1 )
{
counter2 = 0;
continue;
}
else
break;
}
{for (int i = 0; i < 4; i++){
if (input[0] == number[0]){
rightnumber ++;
rightpostion ++;}
if (input[1] == number[1]){
rightnumber ++;
rightpostion ++;}
if (input[2] == number[2]){
rightnumber ++;
rightpostion ++;}
if (input[3] == number[3]){
rightnumber ++;
rightpostion ++;}
if ((input[0] == number[1]) || (input[0] == number[2]) || (input[0] == number[3]))
rightnumber ++;
else if ((input[1] == number[0]) || (input[1] == number[2]) || (input[1] == number[3]))
rightnumber ++;
else if ((input[2] == number[1]) || (input[2] == number[0]) || (input[2] == number[3]))
rightnumber ++;
else if ((input[3] == number[1]) || (input[3] == number[2]) || (input[3] == number[0]))
rightnumber ++;
if(rightnumber == 4 && rightpostion == 4)
cout << "O O O O" << endl;
else if(rightnumber == 4 && rightpostion == 2 )
cout << "O O # #" << endl;
else if(rightnumber == 4 && rightpostion == 1 )
cout << "O # # #" << endl;
else if(rightnumber == 4 && rightpostion == 0 )
cout << "# # # #" << endl;
else if(rightnumber == 3 && rightpostion == 3 )
cout << "O O O " << endl;
else if(rightnumber == 3 && rightpostion == 2 )
cout << "O O # " << endl;
else if(rightnumber == 3 && rightpostion == 1 )
cout << "O # # " << endl;
else if(rightnumber == 3 && rightpostion == 0 )
cout << "# # # " << endl;
else if(rightnumber == 2 && rightpostion == 2 )
cout << "O O " << endl;
else if(rightnumber == 2 && rightpostion == 1 )
cout << "O # " << endl;
else if(rightnumber == 2 && rightpostion == 0 )
cout << "# # " << endl;
else if(rightnumber == 1 && rightpostion == 1 )
cout << "O " << endl;
else if(rightnumber == 1 && rightpostion == 1 )
cout << "# " << endl;
else if(rightnumber == 0 && rightpostion == 0 )
cout << " " << endl;
cout << "--------------------------------------------" <<endl;
}
}
}while((number[0] == input[0] && number[1] == input[1] && number[2] == input[2] && number[3] == input[3])==false);
cout << "--------------------------------------------" << endl;
cout << "Congratulations! You win the game in " << roundN - 1<<"steps" << endl;
// Hold the command window
system("pause");
return 0;
}