Here is my final (pimped) Version of TripleX ;)

I made my full version of that game with some different things.

In my version you can choose your Level and i added much more… (No spoiler here).
I am new to C++, and i developed this by using the Video-Teachings, Google, YouTube and my ideas… ( and my brain of course :wink: )

So try it out and have fun!!!
I recomment to cear the console and make it as big as possible before starting the game :slight_smile:
And i am happy, if you leave a comment. Tell me what you think! :slightly_smiling_face:

#include<iostream>
#include<ctime>
#include<mutex> 
#include<string>
#include<thread>
#include<chrono>

//Name space res for the type-writer effect
using std::cout;
using std::flush;
using std::string;
using std::this_thread::sleep_for;
using std::chrono::milliseconds;

// Forward SlowPrint  --- tbh i do not know why i have to do it, but the instruction i found told  me so ;) 
void SlowPrint(const string&, unsigned int);

void PrintIntroduction() 
{   


    /* Some ascii aRT for the gamestart */
    std::cout << std::endl;
    std::cout << std::endl;
    std::cout << std::endl;
    std::cout << std::endl;
    std::cout << "\n         .---.                          |";
    std::cout << "\n  =   _/__~0_ _X .    .  *             -O-       '";
    std::cout << "\n = = (__o___o___o_)             .       |";
    std::cout << "\n                 .                        *";
    std::cout << "\n     *  o     .    '       *      .        o";
    std::cout << "\n __   ______________                                   __   ";
    std::cout << "\n|  | |  |/_   _____/   __ _             ____   __ __ _/  |_ ";
    std::cout << "\n|  | |  | |    __)   /  _   )   ______  /  _ | |  |  \\   __|";
    std::cout << "\n|  <_>  | |    |    /  <_>  |  /_____/ (  <_> )|  |  /|  |  ";
    std::cout << "\n|______ | |___ |    L_______/           | ___/ |____/ |__|  ";
    std::cout << std::endl;
    std::cout << std::endl;

    // This following output informs the player about the game (slowPrint)
    string BeginnerMessage = "You are a prisoner of aliens in a space-ship. They think all humans are stupid.\nThe very friendly artificial intelligence AI-610 is watching your door-locks. \nIt is not allowed to tell you the codes, but the friendly AI decided to give you some hints! \nYou need to enter the correct door codes to escape....\n";
    SlowPrint(BeginnerMessage, 1);
}   

// here i set an empty flag "Gamestart" for the std::call_once function, to use later  (you have to #include<mutex> in your Programm)
std::once_flag GameStart;

bool PlayGame(int Difficulty, int GuessCounter)
{
  
    
      /*because the ASCII Art in  PrintIntroduction() is to big 
    it should load only once in the game ;)   */
    std::call_once(GameStart, PrintIntroduction); // this calls the function "PrintIntroduction" and set the flag "GameStart" to used so it never starts again
    
       
       
        /* declare 3 codes (notice: The unusual names in UpperCamelCase following the
        Unreal4 naming conventions. */
    const int CodeA = rand() % Difficulty + Difficulty;
    const int CodeB = rand() % Difficulty + Difficulty;
    const int CodeC = rand() % Difficulty + Difficulty;

    // declare sum and product
    const int CodeSum = CodeA + CodeB + CodeC ;
    const int CodeProduct = CodeA * CodeB * CodeC;

    
    // slowprint to sum/product to terminal
    std::cout << std::endl;
    string HintMessage1 = "*****************************************************\n** AI-610 : \n** You are now in a Level-";
    SlowPrint(HintMessage1, 1);
    cout << Difficulty;
    string HintMessage2 = "-Difficulty-Room \n** There are 3 numbers in the code!\n** The codes adds-up to: ";
    SlowPrint(HintMessage2, 1);
    cout << CodeSum << std::endl;
    string HintMessage3 ="** If you multiply the codes the product is: ";
    SlowPrint(HintMessage3, 1);
    cout << CodeProduct << std::endl; 
    string HintMessage4 = "*****************************************************\n";
    SlowPrint(HintMessage4, 1);


    // declare the Variables for player guesses
    int GuessA, GuessB, GuessC;

    // following i changed the teached program because i wanted the player to have more guesses!   
    while ( GuessCounter != 4 )
    {
         // Waiting for player input for the codes
        std::cout << "Now enter your 3 numbers with spaces in between \n";
        std::cout << "(like e.g: 1 2 3) and hit Return ! \n";
        std::cin >> GuessA >> GuessB >> GuessC;
        
        // print out the guess
        string GuessPrint = "Your guess was: ";
        SlowPrint(GuessPrint, 1);
        cout <<GuessA << "  " << GuessB << "  " << GuessC << std::endl;


        int GuessSum = GuessA + GuessB + GuessC;
        int GuessProduct = GuessA * GuessB * GuessC;
    
    // Checking if the players guess is right
        if (GuessSum == CodeSum && GuessProduct == CodeProduct ) // so he is right
        {
            string OpenedDoor = "*********************************************\n** AI-610 : YOU OPENED THAT DOOR ! \n**  You entered the next room \n**********************************************" ;   
            SlowPrint(OpenedDoor, 1);
            return(true);

        } else // he was wrong:
        {       
            string WrongGuess1 ="*********************************************************\n** AI-610 :  YOU ARE WRONG! \n**  You have only 3 attempts\n**  This was attempt number: ";
            SlowPrint(WrongGuess1,1);
            cout << GuessCounter;
            string WrongGuess2 = "! \n**********************************************************\n";
            SlowPrint(WrongGuess2, 1);
            ++GuessCounter;
            
            if (GuessCounter == 4) // after 3 attemps: GAME OVER! 
            {
                string GameOver = "*******************************************************************************\n** AI-610 : SO YOU LOST! \n**  I am sorry, but that was wrong!\n**  You can to try again later ! Maybe in a lower level?? \n*********************************************\n";
                SlowPrint(GameOver, 25);

            }
        }

    }
    /*  if the player lands here, it means he was 3 times wrong (guesscounter == 4) 
        and the function returns now "false" to end the game*/
    std::cout << "The solution was : " << CodeA << " " <<  CodeB << " " << CodeC << "!"; 
    return(false);
}


