Triple X - Taming rand()

The game started to feel difficult on level 4. It was quite easy to make because I have experience coding in other languages.

Code:

#include <iostream>
#include <ctime>

void PrintIntroduction() 
{
    // Printing welcome messages to terminal
    std::cout << "\n\nYou see the world around you glazing slowly. Everything stops around you and the only way to save everyone is to go back in time and stop the glazing before it even starts. \n\n";

    std::cout << "Get ready for a challenge, brave one. You need to stop a mad man from making his dream true and find the Time Machine.\n\n";

    std::cout <<  "Time Machine is in government's secret lab and you need to get access it by hacking their security system. If you don't succeed, you will end up to timeless space for the rest of your life.\n\n";
    
    std::cout << "You need to enter correct answers to following codes to get access to the time machine...\n";
}
bool PlayGame(int Difficulty) 
{
    std::cout << "Your current level is " << Difficulty << std::endl;

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

    // Using operators to get sum and multiply of our numbers
    int CodeSum = CodeA + CodeB + CodeC;
    int CodeMult = CodeA * CodeB * CodeC;

    //Print out the sum and multiply results of our numbers
    std::cout << "\n~ There are 3 numbers in the codes.\n";
    std::cout << "~ The codes add-up to: " << CodeSum;
    std::cout << "\n~ The codes multiply to give: " << CodeMult << std::endl;

    // Declearing player guess variables to store input
    int GuessA, GuessB, GuessC;
    
    // Storing player guess
    std::cin >> GuessA >> GuessC >> GuessB;

    int GuessSum = GuessA + GuessB + GuessC;
    int GuessMult = GuessA * GuessB * GuessC;
    
    std::cout << "\nThe sum is " << GuessSum;
    std::cout << "\nThe multiply is " << GuessMult;

    // Check if user gives correct numbers
    if(GuessSum == CodeSum && GuessMult == CodeMult) 
    {
        std::cout << "\n\n~~ Well done! Keep going! ~~\n";
        return true;
    } else {
        std::cout << "\n\n~~ You failed... Be careful and try again! ~~\n";
        return false;
    }
}

int main() 
{
    srand(time(NULL));  // Create new random sequence based on time of day
    PrintIntroduction();

    int LevelDifficulty = 1;
    const int MaxDifficultyLevel = 5;

    // Loop game until difficulty level is as high as max difficulty
    while (LevelDifficulty <= MaxDifficultyLevel) 
    {
        bool bLevelComplete = PlayGame(LevelDifficulty);
        std::cin.clear(); // Clears errors
        std::cin.ignore(); // Discards the buffer

        // If player completes level, difficulty increases
        if (bLevelComplete)
        {
            LevelDifficulty++;
        }
    }

    std::cout << "\n ~~ Amazing! You made it! ~~";
    return 0;
}

Real quick everyone. Has anyone had the idea of adding a timer between each level in this? I might do it.

So I played through it and was able to solve all of them in my head so I thought of a simple way to make it harder.
Where you initialize the LevelDifficulty you can set it higher than one and under PrintIntroduction you can subtract the Difficulty by the amount you raised it to make it look like youโ€™re on level 1, 2, etc. For example, I subtracted it by one below because LevelDifficulty was set to 2. You can go higher of course, but remember it will get difficult. I also thought of another way to make it more random but didnโ€™t go with it. You could add 1 to the end of codeB and 2 to the end of codeC.
By the way, this is a great section to introduce people to C++. Keep up the good work and thank you!

#include<iostream>
#include<ctime>

void PrintIntroduction(int Difficulty)
{
    //prints story on screen
    std::cout << "\n\nYou were captured by some pirates and are locked in a level "<< Difficulty - 1 <<" cell with a 3 digit combination lock...\n";
    std::cout << "You have to enter the correct code to get out...\n";
    
    std::cout << " ______ \n";
    std::cout << " |_||_| \n";
    std::cout << " |_||_| \n";
    std::cout << "_|_||_|_\n\n";
}

