Here is my programm so far:

#include <iostream>
#include <ctime>


void PrintIntroduction(int Difficulty) {
    std::cout<< "\n\nYou are locked into a room with a captivated cobra!\n";
    if (Difficulty > 1) 
    {
        std::cout<< "This prblem is going to be harder than the last, you are on level " << Difficulty << std::endl;
    }
    std::cout<< "You have to enter the correct numbers, otherwise the cobra will be released and poison you to death... \n";

}

bool PlayGame(int Difficulty)
{
    PrintIntroduction(Difficulty);

    const int CodeA = rand() % Difficulty + Difficulty;
    const int CodeB = rand() % Difficulty + Difficulty;
    const int CodeC = rand() % Difficulty + Difficulty;    

    const int CodeSum = CodeA + CodeB + CodeC;
    const int CodeProduct = CodeA * CodeB * CodeC;
    
    //Print sum and product to the terminal
    std::cout<<"\n+ You have to enter 3 numbers";
    std::cout<< "\n+ The numbers add up to: " << CodeSum;
    std::cout<< "\n+ The numbers multiply to give: " << CodeProduct << std::endl;

    int GuessA;
    int GuessB;
    int GuessC;
    
    // Store player guess
    std::cin >>GuessA;
    std::cin >> GuessB;
    std::cin >> GuessC;

    int GuessSum = GuessA + GuessB + GuessC;
    int GuessProduct = GuessA * GuessB * GuessC;
    
    //check, if the player is correct
    if (GuessSum == CodeSum && GuessProduct == CodeProduct) 
    {
        std::cout << "\n*** You enterd the coorect code! The cobra is kept inside, but it is getting hungry! ***\n\n";
        return true;
    }
    else 
    {
        std::cout << "\n*** Oh no, you entred the wrong code! The cobra is released and bytes you to death! ***n\n";
        return false;
    }
}

int main() 
{
    srand(time(NULL)); // change the seed based on the time of day
    int LevelDifficulty = 1;
    const int MaxLevel = 5;
    bool bAlive = true;

    while (LevelDifficulty <= MaxLevel && bAlive == true) // Lopp game until all levels are completed
    {
        bool bLevelComplete = PlayGame(LevelDifficulty);
        std::cin.clear();
        std::cin.ignore();

        if (bLevelComplete)
        {
            ++LevelDifficulty;
        }
        else 
        {
            bAlive = false;
        }
        
    }
    if (bAlive == true)
    {
        std::cout<<"\n*** Wow! You answerd everything correctly! ***\n";
        std::cout << "*** A gate opens slowly and you can now walk out! ***\n";
        std::cout <<"*** Congratulations! ***";
    }
    return 0;
}

Privacy & Terms