Hello everyone
I have finished my TripleX game and thought it would be cool to share it here. I had prior experience with C++ but this is the first time I created a code from scratch and it was really fun!
I added a few features of my own in the code.You can select the game mode at the start which affects how the code values are calculated and I also added some code to make sure that the title and initial game description is shown only once. I’m looking forward to learning the next part of the course!
#include <iostream> // Includes an external code library to be used in my code
#include <ctime>
bool ShownTitle = false; // Declaring a global variable for the title so it is only shown once at the start of the game
int ChosenMode; // A global variable that is used by ModeSelect and PlayGame functions to control how the CodeA, CodeB, and CodeC variables are calculated
void ModeSelect() // A mode selection screen that appears at the start of the game
{
std::cout << "\nPlease select your game mode by pressing a number: ";
std::cout << "\n[1] Easy Mode";
std::cout << "\n[2] Normal Mode\n";
int GameMode;
std::cin >> GameMode;
if (GameMode == 1 || GameMode == 2)
{
ChosenMode = GameMode;
}
else
{
std::cout << "\nInvalid input!";
ModeSelect();
}
}
void PrintIntroduction(int Difficulty)
{
// Introduction to the game in the form of an expression statement
if (Difficulty == 1 && ShownTitle == false)
{
std::cout << "___________ .__ .__ ____ ___\n";
std::cout << "\\__ ___/______|__|_____ | | ____ \\ \\/ /\n";
std::cout << " | | \\_ __ \\ \\____ \\| | _/ __ \\ \\ / \n";
std::cout << " | | | | \\/ | |_> > |_\\ ___/ / \\ \n";
std::cout << " |____| |__| |__| __/|____/\\___ >___/\\ \\ \n";
std::cout << " |__| \\/ \\_/ \n";
std::cout << "\n\nYou are locked in a facility with an AI called TripleX. It refuses to let you out.";
}
std::cout << "\nTripleX presents you with a Level " << Difficulty << " terminal. You need to enter the correct codes to escape.\n";
}
bool PlayGame(int Difficulty, int MaxGameDifficulty, int Mode)
{
PrintIntroduction(Difficulty);
ShownTitle = true; // Changing the variable to true as the title has been shown after calling the PrintIntroduction function for the first time
// Declarion of the offset variable to be used by the calculation in the code value decleration code below based on the game mode the player has chosen
int ModeOffset;
if (Mode == 1)
{
ModeOffset = 1;
}
else if (Mode == 2)
{
ModeOffset = Difficulty;
}
// Decleration statement where variables are declared to be used for later calculation
const int CodeA = (rand() % Difficulty) + ModeOffset;
const int CodeB = (rand() % Difficulty) + ModeOffset;
const int CodeC = (rand() % Difficulty) + ModeOffset;
// Calculation of sum and product using values stored in variables that were declared earlier in the code
const int CodeSum = CodeA + CodeB + CodeC;
const int CodeProduct = CodeA * CodeB * CodeC;
// Expression statement that displays the sum and product
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 << 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 player guess is correct
if (GuessSum == CodeSum && GuessProduct == CodeProduct)
{
if (Difficulty < MaxGameDifficulty)
{
std::cout << "\n*** Correct. *** However, this was an expected outcome. Next level.";
}
return true;
}
else
{
std::cout << "\nWrong answer. Clearly my estimation of your skills was too optimistic. Try again.";
return false;
}
}
int main()
{
ModeSelect(); // Calling the game mode selection function when the game starts
srand(time(NULL));
int LevelDifficulty = 1;
const int MaxDifficulty = 5;
while (LevelDifficulty <= MaxDifficulty) // Loop game until all levels are completed
{
bool bLevelComplete = PlayGame(LevelDifficulty, MaxDifficulty, ChosenMode);
std::cin.clear();
std::cin.ignore();
if (bLevelComplete)
{
++LevelDifficulty;
}
}
std::cout << "\nCongratulations, you have entered the final code. You can leave.";
return 0;
}