My final TripleX game code with additional Lives Counter

#include <iostream>
#include <ctime>

void PrintIntroduction(int Level, int Lives)
{
    std::cout << "\n\nYou were kidnapped by the local serial killer...\n";
    std::cout << "~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~\n";
    std::cout << "You're in his secret bunker four levels under the ground...\n";
    std::cout << "~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~\n";
    std::cout << "Exit is on the level 0. Good Luck!\n";
    std::cout << "~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~\n";
    std::cout << "You're on the level " << Level << ".\n";
    std::cout << "Lives left " << Lives << "\n\n";
}

bool PlayGame(int Level, int LevelDifficulty, int Lives)
{
    PrintIntroduction(Level, Lives);

    // Declare 3 number code
    const int CodeA = rand() % LevelDifficulty + LevelDifficulty;
    const int CodeB = rand() % LevelDifficulty + LevelDifficulty;
    const int CodeC = rand() % LevelDifficulty + LevelDifficulty;
    
    const int CodeSum = CodeA + CodeB + CodeC;
    const int CodeProduct = CodeA * CodeB * CodeC;

    // Print CodeSum and CodeProduct to the terminal
    std::cout << std::endl;
    std::cout << "+ There are 3 numbers in the code";
    std::cout << "\n+ The codes add-up to: " << CodeSum;
    std::cout << "\n+ The codes multiply to give: " << CodeProduct << std::endl;
    
    // Store player guess
    int GuessA, GuessB, GuessC;
    std::cin >> GuessA >> GuessB >> GuessC;

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

    // Check if the players guess is correct
    if (GuessSum == CodeSum && GuessProduct == CodeProduct) 
    {
        std::cout << "\nCode accepted!";
        return true;
    }
    else
    {
        std::cout << "\nTry Again!";
        return false;
    }
}

int main()
{
    srand(time(NULL)); // create new random sequence based on time of day
    int Level = -4;
    int LevelDifficulty = 1;
    const int MaxLevel = 0;
    int Lives = 3;

    while (Level <= MaxLevel && Lives > 0) // Loop game until all levels completed and not less than 1 life
    {
        bool bLevelComplete = PlayGame(Level, LevelDifficulty, Lives);
        std::cin.clear(); // Clears any errors
        std::cin.ignore(); // Discards the buffer

        if (bLevelComplete)
        {
            ++Level;
            ++LevelDifficulty;
        }
        else
        {
            --Lives;
        } 
    }
    
    if (Lives > 0)
    {
       std::cout << "\n\nGreat job! You escaped. Find some help before kidnapper will comeback."; 
    }
    else
    {
        std::cout << "\n\nYou had only one chance. Now, find another way out or prepare for the worst.";
    }

    return 0;
}

FInal TripleX game - Lose message

Final TripleX game - Win message

1 Like

Looks good. Thanks for sharing.

1 Like

Privacy & Terms