I upgraded my TripleX game. Now the player have 3 chances!

Hello.
I upgraded my TripleX game. Now the player have 3 chances. Is the chances are equal to 0 it break the game and print “You died!”.

#include <iostream>
#include <ctime>

void PrintIntroduction(int Difficulty, int Health)
{
    //Print welcome message to the terminal
    std::cout << "Your are a secret agent on Level: " << Difficulty;
    std::cout << std::endl;
    std::cout << "Your health: " << Health;
    std::cout << std::endl;
    std::cout << "You have to enter the secret code!\nWrite the secret code: ";
}

bool PlayGame(int Difficulty, int Health)
{

    PrintIntroduction(Difficulty, Health);

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

    const int CodeSum = CodeA + CodeB + CodeC; // adding up a,b,c
    const int CodeMultiply = CodeA * CodeB * CodeC; // multiply a, b, c

    //Print CodeSum and CodeMultiply
    std::cout << std::endl;
    std::cout << "\n+ There are 3 numbers in the code";
    std::cout << "\n+ The code add-up to: " << CodeSum;
    std::cout << "\n+ The codes CodeMultiply to give: " << CodeMultiply << std::endl;
    
    std::cout << "\n";

    //Player inputs numbers
    int GuessA, GuessB, GuessC;
    std::cin >> GuessA >> GuessB >> GuessC;
    
    int GuessSum = GuessSum = GuessA + GuessB + GuessC;
    int GuessMultiply = GuessMultiply = GuessA * GuessB * GuessC;
    
    //check if the player guess the right numbers
    if (GuessSum == CodeSum && GuessMultiply == CodeMultiply)
    {
        std::cout << "\n** You picked right numbers **\n" << std::endl;
        return true;
    }
    else {
        std::cout << "\n** Wrong numbers. Try again **" << std::endl;
        return false;
    }
}


int main()
{
    srand(time(NULL)); // create new squences depend on time

    int LevelDifficulty = 1;
    const int MaxLevel = 2;
    int PlayerLife = 3; // life

    while(LevelDifficulty <= MaxLevel) // Loop game until all levels are completed
    {
        bool bLevelComplete = PlayGame(LevelDifficulty, PlayerLife);
        std::cin.clear(); //Clears any error
        std::cin.ignore();  //Discards the buffer

        if (bLevelComplete)
        {
            ++LevelDifficulty;
        }  else {
            --PlayerLife;
        }

        if(PlayerLife == 0)     
        {
            std::cout << "You Died!"; // print this if player life == 0
            break;
        }   
   } 
    
    std::cout << "You Won!";
    return 0;
}
2 Likes

Awesome job giving the player 3 chances!