Hello, i finished the TripleX module, but i’m a little unclear on one part. The difficulty value (variable?) is being passed into a couple of functions, PrintIntroduction and PlayGame, but i haven’t defined a ‘difficulty’ variable anywhere. So i’m not sure where the value is coming from. Anyone help me to understand that? Thank you!
#include <iostream>
#include <ctime>
void PrintIntroduction(int Difficulty)
{
// Print welcome message
std::cout << "\n\nYou are a secret agent breaking into a level " << Difficulty;
std::cout << " secure server room...\nEnter the correct codes to continue...\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;
const int CodeSum = CodeA + CodeB + CodeC;
const int CodeProduct = CodeA * CodeB * CodeC;
// Print sum and product
std::cout << "+ There are 3 numbers in the code\n";
std::cout << "\n+ The codes add-up to: " << CodeSum;
std::cout << "\n+ The codes multiple 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 player is correct
if (GuessSum == CodeSum && GuessProduct == CodeProduct)
{
std::cout << "Level up!";
return true;
}
else
{
std::cout << "Try again!";
return false;
}
}
int main()
{
srand(time(NULL)); //create a new random sequence based on the time of the day
int LevelDifficulty = 1;
int const MaxDifficulty = 5;
while (LevelDifficulty <= MaxDifficulty) // Loops until all levels are completed
{
bool bLevelComplete = PlayGame(LevelDifficulty);
std::cin.clear(); // clears any errors
std::cin.ignore(); // discards the buffer
if (bLevelComplete)
{
++LevelDifficulty;
}
}
std::cout << "\nGame completed";
return 0;
}