137 lines
3.8 KiB
C++
137 lines
3.8 KiB
C++
//
|
|
// Created by DH10MBO on 13.11.2024.
|
|
//
|
|
|
|
#include "Mastermind.h"
|
|
|
|
#include <iostream>
|
|
#include <ostream>
|
|
#include <random>
|
|
|
|
#include "../Aufg4/CrimeStats.h"
|
|
|
|
int getRandomNumberInRange(int min, int max) {
|
|
static std::mt19937 generator(static_cast<unsigned int>(time(nullptr)));
|
|
std::uniform_int_distribution<int> distribution(min, max);
|
|
|
|
int randomNumber = distribution(generator);
|
|
|
|
return randomNumber;
|
|
}
|
|
|
|
void printAllStringsInVector(std::vector<std::string> &strings) {
|
|
std::cout << "[";
|
|
int i;
|
|
for (i = 0; i < strings.size() - 1; i++) {
|
|
std::cout << strings[i] << ", ";
|
|
}
|
|
std::cout << strings[i] << "]" << std::endl;
|
|
}
|
|
|
|
std::vector<std::string> generateSecretCode(int digits, int optionsPerDigit) {
|
|
std::vector<std::string> secretCode;
|
|
|
|
for (int i = 0; i < digits; i++) {
|
|
int digit = getRandomNumberInRange(1, optionsPerDigit);
|
|
secretCode.push_back(std::to_string(digit));
|
|
}
|
|
|
|
return secretCode;
|
|
}
|
|
|
|
std::vector<std::string> getCodeGuessFromConsole(int codeLength, int livesRemaining) {
|
|
std::cout << "Have a Guess!" << std::endl;
|
|
std::cout << "You have " << livesRemaining << " more Attempts before you will be roasted" << std::endl;
|
|
|
|
std::vector<std::string> guess;
|
|
|
|
for (int i = 1; i <= codeLength; ++i) {
|
|
std::string input;
|
|
std::cin >> input;
|
|
|
|
guess.push_back(input);
|
|
}
|
|
std::cout << "You entered: " << std::endl;
|
|
printAllStringsInVector(guess);
|
|
|
|
return guess;
|
|
}
|
|
|
|
int countPefectMatches(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++) {
|
|
if (v1[i] == v2[i]) {
|
|
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();
|
|
}
|
|
|
|
std::vector<std::string> copyOfv2 = v2;
|
|
|
|
int count = 0;
|
|
for (int i = 0; i < shorterSize; i++) {
|
|
for (int j = 0; j < shorterSize; j++) {
|
|
if (v1[i] == copyOfv2[j]) {
|
|
count++;
|
|
copyOfv2[j] = "I have been counted already";
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return count;
|
|
}
|
|
|
|
void Aufg5Main() {
|
|
int optionsPerDigit = 4;
|
|
int codeLength = 4;
|
|
int livesRemaining = 6;
|
|
std::cout << optionsPerDigit << " options per Digit" << std::endl;
|
|
std::cout << codeLength << " Code Length" << std::endl;
|
|
std::cout << livesRemaining << " Lives Remaining" << std::endl;
|
|
std::cout << "======================================" << std::endl;
|
|
std::vector<std::string> SecretCode = generateSecretCode(codeLength, optionsPerDigit);
|
|
|
|
std::cout << "I have locked the door with a secret Code, you will never be able to figure it out!!" << std::endl;
|
|
|
|
while (livesRemaining) {
|
|
std::vector<std::string> guess = getCodeGuessFromConsole(codeLength, livesRemaining);
|
|
|
|
int perfectCounter = countPefectMatches(guess, SecretCode);
|
|
|
|
std::cout << "correct were: " << countMatches(guess, SecretCode) << std::endl;
|
|
std::cout << "perfect were: " << perfectCounter << std::endl << std::endl;
|
|
|
|
livesRemaining--;
|
|
|
|
if (perfectCounter == codeLength) {
|
|
std::cout << "No! This cant be!" << std::endl << "How did you know???" << std::endl << "What kind of Computer Nerd are you?!?";
|
|
return;
|
|
}
|
|
}
|
|
|
|
std::cout << "You Lost!" << std::endl << "My Secret Code stays a Mystery!!!" << std::endl << std::endl << "Fine, I will tell you" << std::endl;
|
|
printAllStringsInVector(SecretCode);
|
|
std::cout << "NERD ㄟ(≧◇≦)ㄏ";
|
|
|
|
}
|