Hi,
I thought I would share my TripleX game. I followed the mechanics but included a ‘lives’ system which deducts whenever a wrong code is input.
// preprocessor directives
#include <iostream>
#include <ctime>
/* variables are used to store data,
to use variables in C++ you must declare them first
by declaring a variable, it reserves space in memory.
variables need to be given a data type (int, char)
int a; < this is an uninitiated variable
int a = 1; < this is a initiated variable
variables can be changed at runtime
*/
void PrintIntroduction(int Difficulty)
{
/* expression statements
an expression terminated with a ; is a expression statement.
print welcome messages to the terminal */
std::cout << "\nYou are a halfling thief trying to explore deeper into a mage's tower. Currently you are on floor " << Difficulty << std::endl;
std::cout << "Enter the correct magical numbers to unlock the door...\n\n";
}
/* parameters need to be passed with type. once passed, they are passed without type
*/
bool PlayGame(int Difficulty)
{
/* const, or constant, is a keyword. it ensures that the variables cannot be changed.
declaration statements
used to declare the values of the three number code.*/
const int CodeNumber1 = rand() % Difficulty * Difficulty + 1;
const int CodeNumber2 = rand() % Difficulty * Difficulty + 1;
const int CodeNumber3 = rand() % Difficulty * Difficulty + 1;
const int CodeSum = CodeNumber1 + CodeNumber2 + CodeNumber3;
const int CodeProduct = CodeNumber1 * CodeNumber2 * CodeNumber3;
PrintIntroduction(Difficulty);
/* this prints the value of the sum and product to the terminal */
std::cout << "+ The requires three magical numbers to unlock it";
std::cout << "\n+ The magical numbers add-up to " << CodeSum;
std::cout << "\n+ The product of the numbers are " << CodeProduct;
/*std::cin >> PlayerGuess;
>> - extraction operators
std::cin will extract everything up until a space. if values exist in
cin stream, it will continue until stream is empty. characters get
converted to 0.
chars like 'x' will halt cin from working until it is reset.
*/
int Guess1, Guess2, Guess3;
std::cout << "\nEnter the magical numbers: \n";
std::cin >> Guess1 >> Guess2 >> Guess3;
// std::cout << "You entered: " << Guess1 << Guess2 << Guess3;
int GuessSum = GuessSum = Guess1 + Guess2 + Guess3;
int GuessProduct = Guess1 * Guess2 * Guess3;
/*
if statements - execute code block - or compound statement - if conditions are met.
if we don't wish code block to execute, condition = false
if we do wish code block to execute, condtion = true
*/
// if-else statements used to
if (GuessSum == CodeSum && GuessProduct == CodeProduct)
{
std::cout << "The magical numbers sparkle and the door opens. You progress to the next floor of the tower.\n";
return true;
}
else
{
std::cout << "The magical numbers are wrong.";
return false;
}
}
/* main function
return type - this declares the type of variable returned.
function name - important to give names that describe its use.
*/
int main()
{
srand(time(NULL));
int LevelDifficulty = 1;
int Lives = 3;
const int MaxDifficulty = 5;
while (LevelDifficulty <= MaxDifficulty && Lives > 0) // loop until all game levels are completed.
{
bool bLevelComplete = PlayGame(LevelDifficulty);
std::cin.clear(); // clears any errors.
std::cin.ignore(); // discards the buffer.
if (bLevelComplete)
{
// this can be expressed as ++LevelDifficulty
LevelDifficulty += 1;
}
else
{
Lives -= 1;
}
}
if (lives == 0)
{
std::cout << "\nUnfortunately you failed. The numbers burst into flames and the door is lost forever.";
}
else
{
std::cout << "\nWell done, adventurer, you have overcome the wizard's tower.\n";
}
// return statement
return 0;
}