In the lesson, a LevelDifficulty variable was declared under the main() function.
int main()
{
int LevelDifficulty = 1;
This variable was then used as a parameter for the PlayGame () function inside the SAME code block.
while (true)
{
bool bLevelComplete = PlayGame(LevelDifficulty);
std::cin.clear(); // Clears any errors
std::cin.ignore(); // Discards the buffer
And then it was explained somehow that the value of “LevelDifficulty” is passed on to the PlayGame() function OUTSIDE the code block.
bool PlayGame(int Difficulty)
{
PrintIntroduction(Difficulty);
This outside-codeblock PlayGame() function has declared its own parameter: “Difficulty”
I don’t understand how the value of “LevelDifficulty” is passed on to “Difficulty” when the two parameters clearly have different names.
Question 1:
How is this working and why must the names be different? Why can’t the parameter for the outside-code block PlayGame() be also “LevelDiffcutly” instead of the differently-named “Difficulty”?
Question 2:
Why must the “Difficulty” parameter inside the outside-code block PlayGame() be declared with an “int”? In the main(), the “LevelDifficulty” was already declared with an “int”. If “LevelDifficulty” and “Difficulty” are supposed to carry the same value (which is an integer), then shouldn’t there be no need to repeat the “int” declaration?
I feel like there was something that wasn’t clearly explained. Thanks to anyone who will be able to answer!
My full code below for reference
#include <iostream>
void PrintIntroduction(int Difficulty)
{
// Print welcome messages to the terminal
std::cout << "\n\n___________________________AAAAAA\n";
std::cout << "_______________________AAAAAcceeAA\n";
std::cout << "_____________________AAAcccceeaaAA\n";
std::cout << "_________________AAAAAcccceeaaaAA\n";
std::cout << "_____________AAAAAcccccceeaaaaAA\n";
std::cout << "__AAAAAAAAAAAAcccccceeaaaaaaaAA\n";
std::cout << "AAAccccccccccccceeaaaaaaaaaaAA\n";
std::cout << "__AAAaaaaaaaeeceeeeeaaaaaagAA\n";
std::cout << "____AAAAAAaaaaaaaaeeeeeeagAA\n";
std::cout << "________AAAAAAAAaaaaaaeeeggAA\n";
std::cout << "_________#######AAAAAaaaagggAA\n";
std::cout << "________###| |########AAaaaagggAA\n";
std::cout << "______AA###|_|###| tr|#####AAAaaggggAA\n";
std::cout << "____AAEE#########|_|########AAAAgggAA\n";
std::cout << "__AAEEAA#################EEEEEAAAAA\n";
std::cout << "__AAEEEEEE#########EEEEEEEEAAEAA\n";
std::cout << "__AAAAEEEEEEEEEEEEAAAAAAEEEEEAA\n";
std::cout << "_A§xxxAAEEEEEEAAAAEEEEEEEEEEEEAA\n";
std::cout << "_A§xxxAAEEEEAAAAAAEEEEEEEEEEEEAA\n";
std::cout << "__AAAAEEEEEE§xxxAAEEEEEEEEEEEEAA\n";
std::cout << "__AAEEAAEEEE§xxxAAEEEEEEEEEEEEAA\n";
std::cout << "__AAEEAAEEEEAAAAAAEEEEEEEEEEAA\n";
std::cout << "__AAEEEAAEEEEEEEEEAAEEEEEEEEAA\n";
std::cout << "__AAEEEEAAAEEEEEEEAAEEEEEEEEAA\n";
std::cout << "_AAEEEEEEEAAAAAAEEEEAAEEEEAAEEAA\n";
std::cout << "AAEEEEEEEEEEEEEEEEEEEEAAAAEEEEEEAA\n";
std::cout << "_AAAAAAAAAAAAAAAAAAAAA____AAAAAAA\n";
std::cout << "\nWelcome to the Tower of Mathemon. You are at floor " << Difficulty << ".";
std::cout << "\nEnter the three mathemagical numbers to ascend...\n";
}
bool PlayGame(int Difficulty)
{
PrintIntroduction(Difficulty);
// Declare mathemagical numbers
const int CodeA = 4;
const int CodeB = 2;
const int CodeC = 1;
const int CodeSum = CodeA + CodeB + CodeC;
const int CodeProduct = CodeA * CodeB * CodeC;
// Print CodeSum and CodeProduct to the terminal
std::cout << std::endl;
std::cout << "- There are three numbers.";
std::cout << "\n- They add up to: " << CodeSum;
std::cout << "\n- They mutiply to give: " << CodeProduct << "\n";
// 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 corect
if (GuessSum == CodeSum && GuessProduct == CodeProduct)
{
std::cout << "\nYou've chosen the correct Mathematemons!";
return true;
}
else
{
std::cout << "\nYou've chosen poorly and have angered the Mathemategods!";
return false;
}
}
int main()
{
int LevelDifficulty = 1;
while (true)
{
bool bLevelComplete = PlayGame(LevelDifficulty);
std::cin.clear(); // Clears any errors
std::cin.ignore(); // Discards the buffer
if (bLevelComplete)
{
++LevelDifficulty;
}
}
return 0;
}