added solution for simple mastermind

This commit is contained in:
Sebastian Brosch 2024-11-13 23:21:40 +01:00
parent ea225bcc19
commit b5adfe9750
Signed by: sebastianbrosch
GPG Key ID: 66E3143B54A6CF10

58
VL03-04-02/main.cc Normal file
View File

@ -0,0 +1,58 @@
#include <iostream>
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;
}