Triple x With a life count down

Hey there, I just completed the Triple X section of the Unreal / C++ courses.

I added a few tweaks of my own to add a bit of color to the game, mainly, a life total that decreases after each wrong answer, and a few more else if statement for the game to act differently if the player just beat the game or died.

Please tell me how I did and how I could improve my code!

#include <iostream> 
#include <ctime>

void PrintIntroduction(int Difficulty, int NumberOfLives)
{
    std::cout << "\nYou are now standing in the chamber #" << Difficulty << ". The Sphinx appears before you and speak in a belowing voice:\n";
    std::cout << "YOU NEED TO FIND THE 3 NUMBERS THAT VERIFY THE FOLLOWING RULES!\nYOU HAVE " << NumberOfLives << " ATTEMPTS LEFT!\n";

}

bool PlayGame(int Difficulty, int NumberOfLives)
{
    PrintIntroduction(Difficulty, NumberOfLives);
    
    const int CodeA = rand() % (Difficulty * 2)+1;
    const int CodeB = rand() % (Difficulty * 2)+1;
    const int CodeC = rand() % (Difficulty * 2)+1;

    const int CodeSum = CodeA + CodeB + CodeC;
    const int CodeProduct = CodeA * CodeB * CodeC;

    
    std::cout << "\n-The 3 numbers add-up to: " << CodeSum;
    std::cout << "\n-The product of the 3 numbers is: " << CodeProduct;
    
    // Store player's guess
    int GuessA, GuessB, GuessC;
    std::cout << std::endl;
    std::cout << std::endl;
    std::cin >> GuessA >> GuessB >> GuessC;

    //Calculate the player's sum and product
    int GuessSum = GuessA + GuessB + GuessC;
    int GuessProduct = GuessA * GuessB * GuessC;

    //Check if player's guess is correct
    if (GuessSum == CodeSum && GuessProduct == CodeProduct && Difficulty != 10)
    {
        std::cout << "\nGood job, you found the right numbers, you move toward the next chamber!\n\n";

        return true;
    }
    else if (GuessSum == CodeSum && GuessProduct == CodeProduct && Difficulty == 10)
    {
        return true;
    }
    else if (NumberOfLives ==1)
    {
        return false;
    }
    else
    {
        std::cout << "\nThe sphinx shakes it's head gravely, you get to try again!\n\n";

        return false;
    }
}


int main() 
{   
    srand(time(NULL));
    int LevelDifficulty = 1;
    int const MaxDifficulty = 10;
    int NumberOfLives = 3;

    std::cout << "\nAfter a week long journey through the desert, you finally arrives at the Sphinx's lair. All it's riches will be yours if you answer it's riddle correctly.\n";

    while (LevelDifficulty <= MaxDifficulty && NumberOfLives > 0) // Loop the ga,e until all levels are completed
    {
        bool bLevelComplete = PlayGame(LevelDifficulty, NumberOfLives);

        std::cin.clear(); //Clears any errors in cin
        std::cin.ignore(); //Discards the buffer of cin

        if (bLevelComplete)
        {
            ++LevelDifficulty;
        }
        else        
        {
            --NumberOfLives;
        }
        
    }
    if (LevelDifficulty == 11)
    {
        std::cout << "\nCongratulations, you answered all of the sphinx's riddles correctly, you step forward in the final chamber to gather it's riches!";
    }
    else if (NumberOfLives == 0)
    {
        std::cout << "The sphinx jumps on you and devours you whole! No one will ever know you were there...";
    }
    
    
    return 0;
}
1 Like

Awesome job! :smiley:

Privacy & Terms