bool PlayGame(int Difficulty, int MaxLevel)
{
    PrintIntroduction(Difficulty);

    //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 sum/product to terminal 
    std::cout << "You see some text on them...";
    std::cout << "\nWhat 3 numbers add up to " << CodeSum << "?";
    std::cout << "?\nAnd multiplie into " << CodeProduct << "?";

    //store player guess
    int GuessA, GuessB, GuessC;
    std::cout << "\nYour answer (Use spaces in between each): \n";
    std::cin >> GuessA >> GuessB >> GuessC;
    std::cout << "\nYou answered " << GuessA << GuessB << GuessC << "...";

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

    //checks to see if the player is right
    if(CodeSum == GuessSum && CodeProduct == GuessProduct)
    {
        std::cout << "\nYou put in the number and the lock opens. You can now get out of the cell...";
        
        if (Difficulty != MaxLevel)
        {
            std::cout << "\nExcept there is another door with another lock";
        }

        return true;
        
    }
    else
    {
        std::cout << "\nYou put in the number but it won't open. Must be wrong.\nYou should try again.";
        return false;
    }
}



int main()
{
    srand(time(NULL)); //Create a new random sequence based on the time of day

    int LevelDifficulty = 2;
    int MaxLevel = 5;

    while(LevelDifficulty <= MaxLevel) //stops game when MaxLevel is met 
    {
        bool bLevelComplete = PlayGame(LevelDifficulty, MaxLevel);
        std::cin.clear(); //clears any errors
        std::cin.ignore(); //discards the buffer

        if (bLevelComplete)
        {
            ++LevelDifficulty;
        }
        
    }
    std::cout << "You've escaped the cells and go up to the deck (luckily the ship is sill in harbor). You've escaped.";
    return 0;

}

I love this solution and the way you are using your functions. I do not believe arrays were discussed in this section, so seeing Arrays to pass data is refreshing. It is definitely a little different than what was taught. Before moving forward on lessons, Ive been looking at other solutions to the Triple-X game and looking into other ways or logic for the build.

1 Like

This whole section was a really nice overview of C++, here is my completed version of the game.

#include <iostream>
#include <ctime>
void PrintIntroduction(int Difficulty)
{
 //Print welcome messages

    
    std::cout << "\n\nYou are waiting in line to board your airplane.\n";
    std::cout << "       __|__\n--@--@--(_)--@--@--\n";
    std::cout << "As you arrive at the front of the line, the airline officer checking boarding passes informs you that you must complete a level " <<Difficulty;
    std::cout <<" puzzle in order to board as part of new TSA regulations...";
    
}
bool PlayGame(int Difficulty)
{
    PrintIntroduction(Difficulty);
   
    //Declare 3 int variables
    const int CodeA = rand() % Difficulty + Difficulty;
    const int CodeB = rand() % Difficulty + Difficulty;
    const int CodeC = rand() % Difficulty + Difficulty;
    
    
    //Now we assign the CodeSum and CodeProduct
    const int CodeSum = CodeA + CodeB + CodeC;
    const int CodeProduct = CodeA * CodeB * CodeC;
    

    
    std::cout <<"\n\n+ There are 3 numbers in the code\n";
    std::cout <<"+ The sum is: ";
    std::cout <<CodeSum;
    std::cout <<"\n+ The product is: " <<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 guess to see if its correct
    if (GuessSum == CodeSum && CodeProduct == GuessProduct)
    {
        std::cout <<"\nThe airline officer congradulates you, but the level " <<Difficulty;
        std::cout <<" section of the plane has already boarded, and you will have to complete a level " <<Difficulty + 1;
        std::cout <<" puzzle in order to board.";

        return true;
    }
    else
    {
        std::cout<< "\nThe airline officer patronazingly tells you to try again.";
        return false;
    }
    
}


int main()
{
    srand(time(NULL));
    const int MaxDifficulty = 5;
    int LevelDifficulty = 1;
    while (LevelDifficulty <= MaxDifficulty) //Loops the game until all levels are complete
    {
        bool bLevelComplete = PlayGame(LevelDifficulty);
        std::cin.clear(); //Clears any errors
        std::cin.ignore(); //Discards the input buffer

        if (bLevelComplete)
        {
            ++LevelDifficulty;
        }
        
    }
    std::cout <<" The airline officer frowns and informs you that there is in fact a seat availible and allows you to board a long, tedius and uncomfortable flight.";
    
    return 0;
}

1 Like

Here is my final code. Didnโ€™t tinker too much with it because Iโ€™m eager to jump into the next lesson.

#include <iostream>
#include <ctime>

