I added a Lives counter to TripleX

Here’s my code

#include <iostream>
#include <ctime>

int Lives = 3;

void PrintIntroduction(int Difficulty)
{
    // Prints welcome messages to the terminal
    std::cout << " _____    _       _     __   __\n";
    std::cout << "|_   _|  (_)     | |    \\ \\ / /\n";
    std::cout << "  | |_ __ _ _ __ | | ___ \\ V /\n";
    std::cout << "  | | '__| | '_ \\| |/ _ \\/   \\\n";
    std::cout << "  | | |  | | |_) | |  __/ /^\\ \\\n";
    std::cout << "  \\_/_|  |_| .__/|_|\\___\\/   \\/\n";
    std::cout << "           | |                 \n";
    std::cout << "           |_|                 ";
    std::cout << "\n\n===========================================================\n";
    std::cout << "You are a secret agent breaking into a LEVEL " << Difficulty << " secure server room\n"; 
    std::cout << "You need to enter the correct codes to continue...\n";
    std::cout << "===========================================================\n";
    
}

bool bPlayGame(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;  

    // Prints sum and welcome to the terminal
    
    std::cout << "Attempts Left: " << Lives << std::endl;
    std::cout << "+ There are 3 numbers in the code";
    std::cout << "\n+ The codes add-up to: " << CodeSum;
    std::cout << "\n+ The numbers multiply to give: " << CodeProduct << std::endl;

    // Stores player's guess
    int GuessA, GuessB, GuessC; 
    std::cin >> GuessA >> GuessB >> GuessC;

    int GuessSum = GuessA + GuessB + GuessC;
    int GuessProduct = GuessA * GuessB * GuessC;  

    // Checks if the player's guess is correct
    if (GuessSum == CodeSum && GuessProduct == CodeProduct) 
    {   
        std::cout << "\n== Well done agent! You have extracted a file! Keep going! ==\n";
        return true;
    }

    else
    {
        std::cout << "\n== You entered the wrong code! Careful agent! Try again ==\n";
        Lives = Lives - 1;
        return false;
    }

    
}

int main()
{
    srand(time(NULL));

    int LevelDifficulty = 1;
    const int MaxDifficulty = 5;
    
        while (LevelDifficulty <= MaxDifficulty) 
        {
            if (Lives >= 0)
            {
                bool bLevelComplete = bPlayGame(LevelDifficulty);
                std::cin.clear();
                std::cin.ignore();
            
                if (bLevelComplete == true)
                {                
                    ++LevelDifficulty;
                } 
                
                
            }

            else
            {
                std::cout << "\n======== Game Over ========"; 
                break;
            }
            
        }   

        if (Lives > 0)
        {
        std::cout << "\n== Well done agent, you have extracted all of the files! Now, get out of there! ==";
        }

    return 0;
    
}```
3 Likes

How did you figure out how to make lives?

1 Like

Kevin, I’m not really good at explaining things in general but I’ll do my best.
Lives are just a number, right?
So, I made an int variable outside of the functions so I could use the value of that variable in multiple places.
Then I just did operations with said variable, every time the player gets a wrong answer it decreases the lives by one.
I then changed the WHILE in the main function so now there’s an IF that checks if the lives are greater than zero and if it isn’t, it sends a game over message and ends the game, and after the WHILE there’s an IF that checks if the lives are greater than zero and if they are it sends the game end message.

I hope I answered your question, if not please tell me.

1 Like

It’s all good man! I have a hard time explaining stuff too. It all seems like it makes sense! Great job figuring all that out! Also great job explaining it! :clap:

1 Like

Thanks. :smiley:

1 Like

Anytime :+1:

Another approach would be to alter the WHILE loop to check for the number of lives instead of using an IF statement in the loop.

while (LevelDifficulty <= MaxDifficulty && Lives>0) // checks level and number of lives
{
DoGameStuff();
}

if (LevelDifficulty>MaxDifficuty)//while loop is only broken when they win or run out of lives
{
YouWin();
}
else
{
YouLose();
}

Looks great! Showing the number of lives each time was useful. I made lives in my version, but they reset after you complete a level. I assumed yours worked that way for some reason, and displaying the lives counter helped clarify. Keep it up!

Privacy & Terms