Issues with Mastermind Program

I've been staring at this program for too many hours, and I'm at a complete standstill. I don't have the slightest idea when it comes to getting the hint system to work. The game is the exact same as the physical one. One icon for right color in right spot(X), another icon for right color in the wrong spot(O), a third for completely wrong(-). Both the computer-generated code, and the user input are inputted as capital letters, with no space. (Ex: ROPG). The 6 colors you can choose from are Red, Green, Blue, Orange, Yellow, and Purple. On request, I can provide the entire .cpp file, as this is just a subfunction of it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void giveHint(string code, string input)//Hints player at what the code is, based on their input
{
	string chunk = "";
	cout << "Your Hint: ";
	for(int x = 0; x < code.length()-1; x++)
	{
		chunk = code.substr(x,1);
		if(code.substr(x,1) == input.substr(x,1))
			cout << "X ";
		else if(input.find(chunk,x) != -1)
			cout << "O ";
		else
			cout << "- ";
	}
	cout << endl;
	
}//end of giveHint function 

As an example of what happens, the computer-generated code is OYGY. I give it OGYY, and it should have given me X O O X, but it gives me X O - X.
Last edited on
@BigIsBig453

Your first problem in the code, is you're only checking the accuracy of the mastermind input once. Here's how I checked the coding in program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

// Create two variables at start of function, int right and int close;
//Determine the number of right colours in the right place
for (int x = 0; x<4; x++)// Change the for loops to length of your code numbers
	{
	if (Code[x] == peg[x])
	{
		right++; // Increase each time right color in right place
		peg[x] = "X"; // Remove peg color so it's not looked at again
	}
}

//Determine the number of right colors in the wrong place
for (int x = 0; x<4; x++)// Change the for loops to length of your code numbers
{
	for (int y = 0; y<4; y++)// Change the for loops to length of your code numbers
	{
		if (Code[x] == peg[y])
		{
			close++; // Increase if color found
			peg[y] = "X"; // Remove peg color so it's only picked once
		}
	}
}

if (right == 4) // If right is 4, all guesses were correct
{
		cout << "\n\nCongratulations! You broke the code."<< endl;
//Etc. Rest of your program below.	 


Hope you find this helpful.
Last edited on
You were very close... a few things:
instead of substr, just reference the character in the array... second, you are check for < 4-1... so 0->2, this should be for (int x= 0; x < code.length(); x++)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void giveHint(string code, string input)//Hints player at what the code is, based on their input
{
	cout << "Your Hint: ";
	for (int x = 0; x < code.length(); x++)
	{
		if (code[x] == input[x])
			cout << "X ";
		else if (input.find(code[x]) != -1)
			cout << "O ";
		else
			cout << "- ";
	}
	cout << endl;

}//end of giveHint function  


Here is a working version with said changes! I would probably check that the string lengths match before doing much as well, otherwise it can cause a problem if x is out of range of the string!
These are all very nice, and thank you for your inputs, but both of those answers use arrays. Both the code and the user's input are just a single string.
@BigIsBig453

The answers we gave work for strings or arrays. Checking the string uses the same procedures. You can print out specific parts of a string by cout << input[x] in the same way you cout the parts of an array. Before writing it off, at least try the methods. Or, post the entire code, and I'll get it to work as I've described.
One change to make: you should probably sort the resulting hint string when you're done. Otherwise it tells the user not only how many letters are correct and/or in the right place, but which ones.
Actually, it's better just to use the two variables, close and right. You just cout that x amount (right variable) were the right color in right positions and y amount ( close variable) were right color but in wrong place.
Last edited on
As mentioned, it is perfectly acceptable to access a string by index just like a character array. Notice my code uses a string as a parameter to the function! I do agree you may want to present it to the user in another fashion as to not give away the answer to easily, but seeing how simple it is to check the string was the part I was trying to show.
@dhayden That's the plan. I'm probably going to modify the code to put the Xs, Os, and -s into a char array, instead of displaying them directly, and then use a bubble sort algorithm to make it less obvious.

@Ready4Droid @whitenite1 I did not realize that you could use strings in that way. That subfunction now works perfectly. I'm probably going to setup a small bubble sort algorithm to get the hint to display in a less obvious way.

Everyone, thanks for your input.
Whitenite1's approach is better (and a lot easier) than sorting.
Topic archived. No new replies allowed.