void PrintIntroduction()
{
    //Print welcome messages to the terminal

    std::cout << " ##############################################################\n\n";
    std::cout << "\n  _|_|_|_|_|            _|            _|            _|      _|";
    std::cout << "\n      _|      _|  _|_|      _|_|_|    _|    _|_|      _|  _|";
    std::cout << "\n      _|      _|_|      _|  _|    _|  _|  _|_|_|_|      _| ";
    std::cout << "\n      _|      _|        _|  _|    _|  _|  _|          _|  _|";
    std::cout << "\n      _|      _|        _|  _|_|_|    _|    _|_|_|  _|      _|";
    std::cout << "\n                             _| ";
    std::cout << "\n                             _|\n\n";
    std::cout << " ##############################################################\n\n";
    std::cout << " You are a netrunner trying to break into a corporate database!\n";
    std::cout << " There are 10 levels of security!\n";
}

void PrintLevelDifficuty(int Difficulty)
{
    // Prints current level to the screen
    std::cout << "\n The current security level is: " << Difficulty << "\n";
    std::cout << " Enter the correct code to bypass their security...\n\n";
}

bool PlayGame(int Difficulty)
{
    PrintLevelDifficuty(Difficulty);

    // Declare 3 number code, sum and product of them
    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 sum and product to the teminal
    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 << std::endl;

    // Declare a variable for player input
    int GuessA, GuessB, GuessC;

    // Getting player input
    std::cin >> GuessA >> GuessB >> GuessC;

    // Declare a variable for sum and product of players input
    int GuessSum = GuessA + GuessB + GuessC;
    int GuessProduct = GuessA * GuessB * GuessC;

    // Checking if player guessed the code
    if (GuessSum == CodeSum && GuessProduct == CodeProduct)
    {
        std::cout << "\n ### You have successfully bypassed this security level! ###\n";
        return true;
    }
    else
    {
        std::cout << "\n ### You have entered the wrong code! The screen flashes red and displays \"ACCESS DENIED\" message! You can try again! ###\n";
        return false;
    }
}

int main()
{
    PrintIntroduction(); // Prints introduction to the screen
    srand(time(NULL)); // Uses system time to generate a random number seed

    int LevelDifficulty = 1;
    const int MaxLevelDifficulty = 10;

    while (LevelDifficulty <= MaxLevelDifficulty) // Loop game untill all levels are complete
    {
        bool bLevelComplete = PlayGame(LevelDifficulty);
        std::cin.clear(); // Clears any errors
        std::cin.ignore(); // Discards the buffer

        if (bLevelComplete)
        {
            // Increase the level difficulty
            ++LevelDifficulty;
        }
        
    }

    std::cout << "\n ### You have sucesfully hacked into the databas! Nice work! ###\n";

    return 0;
}

Hi Gavin,

I have question. Iโ€™ve added the + 1 or + Difficulty to our rand() modulo, but when I print the values, I can still see zeros coming up! But when I run the game actually, it doesnโ€™t seem that 0 is used. How does that work?

Thanks a lot.

I.

1 Like

Hi all,

I have another question. When I enter the wrong answer, the game runs again and every time it is with a new set of random numbers.

Is there a way I can store the random numbers generated and use it over and over again, until the player gets it right, and then tell the function to generate a new set of numbers for the next level?
Basically, is there a way to keep a set of numbers for each level, and not have them regenerated every time the player enters wrong answer?

Thanks a lot!

Nice! Iโ€™m in the same boat as you. Had many failed attempts to teach myself C++(programming in general). Love the use of the header files.

2 Likes

If I wanted to have some of my friends try out the game I made can I just send them the output file โ€œTripleX.exeโ€? Or do I have to send more than that?

Hello,

Here is my completed game. I tried to spice up the story a bit.

#include <iostream> 
#include <ctime>

