#include <iostream>
#include <string>
#include <ctime>
using namespace std;
bool PlayGame(const short int& LevelDifficulty);
void PrintIntro(const short int& LevelDifficulty);
void PrintHint(const short int& CodeSum, const short int& CodeProduct);
bool CheckInput(string& Input);
int main()
{
srand(time(NULL));
short int LevelDifficulty{ 1 };
const char MaxDifficulty{ 4 };
while (LevelDifficulty <= MaxDifficulty)
{
//PlayGame();
bool bLevelComplete = PlayGame(LevelDifficulty);
//Increase difficulty or close.
if (bLevelComplete)
{
LevelDifficulty++;
cout << "\nComputer hacked!\nMoving to next level.\n\n";
}
else
{
LevelDifficulty = 1;
}
}// while
cout << "\nSYSTEM HACKED..\n";
return 0;
}
bool PlayGame(const short int& LevelDifficulty)
{
// Declate three number code.
short int ThreeNumberCode[3]{ (rand() % LevelDifficulty + LevelDifficulty),
(rand() % LevelDifficulty + LevelDifficulty), (rand() % LevelDifficulty + LevelDifficulty) };
short int CodeSum{ ThreeNumberCode[0] + ThreeNumberCode[1] + ThreeNumberCode[2] };
short int CodeProduct{ ThreeNumberCode[0] * ThreeNumberCode[1] * ThreeNumberCode[2] };
for (rsize_t i = 0; i < 3; i++)
cout << '\n' << "answer: " << ThreeNumberCode[i];
cout << '\n';
// Store players input
short int PlayersGuess[3]{};
short int PlayersGuessSum{ 0 };
short int PlayersGuessProduct{ 0 };
PrintIntro(LevelDifficulty);
PrintHint(CodeSum, CodeProduct);
string Input;
cout << "\nEnter the correct code: ";
getline(cin, Input);
if (!CheckInput(Input))
{
cout << "Input is not valid.\n";
return false;
}
else
{
char SpaceFound{ 0 };
if(Input.find(' ') != string::npos)
PlayersGuess[0] = stoi(Input.substr(0, Input.find(' ')));
SpaceFound = Input.find(' ') + 1;
if (Input.find(' ', 1) != string::npos)
PlayersGuess[1] = stoi(Input.substr(SpaceFound, Input.find(' ', 1)));
SpaceFound = Input.find(' ', SpaceFound);
PlayersGuess[2] = stoi(Input.substr(SpaceFound, Input.size() - 1));
Input.clear();
PlayersGuessSum = PlayersGuess[0] + PlayersGuess[1] + PlayersGuess[2];
PlayersGuessProduct = PlayersGuess[0] * PlayersGuess[1] * PlayersGuess[2];
// Debug answers
//cout << '\n' << "Players Guess Sum : " << PlayersGuessSum << " - Players Guess Product: " << PlayersGuessProduct << '\n';
//cout << '\n' << "Code Sum : " << CodeSum << " - Code Product: " << CodeProduct << '\n';
//cout << '\n' << PlayersGuess[0] << " " << PlayersGuess[1] << " " << PlayersGuess[2] << '\n';
if (PlayersGuessProduct == CodeProduct && PlayersGuessSum == CodeSum)
{
return true;
}
else
{
cout << "\nERROR: WRONG CODE.. RESTARING.\n\n";
} // PlayGame.. if else.. if else
return false;
}// PlayGame.. if else
}// PlayGame
void PrintIntro(const short int& LevelDifficulty)
{
if (LevelDifficulty == 1)
cout << "You are a secret agent breaking into a secure server room.\n";
cout << "Enter the correct code to continue...\n";
cout << "Level Difficulty: " << LevelDifficulty << '\n';
}
void PrintHint(const short int& CodeSum, const short int& CodeProduct)
{
cout << "There are three numbers in the code.\n";
cout << "The code add-up to: " << CodeSum << '\n';
cout << "The code multiply to get: " << CodeProduct << '\n';
}// PrintHint
bool CheckInput(string& Input)
{
int SpaceCount{ 0 };
for (int i = 0; i < Input.size(); i++)
{
if (!isdigit(Input[i]) && Input[i] != ' ')
return false;
if (Input[i] == ' ')
SpaceCount++;
}// CheckInput.. for
if (SpaceCount == 2)
return true;
else
return false;
}// CheckInput```