So I am making the game called mastermind. It's simple you have the computer create a code that the player doesn't know using n values with a range of n. Ex: computer randomly generates the series of 5 numbers between the range(1-5). Now the player is asked to input 5 numbers in the same range and submits it. Then the computer returns a feedback to indicate if any of the values are either 1: both in the same position and the values are both equal to each other. 2: both playerstr and compstr contain a value that is the same but in the wrong position. 3: the playerstr does not contain a value also stored in the compstr. This is what my for loop is doing....atleast I think I can't get it to run correctly and return the right values stored in a char[] thats converted to a string later on.
The loop is suppose to go through each position and figure out if a value matches and what feedback should be stored in the char[]. I hope this question makes sense. What is causing my char[] to be reset to default values and returned as default when I initialize new values in the loops?
char temp[5] = { 'x', 'x', 'x', 'x', 'x' };
//checks for win
if (comp.a == player.a)
{
a = true;
}
//compare loop start
for (int i = 0; i < 5; i++)
{
if (comp.a[i] == player.a[i] && temp[i] != 'x')//checks to see if value is correct at right position
{
temp[i] = '1';
}
for (int j = 0; j < 5; j++)
{
if (comp.a[i] == player.a[j] && temp[i] != 'x')
{
temp[i] = '2';
}
for (int y = 0; y < 5; y++)//checks for wrong value
{
if (comp.a[i] == player.a[y] && temp[i] != 'x' && y == 4) // y = 4 prevents it from initializing till the whole string has been compared.
{
temp[i] = '0';
}
}
}
}
//end compare loop
won.a = string(temp, 5);
cout << won.a << endl;
system("PAUSE");Put the code you need help with here.