Triple X

Visual Studio Code Terminal Snippet

A Triple X C++ Terminal Project as part of the Udemy Unreal Engine C++ Developer Course by Gamedev.tv.

The objective of the game is to guess the correct 3 numbers.

2 hints are given, the addition and product of all the 3 numbers.

Introduction is printed.

void PrintIntroduction(int Difficulty)
{
//Print welcome messages to the terminal
 
    std::cout << "\n\nChoose the correct passcodes at level..." << Difficulty << "\n";
    std::cout << "Start guessing till its right...\n";
}

3 numbers are selected at random. Randomization is set to be based off of the time of day. The numbers increase with difficulty. A max level difficulty is set at 10. The game is looped until all the levels are completed.

bool PlayGame(int Difficulty)
{
    PrintIntroduction(Difficulty);
    //Declare 3 number code
    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 CodeSum and CodeProduct to the terminal
    std::cout <<"\n- 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 <<"\n";

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

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

//Check if the player's guess is correct
    if(GuessSum == CodeSum && GuessProduct == CodeProduct)
{
    std::cout << "\nGo Next!";
    return true;
}
 else
{
    std::cout << "\nRetry!";
    return false;

}

}


int  main()
{   
    srand(time(NULL)); // create new random sequence based on time of day
    int LevelDifficulty = 1;
    const int MaxDifficulty = 10;
    
    while(LevelDifficulty<=MaxDifficulty)// Loop game until all levels completed
    {
    
        bool bLevelComplete = PlayGame(LevelDifficulty);
        std::cin.clear(); //Clears any errors
        std::cin.ignore(); // Discards the buffers

        if (bLevelComplete)
        //increase level difficulty
        {
            ++LevelDifficulty;
        }
        
    }
   
   std::cout << "\nEnd Game~Well done!";
    return 0;
}

Leave a comment

Design a site like this with WordPress.com
Get started