tripleX game progress

Lets see how we go with a new Unreal Engine Course with a little background in C++ using this as a refresher and further training.
First dive into this forum for me with GameDev.tv trying out the exercises in Udemys’ C++ Unreal course.
The main() function in the program defines what runs and in what order of execution. Its where the machine looks for its execution instructions and usually refers to other functions defined outside the main to divide up the complexity of the code and so the processor isn’t trying to load everything at once. While loops used in this game make for continued execution of a function until a parameter is met to close the loop and end the game. Something like that?

1 Like

Const as a prefix for a variable declaration ensures the value is constant and will not be changed by the program at run time

1 Like

Preprocessor statements include prewritten functionality from an already established library. Usually to facilitate commonly used actions without having to write them out every time you want them in a program.
Expression statements are a line of instruction to be added to the stack to be completed at runtime.
Declaration statements define variables and functionsto be used in the program.
Return statement gives an integer number to identify that the program block has finished.

1 Like

A quick look at the tripleX code after a few of the basic lessons on C++ syntax. A few extra things in there as I’m getting ahead of myself a bit but basically as the tutorials suggest.

#include <iostream>
#include <ctime>
/*
void PlayGameAtDifficulty(int difficulty)
{
    std::cout<< "Run succesful"<<std::endl;
}
*/
int main ()
{
    //introduction to start the game
    std::cout<< "Initialting BattleMech startup..."<<std::endl;
    std::cout<<"Please enter your startup code here: "<<std::endl;
    
    //variables for calculation
    int a = 4;
    int b = 3;
    int c = 2;

    //variables to store the mathmatical functions of the game
    int sum = a + b + c;
    int product = a * b * c;

    std::cout << "The code has 3 numbers." << std::endl;
    std::cout << "The sum of the code is: " << sum << std::endl;
    std::cout << "The pruduct of the code is: " << product << std::endl;
    
    /*
    int difficulty = 2;
    int maxDifficulty = 10;
    while (difficulty <= maxDifficulty)
    {
        PlayGameAtDifficulty(difficulty);
        std::cin.clear(); //clears the failbit
        std::cin.ignore(); //discards the buffer
        ++difficulty;
    }

    std::cout << "WOW - You're a master hacker!\n";
    return 0; //exit with no error code
    */
}
1 Like

Hi, Dan! So do you have experience with coding? I had fun with this part of the course but am brand new to it all. Thanks for sharing!

Tidied up code for cleaner readability. Will add ASCII art later on just to move things along a bit. BattleTech themed just for fun.

#include <iostream>
#include <ctime>

void PrintIntroduction()
{
    //add ASCII art here
    std::cout << "\nWelcome to BattleMech StartUp System\n\n";

    //introduction to start the game
    std::cout<< "Initialting BattleMech reactor core...\n";
    std::cout<< "Requires a 3 digit code to continue.\n\n";
}

void PlayGame()
{
    //variables for correct answer
    int CodeA = 4;
    int CodeB = 3;
    int CodeC = 2;

    //variables to store the correct answer for sum and product
    int CodeSum = CodeA + CodeB + CodeC;
    int CodeProduct = CodeA * CodeB * CodeC;

    std::cout << " Code Criteria";
    std::cout << "\n + The code has 3 numbers.";
    std::cout << "\n + The sum of the code is: " << CodeSum;
    std::cout << "\n + The multiplication of the code is: " << CodeProduct <<std::endl;

    //store player guess
    int GuessA, GuessB, GuessC;
    std::cout << "Please enter your startup code here: \n";
    std::cin >> GuessA >> GuessB >> GuessC;
    
    int GuessSum = GuessA + GuessB + GuessC;
    int GuessProduct = GuessA * GuessB * GuessC;
    
    //game logic if statment for comparing guess and correct answer
    if (GuessSum == CodeSum && GuessProduct == CodeProduct) {
        std::cout << "You guessed correctly... Systems Online.\n";
    } else {
        std::cout << "Incorrect Code Entered... Shutting Down.\n";
    }
}

int main()
{
    PrintIntroduction();
    PlayGame();
    /* Commented out for future implementation
    int difficulty = 2;
    int maxDifficulty = 10;
    while (difficulty <= maxDifficulty)
    {
        PlayGameAtDifficulty(difficulty);
        std::cin.clear(); //clears the failbit
        std::cin.ignore(); //discards the buffer
        ++difficulty;
    }

    std::cout << "WOW - You're a master hacker!\n";
    */
    return 0; //exit with no error code
}
1 Like

Another quick update to the game code. Just working through the tutorials and adjusted the game loop to include pass/fail logic and corresponding messages.

#include <iostream>
//#include <ctime>

void PrintIntroduction(int Difficulty)
{
    //add ASCII art here
    std::cout << "\nBattleMech StartUp System\n\n";

    //introduction to start the game
    std::cout<< "Initialting BattleMech reactor core...\n";
    std::cout<< "Requires a level " << Difficulty;
    std::cout<< ": 3-digit code to continue.\n\n";
}

