// input: // code guess - a code object containing the user's guess // output: // int correct - the number of digits in the guess that are correctly placed to the secret // // Check how many digits of a guess are correctly placed compared to the secret code. When a correct digit is found // the corresponding digit in the USER'S GUESS is set to 6 since 6 is above the possible range. This indicates that // the index has been covered. The secretDigit value at the same index is also set to 6 since secretDigit represents the // remaining part of the secret to be guessed. // ASSUMPTION: This is called BEFORE checkIncorrect so that it may update the guess with correctly guessed digits int code::checkCorrect(code guess) { vector<int> userGuess = guess.getGuess(); vector<int> secretCode = getSecret(); vector<int> secretDigits = getDigits(); int correct = 0; for (int i=0; i < userGuess.size(); i++) { //Correct digit placement found if (userGuess[i] == secretDigits[i] && secretDigits[i] != 6) { userGuess[i] = 6; secretDigits[i] = 6; correct += 1; } } setGuess(userGuess); setDigits(secretDigits); return correct; }
// input: // code guess - a code object containing the user's guess // output: // int incorrect - the number of digits in the guess that exist in the secret, but are not correctly placed // // Check how many digits are part of the secret code, but were guessed at wrong positions. If an index is set to 6 // we know that the index has been covered and will skip it. If an element in the same spot in secretDigits is set to 6, // this also means that the value has been covered. // ASSUMPTION: This is called AFTER checkIncorrect so that it will only check indices that haven't been confirmed int code::checkIncorrect(code guess) { vector<int> userGuess = guess.getGuess(); vector<int> secretCode = guess.getSecret(); vector<int> secretDigits = getDigits(); int incorrect = 0; for (int i=0; i < userGuess.size(); i++) { //Index has already been confirmed, move on if (userGuess[i] == 6) { continue; } //Digit IS part of solution, HAS NOT been verified by checkCorrect, but in wrong place if (find(secretDigits.begin(), secretDigits.end(), userGuess[i]) != secretDigits.end() && userGuess[i] != secretDigits[i] && secretDigits[i] != 6) { incorrect += 1; userGuess[i] = 6; secretDigits[distance( secretDigits.begin(), find(secretDigits.begin(), secretDigits.end(), userGuess[i]))] = 6; } } setDigits(secretDigits); return incorrect; }