Compare commits

...

8 Commits

Author SHA1 Message Date
Matti
f37af51671 Bugfix, 0 correct doesnt end the game 2024-11-13 22:49:44 +01:00
Matti
df6e679963 Feature Complete? 2024-11-13 22:37:30 +01:00
Matti
253a2daeac Improve Match Counting 2024-11-13 22:18:09 +01:00
Matti
c38e15ded6 Implement Counting Matches 2024-11-13 21:52:30 +01:00
Matti
f5ce0edf4c Implement Counting perfect Matches 2024-11-13 21:29:55 +01:00
Matti
1d32b4798d Implement Getting Code from Console 2024-11-13 21:23:01 +01:00
Matti
90b0e5bd19 Add v1 of Console Input Method 2024-11-13 21:11:45 +01:00
Matti
b555d3d5c8 Basic Layout and implementations of Mastermind Helper Methods 2024-11-13 21:05:16 +01:00
3 changed files with 166 additions and 2 deletions

View File

@ -5,10 +5,12 @@
#include "Aufg2/GuestList.h"
#include "Aufg3/Phonebook.h"
#include "Aufg4/CrimeStats.h"
#include "Aufg5/Mastermind.h"
int main() {
// Aufg1Main();
// Aufg2Main();
// Aufg3Main();
Aufg4Main();
}
// Aufg4Main();
Aufg5Main();
}

140
Aufg5/Mastermind.cpp Normal file
View File

@ -0,0 +1,140 @@
//
// 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));
// std::cout << "Secret Code was set as " << digit << std::endl;
}
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;
// TODO Make limit dynamic
for (int i = 1; i <= codeLength; ++i) {
// std::cout << "Enter Digit " << i << " : " << std::endl;
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 = 99;
int codeLength = 1;
int livesRemaining = 2;
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;
// printAllStringsInVector(SecretCode);
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 ㄟ(≧◇≦)ㄏ";
}

22
Aufg5/Mastermind.h Normal file
View File

@ -0,0 +1,22 @@
//
// Created by DH10MBO on 13.11.2024.
//
#ifndef MASTERMIND_H
#define MASTERMIND_H
#include <string>
#include <vector>
void Aufg5Main();
std::vector<std::string> generateSecretCode(int size, int optionsPerDigit);
std::vector<std::string> getCodeGuessFromConsole();
std::vector<std::string> convertStringToGuess(std::string &guessAsString);
int countMatches(std::vector<std::string> &v1, std::vector<std::string> &v2);
int countPefectMatches(std::vector<std::string> &v1, std::vector<std::string> &v2);
#endif //MASTERMIND_H