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;

}

Problem #1: for([B]counter>0[/B]; counter<=4; counter++) -- first field is wrong.
I also question the interpretation that the 4 numbers must be distinct. I've never seen that requirement. But you need to follow your interpretation of the problem.

And this loop:

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])
	{              // what is the purpose of this if? I don't think it's necessary
	counter2 = 0;  // changing the loop counter is a very bad practice.
	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;

	}

The IFs are too complex and prone to problems.

As for the rest of the program, you are making it hard on yourself. You're counting values but not keeping track of what you need to keep track of. And you're testing the same thing over and over and over...

You need a copy of the answer array and another array for the result information which starts with all SPACEs or DOTs.
Create the first loop to test each input with each number. This is the "right value, right position" loop. If you get a match, change the result array value to O. And when you find a match you need to remove that match from the input array and the copy of the number array.

Next loop tests the "right value, wrong position" changing the arrays accordingly. This will be 2 nested loops.


Lastly, format your code better. It's inconsistent making it hard to follow the code.

I made the change as follow and works !

#include <iostream>
#include <ctime>
using namespace std;
int main()
{
    int digit[4],input[4],temp[4];
    int checkNo=0,checkP=0,step=1;
    int a,b,c,d; 
    int m=0,n=0;


    srand( (unsigned int)time(NULL) );
    for(a=0;a<4;a++)
        digit[a] = rand() % 6+1;


    cout<<"MasterMind (For checking: "<< digit[0] <<" "<<digit[1]<<" "<<digit[2]<<" "<<digit[3]<<")"<<endl;
    cout<<endl;
    cout<<"Enter four digits (1-6) separated by a space"<<endl;
    cout<<"------------------------------------------"<<endl;



    while(checkNo < 4 && checkP < 4){
        cout<<"Round "<<step<< ":"<<endl;
        step++;
        cout<<"Enter Guess: ";
        for(b=0;b<4;b++){
            cin>>input[b];
            temp[b] = input[b];
        }
        for(c=0;c<4;c++){
            for(d=3;d>=0;d--){
                if(temp[c] == digit[d])
                    checkNo++;
                if(temp[c] == digit[c])
                    checkP++;
            }
        }
            while(m<checkNo){
                cout<<"O";
                m++;
            }
            while(n<checkP){
                cout<<"#";
                n++;
            }
            cout<<endl;
            cout<<"------------------------------------------"<<endl;
    }
    cout<<"------------------------------------------"<<endl;
    cout<<"Congratulations! You win the game in "<< step <<"steps";

    system("pause");
    return 0;

}

Edited 3 Years Ago by Dani: Formatting fixed

I made the change as follow and works !

1) Who are you?
2) Why are you hijacking kyky365's thread?
3) Why are you not using code tags?
4) Why are you fixing kyky365's program so he can cheat?

We don't do that here...

This article has been dead for over six months. Start a new discussion instead.