Implement Counting Matches

This commit is contained in:
Matti 2024-11-13 21:52:30 +01:00
parent f5ce0edf4c
commit c38e15ded6

View File

@ -47,7 +47,7 @@ std::vector<std::string> getCodeGuessFromConsole() {
// TODO Make limit dynamic // TODO Make limit dynamic
for (int i = 1; i < 5; ++i) { for (int i = 1; i < 5; ++i) {
std::cout << "Enter Digit " << i << " : " << std::endl; // std::cout << "Enter Digit " << i << " : " << std::endl;
std::string input; std::string input;
std::cin >> input; std::cin >> input;
@ -78,6 +78,28 @@ int countPefectMatches(std::vector<std::string> &v1, std::vector<std::string> &v
return count; return count;
} }
int countMatches(std::vector<std::string> &v1, std::vector<std::string> &v2) {
int shorterSize;
if (v1.size() < v2.size()) {
shorterSize = v1.size();
} else {
shorterSize = v2.size();
}
int count = 0;
for (int i = 0; i < shorterSize; i++) {
for (int j = 0; j < shorterSize; j++) {
if (v1[i] == v2[j]) {
count++;
break;
}
}
}
return count;
}
@ -92,6 +114,7 @@ void Aufg5Main() {
printAllStringsInVector(SecretCode); printAllStringsInVector(SecretCode);
std::vector<std::string> guess = getCodeGuessFromConsole(); std::vector<std::string> guess = getCodeGuessFromConsole();
std::cout << "correct were: " << countPefectMatches(guess, SecretCode) << std::endl; std::cout << "correct were: " << countMatches(guess, SecretCode) << std::endl;
std::cout << "perfect were: " << countPefectMatches(guess, SecretCode) << std::endl;
} }