예제 #1
0
// Compares guess to secret code and returns the number of correct digits
// in the incorrect locations.
int Code::checkIncorrect(Code& guess) {
    const int secretCodeLength = getLength();
    int count = 0; // correct digits in the incorrect location
	vector<bool> secretUsed = getUsed();
	vector<bool> guessUsed = guess.getUsed();
    
    /// check vector lengths
	if (guess.getLength() != secretCodeLength) {
		throw InvalidVectSize("Code::checkCorrect - vectors are not the same length!");
	}

	// compare all unused guess values to current unused secret code value
	for (int i = 0; i < secretCodeLength; i++) {
		for (int j = 0; j < guess.getLength(); j++) {
			if ((!secretUsed[i] && !guessUsed[j]) &&
				(getCode()[i] == guess.getCode()[j] )) {
				// we have a match, mark these indices as used and 
				// increase the count before continuing the compare
				secretUsed[i] = true;
				guessUsed[j] = true;
				count++;
				break;
			}
		}
	}
    return count;
}