void PrintIntroduction(int Difficulty)
{
    //The Story of the Game Printed to Terminal
    std::cout << "\n\nYou are a weary explorer looking for the treasure of the lost ruins of Amaranthian. You must break into a level " << Difficulty;
    std::cout << " door to enter.\nThe only thing standing in your way is the ancient puzzles left behind to gaurd the treasure. Are you prepared?\n\n";
}

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

    //The output to the terminal of the mathmatical operations
    std::cout << "+ There are three numbers in the code";
    std::cout << "\n+ The numbers add-up to: " << CodeSum;
    std::cout << "\n+ The numbers multiply to: " << 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 guesses are correct.
    if (GuessSum == CodeSum && GuessProduct == CodeProduct)
    {
        std::cout << "\nYou completed the level " << Difficulty << " door. Ready for the next one?";
        return true;
    }
    else
    {
        std::cout << "\nYou failed to break the lock, want to try level " << Difficulty << " again?";
        return false;
    }
}

int main()
{
    srand(time(NULL)); //Creating Seed based on time of day

    int LevelDifficulty = 1;
    int const MaxLevel = 5;

    while (LevelDifficulty <= MaxLevel) //Loop Game till all Levels are Complete
    {
        bool bLevelComplete = PlayGame(LevelDifficulty);
        std::cin.clear(); //Clears any Errors
        std::cin.ignore(); //Discards the buffer

        if (bLevelComplete)
        {
            ++LevelDifficulty;
        }
    }
    std::cout << "\nYou have broken through all of the puzzles doors, the treasure is now yours Adventurer.";
        return 0;
}

Jib

1 Like

Same! It looks like we can all help each other achieve our C++ goals.

I enjoying the course so much, thank you. There is my game, i hope you all like it.

#include <iostream>
#include <ctime>

void PrintIntroduction(int Difficulty){
    if (Difficulty == 1)
    {
    std::cout << "\nYour submarine is the last standing in the open field with one battleship surrounding you.\n\n";
    //Ascii art
    std::cout << " _|_|_|_______\n(___________/\n\n\n\n                 ____||________\n                (_____________)\n\n";
    std::cout << "You need to input the right coordinates to destroy the battleship and survive...\n\n";
    }
    else{
    std::cout << "Be careful!.\n\n";
    //Ascii art
    std::cout << " _|_|_|_______       _|_|_|________      _|_|_|________\n(___________/       (____________/      (____________/\n\n\n\n                 ____||________\n                (_____________)\n\n";
    std::cout << "You need to input the right coordinates to destroy the " << Difficulty <<  " battleships and survive...\n\n";
}
}

bool PlayGame(int Difficulty, int TryError)
{
    PrintIntroduction(Difficulty);
    int DifficultyRange = Difficulty + 2;

    int CoordinatesA = rand() % DifficultyRange + Difficulty;
    int CoordinatesB = rand() % DifficultyRange + Difficulty;
    int CoordinatesC = rand() % DifficultyRange + Difficulty;
    int CoordinatesProduct = CoordinatesA*CoordinatesB*CoordinatesC;
    int CoordinatesSum = CoordinatesA+CoordinatesB+CoordinatesC;

        //print sum and the product
        std::cout << "There are three numbers in the panel.\n\nThe numbers multiply to give:\n";
        std::cout << CoordinatesProduct << std::endl;
        std::cout << "\nAnd the sum are:\n";
        std::cout << CoordinatesSum << std::endl;
        std::cout << "\nEnter the numbers.\n";

    int PlayerGuessA, PlayerGuessB, PlayerGuessC;

        //Store player guess
        std::cin >> PlayerGuessA >> PlayerGuessB >> PlayerGuessC;

        std::cout << "You entered: " << PlayerGuessA << ", " << PlayerGuessB << " and " << PlayerGuessC << ".";
    int GuessProduct = PlayerGuessA * PlayerGuessB * PlayerGuessC;
    int GuessSum = PlayerGuessA + PlayerGuessB + PlayerGuessC;

        std::cout << "\nThe product of your guesses is " << GuessProduct << "\n";        
        std::cout << "The sum of your guesses is " << GuessSum << "\n"; 

        //Condition to win
    if(GuessSum == CoordinatesSum && GuessProduct == CoordinatesProduct)
    {
        if (Difficulty < 5){
            std::cout << "You survived, but there is more coming.\n\n";
            return true;
            }
        else{
        std::cout << "\nCongratulations, you fight till the end and win.\n\n";
        return true;
        }
        }
        else{
            if (TryError == 1)
            {
                // print message if the player still has tries
        std::cout << "\nFast, they're approaching!.\n\n";
            return false;
            }
            if (TryError == 2)
            {
                // print message if the player still has tries
        std::cout << "\nYou're getting trapped!.\n\n";
            return false;
            }
            if (TryError == 3)
            {
                // print message if the player still has tries
        std::cout << "\nOur submarine is getting hit!.\n\n";
            return false;
            }
            else
            {
            std::cout << "\nYour submarine were destroyed and you died horribly.\n";
            return false;
            }
            

        }

}