bool PlayGame(int Difficulty)
{
    PrintIntroduction(Difficulty);
    //variables for correct answer
    int CodeA = 4;
    int CodeB = 3;
    int CodeC = 2;

    //variables to store the correct answer for sum and product
    int CodeSum = CodeA + CodeB + CodeC;
    int CodeProduct = CodeA * CodeB * CodeC;

    std::cout << "You are at level " << Difficulty << std::endl;
    std::cout << " Code Criteria";
    std::cout << "\n + The code has 3 numbers.";
    std::cout << "\n + The sum of the code is: " << CodeSum;
    std::cout << "\n + The multiplication of the code is: " << CodeProduct <<std::endl;

    //store player guess
    int GuessA, GuessB, GuessC;
    std::cout << "Please enter your startup code here: \n";
    std::cin >> GuessA >> GuessB >> GuessC;
    
    int GuessSum = GuessA + GuessB + GuessC;
    int GuessProduct = GuessA * GuessB * GuessC;
    
    //game logic if statment for comparing guess and correct answer
    if (GuessSum == CodeSum && GuessProduct == CodeProduct) {
        std::cout << "Authorised User... Continue to next code.\n";
        return true;
    } else {
        std::cout << "Incorrect Code Entered... Please try again.\n";
        return false;
    }
}

int main()
{
    int LevelDifficulty = 1;

    //int difficulty = 2;
    const int MaxDifficulty = 4;
    while (LevelDifficulty <= MaxDifficulty) //loop the game until levels completed
    {
        bool bLevelComplete = PlayGame(LevelDifficulty);
        std::cin.clear(); //clears the failbit
        std::cin.ignore(); //discards the buffer

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

        }
        
    }

    std::cout << "Codes accepted... System Online.\n";

    return 0; //exit with no error code
}
1 Like

Hi there telesphoros, I am just playing around with posting on these forums, kind of new to it. The Udemy course suggested trying it out. I have done a C and a C++ Udemy course prior to this and I mess around with the MechWarrior5 editor and Unreal as I’m just starting out delving into game development. Its fun stuff and this course seems well set up to talk us through coding with a “real life” example, great style of learning I think

1 Like

The sort-of-finished game, about 3 hours or study time. Its got some game logic and messages with a bit of common random number generation but simplified nicely for a starter tutorial. I imagine we will use some of this stuff in future games as there are many common threads of how some games work in their problem solving aspects. Like it. here’s the code below. I really liked seeing how the complexity evolved from the basic premise and functionality to just have something that compiles and works moving through to making it more complex and interesting. I think this is a vital step in witnessing the process to be able to understand how to build and develop a much more complex experience.

#include <iostream>
#include <ctime>

void PrintIntroduction(int Difficulty)
{
    //add ASCII art here
    std::cout << "\nBattleMech StartUp System\n\n";

    //introduction to start the game
    std::cout<< "Initialting BattleMech reactor core...\n";
    std::cout<< "Requires a 4 level: 3-digit code to continue.\n\n";
}

bool PlayGame(int Difficulty)
{
    PrintIntroduction(Difficulty);
    //variables for correct answer
    int CodeA = rand() % Difficulty + Difficulty;
    int CodeB = rand() % Difficulty + Difficulty;
    int CodeC = rand() % Difficulty + Difficulty;

    //variables to store the correct answer for sum and product
    int CodeSum = CodeA + CodeB + CodeC;
    int CodeProduct = CodeA * CodeB * CodeC;

    std::cout << "Please enter a level " << Difficulty << " clearance code to continue..."<< std::endl;
    std::cout << " Code Criteria";
    std::cout << "\n + The code has 3 numbers.";
    std::cout << "\n + The sum of the code is: " << CodeSum;
    std::cout << "\n + The multiplication of the code is: " << CodeProduct <<std::endl;

    //store player guess
    int GuessA, GuessB, GuessC;
    std::cout << "Please enter your startup code here: \n";
    std::cin >> GuessA >> GuessB >> GuessC;
    
    int GuessSum = GuessA + GuessB + GuessC;
    int GuessProduct = GuessA * GuessB * GuessC;
    
    //game logic if statment for comparing guess and correct answer
    if (GuessSum == CodeSum && GuessProduct == CodeProduct) {
        std::cout << "Authorised User completed level " << Difficulty << " security clearance... Continue to next code.\n";
        return true;
    } else {
        std::cout << "Incorrect Code Entered... Please try again.\n";
        return false;
    }
}

int main()
{
    srand(time(NULL));

    int LevelDifficulty = 1;
    const int MaxDifficulty = 4;

    while (LevelDifficulty <= MaxDifficulty) //loop the game until levels completed
    {
        bool bLevelComplete = PlayGame(LevelDifficulty);
        std::cin.clear(); //clears the failbit
        std::cin.ignore(); //discards the buffer

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

    std::cout << "\nCodes accepted... System Online.\n\n";

    return 0; //exit with no error code
}
1 Like

Very cool! I just finished the Bull Cow Game, and am moving on to the Building Escape. Good luck with the lessons, and nice to meet you.

Cheers!
Tele

Privacy & Terms