Compare commits
8 Commits
709475cfc3
...
f37af51671
Author | SHA1 | Date | |
---|---|---|---|
|
f37af51671 | ||
|
df6e679963 | ||
|
253a2daeac | ||
|
c38e15ded6 | ||
|
f5ce0edf4c | ||
|
1d32b4798d | ||
|
90b0e5bd19 | ||
|
b555d3d5c8 |
@ -5,10 +5,12 @@
|
|||||||
#include "Aufg2/GuestList.h"
|
#include "Aufg2/GuestList.h"
|
||||||
#include "Aufg3/Phonebook.h"
|
#include "Aufg3/Phonebook.h"
|
||||||
#include "Aufg4/CrimeStats.h"
|
#include "Aufg4/CrimeStats.h"
|
||||||
|
#include "Aufg5/Mastermind.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// Aufg1Main();
|
// Aufg1Main();
|
||||||
// Aufg2Main();
|
// Aufg2Main();
|
||||||
// Aufg3Main();
|
// Aufg3Main();
|
||||||
Aufg4Main();
|
// Aufg4Main();
|
||||||
|
Aufg5Main();
|
||||||
}
|
}
|
140
Aufg5/Mastermind.cpp
Normal file
140
Aufg5/Mastermind.cpp
Normal 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
22
Aufg5/Mastermind.h
Normal 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
|
Loading…
Reference in New Issue
Block a user