int main(){
    srand(time(NULL));
    int TryError = 1;
    int LevelDifficulty = 1;
    int const MaxDifficulty = 5;
    while (LevelDifficulty <= 5) //Loop game until all levels are complete.
    {
    bool bLevelComplete = PlayGame(LevelDifficulty, TryError); 
            std::cin.clear(); //Clears any errors
            std::cin.ignore(); //Discard the buffer
        
        if (bLevelComplete)
        {
            ++LevelDifficulty;
            //increase the level dificulty
            }
        else {
            if (TryError <= 3) //3 attempts to try again
            {
                ++TryError;
            }
            else
            {
                return 0;
            }
            
        }

    }
        


    return 0;
    }

I included a ProgressMeter and ClearScreen function(which only works for *nix - didnโ€™t take the time to include the Windows CLS code).

#include <iostream>
#include <ctime>

void ClearScreen() 
{
    std::cout << "\x1B[2J\x1B[H";
}

void PrintProgressMeter(int LevelDifficulty) {
    ClearScreen();
    std::cout << " Triple-X Code Cracker\n";
    std::cout << "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”“\n";
    std::cout << "โ”ƒ โ•ญโ”„โ•ฎ โ•ญโ”„โ•ฎ โ•ญโ”„โ•ฎ โ•ญโ”„โ•ฎ โ•ญโ”„โ•ฎ โ•ญโ”„โ•ฎ โ•ญโ”„โ•ฎ โ•ญโ”„โ•ฎ โ•ญโ”„โ•ฎ โ•ญโ”„โ•ฎ โ”ƒ\n";
    std::cout << "โ”ƒ ";

    for(int i = 1; i < 11; i++) {
        std::cout << "โ”†";
        if (LevelDifficulty > i) {
            std::cout << "\033[7;32m" << i - 1 << "\033[0m";
        } else {
            std::cout << "\033[7;31mโ•ณ\033[0m";
        }
        std::cout << "โ”† ";
    }

    std::cout << "โ”ƒ\n";
    std::cout << "โ”ƒ โ•ฐโ”„โ•ฏ โ•ฐโ”„โ•ฏ โ•ฐโ”„โ•ฏ โ•ฐโ”„โ•ฏ โ•ฐโ”„โ•ฏ โ•ฐโ”„โ•ฏ โ•ฐโ”„โ•ฏ โ•ฐโ”„โ•ฏ โ•ฐโ”„โ•ฏ โ•ฐโ”„โ•ฏ โ”ƒ\n";
    std::cout << "โ”—โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”›\n\n";
}

// 
bool PlayGameAtDifficulty(int LevelDifficulty)
{
    const int CodeA = rand() % LevelDifficulty + LevelDifficulty;
    const int CodeB = rand() % LevelDifficulty + LevelDifficulty;
    const int CodeC = rand() % LevelDifficulty + LevelDifficulty;
    int CodeSum = CodeA+CodeB+CodeC; 
    int CodeProduct = CodeA*CodeB*CodeC;
    int GuessA, GuessB, GuessC;

    PrintProgressMeter(LevelDifficulty);

    std::cout << "Level " << LevelDifficulty - 1 << "\n";
    std::cout << "--------\n";
    std::cout << "+ The code is three positive integers.\n";
    std::cout << "+ Their sum is " << CodeSum << "\n";
    std::cout << "+ Their product is " << CodeProduct << "\n\n";

    std::cout << "Enter the correct code to continue.\n";

    // store player guess
    std::cin >> GuessA >> GuessB >> GuessC;
    int GuessSum = GuessA + GuessB + GuessC;
    int GuessProduct = GuessA * GuessB * GuessC;

    // check guess
    if (GuessSum == CodeSum && GuessProduct == CodeProduct) 
    {
        return true;
    } else {
        return false;
    } 
}