int main()
{   
    /*  to initialize rand() with a different seed based 
        on the time of the day*/
    srand(time(NULL));
    
    //  i want to let the player decide, on what level he think he is... ;)
    int LevelQuery;
    string WelcomeMessage = "\n*********************************************************\n Welcone to ALIEN-OUT !! Open 6 Doors to win! \nOn what level do you want to play that game?\nFor LOW enter a number between 1-5, MEDIUM 5-10; HARD = more than 10!\nHint : Enter only a positiv number or the game does not work properly ;) \n";
    SlowPrint(WelcomeMessage, 1);
    std::cin >> LevelQuery; //if the player enters not a number, the game will stop after a time = This is a Feature, not a BUG.;)
    


    // declare needed variables
    int LevelDifficulty = LevelQuery;	
    const int MaxDifficulty = LevelQuery + 6;
    int GuessCounter = 1;
    


    while (LevelDifficulty <= MaxDifficulty)  // will Play the Game until maxLEvel is reached
    {
        bool bLevelComplete = PlayGame(LevelDifficulty, GuessCounter); // bools in Unreal should start with an lowercase b
        std::cin.clear(); // clears any error
        std::cin.ignore(); // Discards the buffer
        
        if (bLevelComplete)    
        {
            ++LevelDifficulty;
        }else
        {
            return(0);
        }
        
    }

     // the player won and here is the Last message
    string WinnerMessage = "**********************************************************************\n**     AI-610 : GREAT!! YOU WON! YOU ESCAPED! HUMANS ARE NOT STUPID!!!     **\n**     You can use that small space-tender in front of you!         **\n**     I programmed the directions to your home into it.            **\n**     Have a nice flight. Good luck and goodbye!                   **\n**********************************************************************\n";
    SlowPrint(WinnerMessage, 25);

    return 0;
} 

void SlowPrint(const string& Message, unsigned int MillisPerChar)
{
    for(const char c:Message)  // for every char in the message 
    {
        cout << c << flush; //print the character and then ?flush?
        sleep_for(milliseconds(MillisPerChar));  //self explaining ;)
    }

}
1 Like

Incredible job! You put so much effort into you code and I can see it! I love it :fire:

Thank You. :slight_smile:

#include

#include

void PrintIntroduction(int Difficulty)

{

std::cout<<"welcome survivor\n";

std::cout<<"in order to pass the door,\n";

std::cout<<"you have to enter the access codes.........\n\n\n";

std::cout<<"*HINTS*\n";

std::cout <<"enter the access codes \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 CodePro = CodeA*CodeB*CodeC;



std::cout<<"* the access code is 3 digits\n";

std::cout <<"* the product of 3 numbers is:"<< CodePro<<std::endl;

std::cout<<"* the sum of 3 numbers is:"<<CodeSum<<std::endl;

int inputA,inputB,inputC;

std::cin>>inputA>>inputB>>inputC;



int InputSum  = inputA+inputB+inputC;

int InputPro = inputA*inputB*inputC;

     

if(CodeSum==InputSum && InputPro==CodePro)

 {

     std::cout<<"you have unlocked the gate \n";

     return true;

 }

 else

 {

    std::cout<<"you are locked try again\n";

    return false;

 } 

}

int main()

{

srand(time(NULL));

int LevelDifficulty = 1;

const int MaxDifficulty = 5;

while (LevelDifficulty <= MaxDifficulty)

{

   bool blevel = PlayGame(LevelDifficulty);

   std::cin.clear();

   std::cin.ignore();

   if (blevel)

   {

     ++LevelDifficulty;

     std::cout<<"you have entered a new level \n";

     

   }

                 

}

std::cout<<“congratulation on completing the game”;

return 0;

}

Privacy & Terms