From c38e15ded63eb0b305fff99b184bea58b3f2e2d3 Mon Sep 17 00:00:00 2001 From: Matti Date: Wed, 13 Nov 2024 21:52:30 +0100 Subject: [PATCH] Implement Counting Matches --- Aufg5/Mastermind.cpp | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/Aufg5/Mastermind.cpp b/Aufg5/Mastermind.cpp index 2124f70..b02f85f 100644 --- a/Aufg5/Mastermind.cpp +++ b/Aufg5/Mastermind.cpp @@ -47,7 +47,7 @@ std::vector getCodeGuessFromConsole() { // TODO Make limit dynamic 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::cin >> input; @@ -78,6 +78,28 @@ int countPefectMatches(std::vector &v1, std::vector &v return count; } +int countMatches(std::vector &v1, std::vector &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); std::vector 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; }