int main()
{
    srand(time(NULL));
    int LevelDifficulty = 1;
    int MaxDifficulty = 10;
    while (LevelDifficulty <= MaxDifficulty)
    {
        bool bLevelComplete = PlayGameAtDifficulty(LevelDifficulty);
        std::cin.clear();  // clears the failbit
        std::cin.ignore(); // discards the buffer
        if (bLevelComplete) {
            if (LevelDifficulty == MaxDifficulty) {
                ClearScreen();
                PrintProgressMeter(LevelDifficulty);
                std::cout << "Mission Accomplished! Congratulations!\n\n\n\n\n";
            } else {
                ++LevelDifficulty;
            }    
        } else {
            ClearScreen();
            PrintProgressMeter(LevelDifficulty);
            std::cout << "Incorrect.  Game Over, Terminally.\n\n\n\n\n";
            break;
        }
    }
    
    return 0;
}

the rand() seems to generate a random modulus outcome but upon second execution it produces exactly the same result of number. ??

that s a good question .

#include

include

void PrintIntro(int Difficulty)
{
std::cout <<โ€œYou are a secret agent breaking into a level " << Difficulty;
std::cout <<โ€ government sever roomโ€ฆ\nEnter the correct codes to continueโ€ฆ\n";

}
bool PlayGame(int Difficulty)
{
PrintIntro(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;
    // print sum and product to terminal
    std::cout << std::endl;
    std::cout <<"+ There are 3 number in the code";
    std::cout <<"\n+ The codes add-up to:" << CodeSum;
    std::cout <<"\n+ The product of the codes are:"<<CodeProduct << std::endl;

    // store player guess
    int GuessA, GuessB, GuessC;
    std::cin>> GuessA;
    std::cin>> GuessB;
    std::cin>> GuessC;
    
    //add and multiply player guesses
    int GuessSum = GuessA + GuessB + GuessC;
    int GuessProduct = GuessA * GuessB * GuessC;

    // if else statement. Checks if player is correct
    if (GuessSum ==CodeSum&& GuessProduct == CodeProduct)
    {   
        std::cout<<"You win!...On to the next server room\n";
        return true;

    }
    else 
    {
        std::cout<<"You lose!...Try again\n";
        return false;
    }

}

int main ()
{
srand(time(NULL)); // create new random sequence based on the time of day

    int LevelDifficulty= 1;
    const int MaxDifficulty = 6;

    while (LevelDifficulty<=MaxDifficulty) // loop game until all games are completed
    {
        
        bool bLevelComplete = PlayGame(LevelDifficulty);
        std::cin.clear(); //clears any errors
        std::cin.ignore(); // discards the buffer
    
        if (bLevelComplete)
        {
            ++LevelDifficulty;
        }
        
    }
    std::cout << "\n Congratulations, Agent. You have sucessfully completed the mission.";
    return 0;
}

This program was really fun.
I chose to change the modulus some so that the random numbers would range from 1-10.
The game was not very hard to play - it generated mostly lower numbers by chance for me. :wink:
Here also is my first attempt at ASCII art.

Just completed this.

this is my first actual C++ text based game and first attempt to learn a coding language, i mainly focused on code organization and re watched the videos multiple times to attempt to understand all the concepts.

#include
#include
void Introduction (int Difficulty)
{
std::cout << โ€œ\n\nYou are an activist hacker attempting to gain access to a level " << Difficulty;
std::cout << " Server \nYou must enter the correct code to crack the security and continue\n\nโ€;

}

bool PlayGame (int Difficulty )
{

Introduction (Difficulty);

int CodeA = rand() % Difficulty + Difficulty;
int CodeB = rand() % Difficulty + Difficulty;
int CodeC = rand() % Difficulty + Difficulty;

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

std::cout << std::endl;
std::cout << "There are 3 numbers in the Security Access Code\n";
std::cout << "\nThe codes add-up to: " << CodeSum;
std::cout << "\nThe codes multiply to give: " << CodeProduct <<"\n";

// store player guess
int GuessA, GuessB, GuessC;
std::cin >> GuessA >> GuessB >> GuessC;


int GuessSum = GuessA + GuessB + GuessC;
int GuessProduct = GuessA * GuessB * GuessC;
   
    //check if players guess is correct
    if( GuessSum == CodeSum && GuessProduct == CodeProduct)
    {
        std::cout << "YOU WIN!!\n";
        return true;
    }
    else
    {
        std::cout << "OH NO! THE FBI IS ON TO YOU AND HAS INITIATED A TRACE!\n";
        return false;
    } 

}
int main()
{
srand(time(NULL)); //create new randon sequence based on time of day
int LevelDifficulty = 1;
int const MaxLevelDifficulty = 6;

while (LevelDifficulty <= MaxLevelDifficulty) // loop until all levels completed
{
bool bLevelComplete = PlayGame(LevelDifficulty);
std::cin.clear(); // clears errors
std::cin.ignore(); // discards the CIN buffer from player input

    if (bLevelComplete)
    {
    //increase level difficulty
        ++LevelDifficulty;
    
    }

}

std::cout << โ€œ***Great Work, your gathered all the data needed to expose the truth!***โ€;
return 0;
}

A very fun and knowledgable exercise, managed to include the presence of a playername input thatโ€™s only asked for at the very beginning of the game and that is referenced at the end. Looking forward to the next sections, although I might wait until theyโ€™ve been fully remade for the new versions! In the meantime Iโ€™ll keep on playing around with C++ on my own.

#include <ctime>

std::string PlayerName;
std::string OK = "OK";

void PrintIntroduction(int Difficulty)
{
    //cout = characteroutput
    //cin = characterinput
    
    //player enters any name of their chosing
   if(Difficulty <= 1 ) 
   {
    std::cout << "\n\nENTER NAME BELOW\n";
    std::cin >> PlayerName;
    std::cout << std::endl;
    std::cout << "\n\n\nHello " << PlayerName << ", I want to play a game.\n";
    std::cout << "This is Step " << Difficulty << ". You are cuffed to a radiator in a room emitting poison gas. There's a keypad here with a code combination that will stop the gas...\n\n";
    std::cout << "You need to solve a riddle to gain the codes in order to stop the poison gas and continue to the next step of the game. Don't fail, your life might just depend on it.\n";
    //std::cout << std::endl; //line space between text.
   }
    else
    {
        std::cout << "\n\nCongratulations, you are now on Step " << Difficulty << " , continue onwards.\n\n";
    }
    
    
}

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;

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

    //print sum and product to the terminal
    std::cout << std::endl;
    std::cout << "* There are three numbers in the code\n";
    std::cout << "* The codes add upp to: " << CodeSum <<"\n";
    std::cout << "* The codes multiply to: "<< CodeProduct <<"\n\n";
    
    std::cout << "ENTER NUMBER BELOW\n\n";

    //store player guess
    int GuessA, GuessB, GuessC;
    std::cin >> GuessA >> GuessB >> GuessC;
    
    int GuessSum = GuessA + GuessB + GuessC;
    int GuessProduct = GuessA * GuessB * GuessC;

    //check if player's guess is correct/not correct
    if (GuessSum == CodeSum && GuessProduct == CodeProduct)
    {
        if(Difficulty < 5)
        {
            std::cout << "\nCongratulations " << PlayerName << ", you have completed Step " <<Difficulty<< " of the game. Now the code resets to a harder riddle.\n\n";
            std::cout << "Type and Enter anything to continue\n";
            std::cin >> OK;
        }

        return true;
    }
    else
    {
        if(Difficulty <= 1)
        {
            std::cout << "\nLooks like you've lost the game at the very beginning, " << PlayerName <<"... GAME OVER";
        }
        else
        {
            std::cout << "\nYou've failed, any the room begins filling with gas... GAME OVER...";
        }
        
        
        std::cout << "\nType and Enter anything to restart level\n";
        std::cin >> OK;
        return false;
    }
}

int main()
{
    srand(time(NULL)); //create new rand sequence based on TOD

    int LevelDifficulty = 1;
    int const MaxDifficulty = 5;
    while(LevelDifficulty <= MaxDifficulty) //Loop the game until all levels complete
    {
        bool bLevelComplete = PlayGame(LevelDifficulty);
        std::cin.clear(); //clears any errors
        std::cin.ignore(); //discards the buffer

        if (bLevelComplete)
        {
            ++LevelDifficulty;
        }   
    }

    std::cout << "\nCongratulations to you, " << PlayerName << " , you have completed my game, and live to see another day...";
    std::cout << "\nType OK to continue\n";
    std::cin >> OK;

    return 0;
}
1 Like

Privacy & Terms