/* CS102 -- Jim Plank */ /* This program does a "master mind"-like program. It assumes that the user will enter a three-digit number, where all three digits are different. It then compares it to a known number (567), and if the user's guess is not correct, it tells the user how many digits are correct, and how many digits are correct and in the correct position. Note the use of integer division, and the mod operator to grab the right (ar), middle (am) and left (al) digits of the answer, and the guess (dr, dm and dl). */ #include using namespace std; main() { int answer = 567; int guess; int number; int right; int dr, dm, dl; int ar, am, al; int correct_number; int correct_position; ar = answer % 10; al = answer / 100; am = (answer / 10) % 10; number = 1; right = 0; while (!right) { cout << "Guess #" << number << ": Enter a number between 0 and 999: "; cin >> guess; if (guess >= 0 && guess <= 999) { if (guess == answer) { cout << "Right! You took " << number << " move"; if (number != 1) cout << "s" ; cout << "." << endl; right = 1; } else { dr = guess % 10; dl = guess / 100; dm = (guess / 10) % 10; correct_position = 0; if (dr == ar) correct_position++; if (dl == al) correct_position++; if (dm == am) correct_position++; correct_number = 0; if (dr == ar || dr == am || dr == al) correct_number++; if (dm == ar || dm == am || dm == al) correct_number++; if (dl == ar || dl == am || dl == al) correct_number++; cout << "Correct position: " << correct_position << endl; cout << "Correct number: " << correct_number << endl; } } else { cout << "Between 1 and 999, please.\n"; } number++; } return 0; }