tripleXGame remastered B)

Logics appended as …

  1. Level increases only if you cross the current level.
  2. Tracking the termination of the program if final level isn’t reached.
  3. 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;

}
1 Like

Incredible code! Great job with this section! Congrats on your game :partying_face:

1 Like

Thank you so much for the encouragement … I found that much of my code was discussed at later point … that made me feel nice that my thought process was in line with that of the mentor … :slight_smile:

Privacy & Terms