Logics appended as …
- Level increases only if you cross the current level.
- Tracking the termination of the program if final level isn’t reached.
- Reduced number of lines of code by using ‘\n’ instead of “std::endl” for expression statements that
prints simple string to the output stream. For me it seems efficient … if not please let me know …
That’s it.! Have a look and play the game below ::
#include <iostream>
bool playAtDifficultyLvl(int difficultyLvl) {
std::cout << "\nGaining access to Google DataCenter !! " << difficultyLvl << "0 % ....\n";
std::cout << "\nTo reach 100 % help me with three numbers that meets below conditions ...\n\n";
const int _1stNum = rand() % difficultyLvl + difficultyLvl;
const int _2ndNum = rand() % difficultyLvl + difficultyLvl;
const int _3rdNum = rand() % difficultyLvl + difficultyLvl;
const int sum = _1stNum + _2ndNum + _3rdNum;
const int product = _1stNum * _2ndNum * _3rdNum;
std::cout << " * Product should be : " << product << std::endl;
std::cout << " + Sum should be : " << sum << std::endl;
std::cout << "\n Please provide 3 space separated numbers followed by any letter :: \n\n";
int answer;
int trialSum = 0;
int trialProduct = 1;
while (std::cin >> answer) {
trialSum += answer;
trialProduct *= answer;
}
if (trialSum == sum && trialProduct == product) {
if (difficultyLvl == 10) {
std::cout << "\n\n Google's Data is yours!! Start Pulling INN ..!!";
} else{
std::cout << "\n\n Breached Level " << difficultyLvl << ".! Sneaking into Level " << (difficultyLvl + 1) << std::endl;
}
return true;
} else {
if ((difficultyLvl < 10) && (answer > 0 || answer < 0)) {
std::cout << "\n\n Interruption detected... !! Terminating !!" << std::endl;
} else {
std::cout << "\n\n Boooo... !! Gotta Retry !!" << std::endl;
}
return false;
}
};
int main(){
int difficultyLvl = 2;
int maxDifficultyLvl = 10;
while (difficultyLvl <= maxDifficultyLvl)
{
bool isLevelCrossed = playAtDifficultyLvl(difficultyLvl);
std::cin.clear();
std::cin.ignore();
if (isLevelCrossed) ++difficultyLvl;
}
return 0;
}