From b5adfe9750bb325a553a55dacfc56ddae0d9a822 Mon Sep 17 00:00:00 2001 From: Sebastian Brosch Date: Wed, 13 Nov 2024 23:21:40 +0100 Subject: [PATCH] added solution for simple mastermind --- VL03-04-02/main.cc | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 VL03-04-02/main.cc diff --git a/VL03-04-02/main.cc b/VL03-04-02/main.cc new file mode 100644 index 0000000..9830f2e --- /dev/null +++ b/VL03-04-02/main.cc @@ -0,0 +1,58 @@ +#include + +int main() { + std::string coder = ""; + std::string encoder = ""; + const int max_attempts = 8; + int attempts = max_attempts; + + std::cout << "~~ Mastermind ~~" << std::endl; + std::cout << std::endl; + std::cout << "Konfiguration:" << std::endl; + std::cout << "\tFarben: 8 (1 bis 8)" << std::endl; + std::cout << "\tCodelänge: 4" << std::endl; + std::cout << "\tMehrfache Farben: Nein" << std::endl; + std::cout << "\tVersuche: " << max_attempts << std::endl; + std::cout << std::endl; + + std::cout << "Kodierer: "; + std::cin >> coder; + + for (int i = 0; i < 10; i++) { + std::cout << std::endl; + } + + while (encoder != coder && attempts > 0) { + int current_attempt = (max_attempts - attempts) + 1; + std::cout << "Versuch " << current_attempt << " von " << max_attempts << ": "; + std::cin >> encoder; + + std::string feedback = ""; + int black = 0; + int white = 0; + + for (int i = 0; i < coder.length(); i++) { + if (encoder[i] == coder[i]) { + black++; + } else { + if (coder.find(encoder[i]) != std::string::npos) { + white++; + } + } + } + + attempts -= 1; + + if (encoder == coder) { + std::cout << std::endl << "Du hast gewonnen!" << std::endl; + } else { + std::cout << "Feedback: " << std::string(black, 'B') << std::string(white, 'W') << std::endl; + + if (current_attempt == max_attempts) { + std::cout << std::endl << "Du hast keine Versuche mehr!" << std::endl; + } + } + } + + return 0; +} \ No newline at end of file