I am new to C++ and am having trouble with an assignment. I cannot figure out the issue I am having with the double white pegs being counted. Can someone please help? I can understand that I have the issue of double counting because I am only using "or" statements, but other than that I believe I need to subtract the black pegs from the white pegs but I do not know how to do that.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
char colors[4];
srand(time(0));
int randomint = (rand() % 5) + 1;
for (int i = 0;i<4;i++) {
randomint = (rand() % 5) + 1;
switch (randomint) {
case 1:
colors[i] = 'R';
break;
case 2:
colors[i] = 'B';
break;
case 3:
colors[i] = 'Y';
break;
case 4:
colors[i] = 'P';
break;
case 5:
colors[i] = 'G';
break;
case 6:
colors[i] = 'Bl';
break;
case 7:
colors[i] = 'R';
break;
case 8:
colors[i] = 'O';
break;
case 9:
colors[i] = 'T';
break;
}
}
char usercolors[4];
cout << "We have our colors!" << endl;
cout << endl << endl;
int turncounter = 0;
while (turncounter != 12) {
turncounter++;
cout << "Current try: " << turncounter << endl;
for (int i = 0;i<4;i++) {
cout << "Color " << i << ": ";
cin >> usercolors[i];
cout << endl;
}
for (int i = 0;i<4;i++) {
if (usercolors[i] == colors[i])
cout << "Black Peg" << " ";
}
if (usercolors[0] == colors[1] ||
usercolors[0] == colors[2] ||
usercolors[0] == colors[3]) {
cout << "White Peg" << " ";
}
if (usercolors[1] == colors[0] ||
usercolors[1] == colors[2] ||
usercolors[1] == colors[3]) {
cout << "White Peg" << " ";
}
if (usercolors[2] == colors[0] ||
usercolors[2] == colors[1] ||
usercolors[2] == colors[3]) {
cout << "White Peg" << " ";
}
if (usercolors[3] == colors[0] ||
usercolors[3] == colors[1] ||
usercolors[3] == colors[2])
{
cout << "White Peg" << " ";
}
cout << endl << endl;
if (usercolors[0] == colors[0] &&
usercolors[1] == colors[1] &&
usercolors[2] == colors[2] &&
usercolors[3] == colors[3])
{
cout << "You win! Number of tries: " << turncounter << endl;
turncounter = 12;
}
else {
cout << "Try Again!" << endl << endl;
}
}
if (turncounter == 12) {
cout << "Sorry, you are incorrect!" << endl;
cout << "Answer: ";
cout << "Color 1: " << colors[0] << "\t" << "Color 2: " << colors[1] << "\t" << "Color 3: " << colors[2] << "\t" << "Color 4: " << colors[3] << "\t" << endl;
}
cin.get();
cin.get();
return 0;
}