Well done for making it this far into the section!
How is the code for your game looking so far?
Post below!
Well done for making it this far into the section!
How is the code for your game looking so far?
Post below!
This is where I’m at by the end of this lesson, nothing particularly creative, no ASCII art or anything, added a few additional functions to make things a little cleaner to my eye:
#include <iostream>
// Prints level detail and objective of the game
void PrintIntroduction(const int difficulty)
{
std::cout << "You're a secret agent breaking into a level " << difficulty << " secure server room..." << std::endl;
std::cout << "You need to enter the correct codes to continue..." << std::endl;
std::cout << std::endl;
}
// For debug purposes, prints the codes that are used to create the clues
void PrintCodesDebug(const int codeA, const int codeB, const int codeC)
{
std::cout << "\t****DEBUG****\tCodes are:\t" << codeA << " " << codeB << " " << codeC << std::endl;
}
// Prints the clue numbers and a prompt to input guesses
void PrintCluesAndPrompt(const int product, const int sum)
{
std::cout << "\tThere are 3 numbers in the code" << std::endl;
std::cout << "\tThe product of the numbers is " << product << std::endl;
std::cout << "\tThe sum of the numbers is " << sum << std::endl;
std::cout << std::endl;
std::cout << "Enter your three guesses (separated by spaces, i.e. 1 2 3): ";
}
// For debug purposes, prints the values derived from input
void PrintGuessDebug(const int guessA, const int guessB, const int guessC, const int guessProduct, const int guessSum)
{
std::cout << "****DEBUG****\tInput:\t\t" << guessA << " " << guessB << " " << guessC << std::endl;
std::cout << "****DEBUG****\tInput Product:\t" << guessProduct << std::endl;
std::cout << "****DEBUG****\tInput Sum:\t" << guessSum << std::endl;
}
// Clear error code and buffer
void ClearInput()
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
// Main logic that returns success or failure for solving the puzzle
bool PlayGame(const int difficulty)
{
PrintIntroduction(difficulty);
// Declare three read-only values for the "secret code"
const int CodeA = 4;
const int CodeB = 3;
const int CodeC = 2;
PrintCodesDebug(CodeA, CodeB, CodeC);
// Calculate the sum and product for the "secret code" clues
const int CodeProduct = CodeA * CodeB * CodeC;
const int CodeSum = CodeA + CodeB + CodeC;
PrintCluesAndPrompt(CodeProduct, CodeSum);
// Declare variables and capture user input
int GuessA, GuessB, GuessC;
std::cin >> GuessA >> GuessB >> GuessC;
// Perform result calculations
const int GuessProduct = GuessA * GuessB * GuessC;
const int GuessSum = GuessA + GuessB + GuessC;
PrintGuessDebug(GuessA, GuessB, GuessC, GuessProduct, GuessSum);
// Test if guess calculations are the same as code calculations
if (GuessProduct == CodeProduct && GuessSum == CodeSum)
{
std::cout << "You guessed correctly! Get ready for the next level..." << std::endl << std::endl;
return true;
}
else
{
std::cout << "These are the wrong numbers! Try again!" << std::endl << std::endl;
return false;
}
}
int main()
{
//Game configuration settings
const int MaxDifficulty = 6;
int LevelDifficulty = 2;
// Play the game until you've beat all the levels
while (LevelDifficulty <= MaxDifficulty)
{
bool bLevelComplete = PlayGame(LevelDifficulty);
ClearInput();
// If you guess correctly, go up a level
if (bLevelComplete)
{
LevelDifficulty++;
}
}
// You've won, let the player know before exiting
std::cout << "You hacked the server room! You win!" << std::endl;
return 0;
}
Hello Gavin, Here’s my code at the end of the section.
I’m also reporting a bug on the 3.0 lesson on Udemy : There’s no 15_tx_uc2 on forum, so forum redirects on a " Oops! That page doesn’t exist or is private."
This topic is bound to 2.9 lesson on Udemy, but there’s no challenges on this video .
Here’s the code, I modified it a bit because i didn’t want the ASCII art and intro to come again and again on each try. I couldn’t go further at level 5… didn’t have any patience for this haha.
#include <iostream>
#include <ctime>
void PrintMainTitle()
{
std::cout << " /### / ### ### ## \n";
std::cout << " / ############/ # ### /#### #### / \n";
std::cout << "/ ######### ### ## / ### /####/ \n";
std::cout << "# / # # ## ### / ## \n";
std::cout << " ## / ## ## ### / \n";
std::cout << " / ### ### /### ### /### ## /## ###/ \n";
std::cout << " ## ## ###/ #### / ### / ### / ## / ### ### \n";
std::cout << " ## ## ## ###/ ## / ###/ ## / ### /### \n";
std::cout << " ## ## ## ## ## ## ## ## ### / ### \n";
std::cout << " ## ## ## ## ## ## ## ######## / ### \n";
std::cout << " ## ## ## ## ## ## ## ####### / ### \n";
std::cout << " ## # / ## ## ## ## ## ## / ### \n";
std::cout << " ### / ## ## ## ## ## #### / / ### / \n";
std::cout << " ######/ ### ### / ####### ### / ######/ / ####/ \n";
std::cout << " ### ### ##/ ###### ##/ ##### / ### \n";
std::cout << " ## \n";
std::cout << " ## \n";
std::cout << " ## \n";
std::cout << " ## \n\n";
}
void PrintIntroduction()
{
// Introduction to the game : Print messages to the terminal
std::cout << "You are a mighty thief, and you finally found the heavy chest you were looking for in lord Dodley's mansion.\n";
std::cout << "Unfortunately, the chest can't be transported and there is an unusual lock, forcing you to enter correct codes numbers to open it...\n\n";
}
bool PlayGame(int 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 to the terminal
std::cout << "The top of the chest displays something...";
std::cout << "\n+ Lock number: " << Difficulty;
std::cout << "\n+ There are three 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 guesses
int GuessA, GuessB, GuessC;
std::cin >> GuessA >> GuessB >> GuessC;
int GuessSum = GuessA + GuessB + GuessC;
int GuessProduct = GuessA * GuessB * GuessC;
std::cout << "\nYou enter the code " << GuessA << GuessB << GuessC << " ...\n\n";
// Check if the answer is correct and return a win/lose message
if(GuessSum == CodeSum && GuessProduct == CodeProduct)
{
std::cout << "*** Click ***\n";
std::cout << "The chest makes a satisfying sound and invites you to type the next code\n\n";
return true;
}
else
{
std::cout << "*** Fwoosh! ***\n";
std::cout << "The chests ignites and burns your hands then quickly returns to a normal state. You're shaking your fingers to cool them before your next attempt.\n\n";
return false;
}
}
int main()
{
srand(time(NULL));// create new random based on time of day
PrintMainTitle();
PrintIntroduction();
int LevelDifficulty = 1;
const int MaxDifficulty = 10;
while(LevelDifficulty <= MaxDifficulty) // loop game until all levels are completed
{
bool bLevelComplete = PlayGame(LevelDifficulty);
std::cin.clear(); //Clears any errors
std::cin.ignore(); // Discards the buffer
if (bLevelComplete)
{
++LevelDifficulty;
}
}
// Win message
std::cout << "*** Success! ***\n";
std::cout << "The chest opens and you fill your bag with everything that seems to be valuable.\n";
std::cout << "When you finally get out of the mansion, you notice something is moving from inside your bag!\n\n";
std::cout << "Perhaps your adventure is not over yet...\n\n";
return 0;
}
Mine is very similar to the lectures, except I went with @Heliott’s version of printing a title only once.
I also noticed that the correct guess and continue message would appear right before the game win message, which seemed a bit odd. So I made some tweaks to the if-statement in PlayGame()
.
if (Difficulty != MaxDifficulty && GuessSum == CodeSum && GuessProduct == CodeProduct)
{
std::cout << "\nA message appears indicating another code is needed.\n\n";
return true;
}
else if (GuessSum != CodeSum || GuessProduct != CodeProduct)
{
std::cout << "\nINCORRECT CODE. Try again.\n";
return false;
}
else
{
return true;
}
}
I did have to use two of the operators that were only mentioned in the lecture but not used; the OR operator || and not equal to operator !=. The first “if” checks if it is the last level with Difficulty != MaxDifficulty
. If that is false (meaning it is the last level) it moves to the “else if” which checks if either GuessSum or GuessProduct are wrong. I had to use the OR operator otherwise, if AND had been used, both GuessSum and GuessProduct would have to be wrong, not just one. Finally, if MaxDifficulty has been reached, and GuessSum and GuessProduct are correct, the final “else” statement runs which just returns true back to main()
. It then exits the “while” loop and prints the win game message.
Hey there,
I think I am done with this for now.
I added a few lines to the implementation so that you can try max. 5 times to enter the code. After 5 tries you loose the game.
There is also a small intro text… but besides that it is pretty much what was shown in the videos.
And just ignore the typos… =)
#include <iostream>
#include <ctime>
void PrintGameIntroduction()
{
//Prints introduction message to terminal
std::cout << "======================================================================\n";
std::cout << "| Secret agent Barns, |\n";
std::cout << "| |\n";
std::cout << "| we need your help to access critical files from 5 different |\n";
std::cout << "| encrypted servers. |\n";
std::cout << "| |\n";
std::cout << "| The files are essential to ensure the security of life on earth |\n";
std::cout << "| as we know it |\n";
std::cout << "| |\n";
std::cout << "| We count on you! |\n";
std::cout << "| |\n";
std::cout << "| |\n";
std::cout << "| (no pressure... but we really really need the files...) |\n";
std::cout << "======================================================================\n\n";
}
void PrintLevelIntroduction(int Difficulty)
{
//Prints welcome message to terminal
std::cout << "\nLEVEL: " << Difficulty;
std::cout << "\n======================================================================\n";
std::cout << "| We need the data from this level " << Difficulty << " secure server.. |\n";
std::cout << "| Enter the correct code get access to the files. |\n";
std::cout << "======================================================================\n\n";
}
void PrintYouWinLevel(int Difficulty)
{
//Prints welcome message to terminal
std::cout << "\n======================================================================\n";
std::cout << "| Great you broke the code! Please collect the files and proceed |\n";
std::cout << "| Hacking the other servers. |\n";
std::cout << "| |\n";
std::cout << "| Be vigilant! |\n";
std::cout << "======================================================================\n\n";
}
void PrintYouWinGame()
{
//Prints welcome message to terminal
std::cout << "\n======================================================================\n";
std::cout << "| You successfully broke into all servers and collected all the |\n";
std::cout << "| necessary data. Please return to HQ to finalise your report. |\n";
std::cout << "| |\n";
std::cout << "| Thank you for your services. |\n";
std::cout << "| You are a true CODE-BREAKER!!! |\n";
std::cout << "======================================================================\n\n";
}
void PrintYouLooseLevel()
{
//Prints welcome message to terminal
std::cout << "\n======================================================================\n";
std::cout << "| Wrong code! Be careful to not trigger the alarm! |\n";
std::cout << "======================================================================\n\n";
}
void PrintYouLooseGame()
{
//Prints welcome message to terminal
std::cout << "\n======================================================================\n";
std::cout << "| You triggered the ALARM! |\n";
std::cout << "| |\n";
std::cout << "| They are coming for you.... |\n";
std::cout << "| |\n";
std::cout << "| All is lost!!! The world will end... |\n";
std::cout << "| |\n";
std::cout << "| ___ ___ _ _ ___ ___ ___ ___ |\n";
std::cout << "| | __ |___| | \\/ | |___ | | \\ / |___ |___| |\n";
std::cout << "| |___| | | | | |___ |___| \\\/ |___ | \\ |\n";
std::cout << "======================================================================\n\n";
}
bool PlayGame(int Difficulty, int MaxDifficulty)
{
PrintLevelIntroduction(Difficulty);
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 CodeSum and CodeProduct to the terminal
std::cout << "\n* It is a 3 digit code. Enter each digit separated by a space.\n";
std::cout << "* The code numbers add up to: " << CodeSum << "\n";
std::cout << "* The product of the code numbers equals: " << CodeProduct << "\n\n";
std::cout << "Enter the 3 digit code to unlock: " << "\n\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 code is correct
if(GuessSum == CodeSum && GuessProduct == CodeProduct)
{
if(Difficulty < MaxDifficulty)
{
PrintYouWinLevel(Difficulty);
return true;
}
if(Difficulty == MaxDifficulty)
{
return true;
}
}
}
int main()
{
srand(time(NULL));
int LevelDifficulty = 1;
int const MaxDifficulty = 5;
int MaxTry = 5;
PrintGameIntroduction();
while(LevelDifficulty <= MaxDifficulty && MaxTry > 0)
{
if (MaxTry != 0) // Loops the game until all the levels are completed
{
bool bLevelComplete = PlayGame(LevelDifficulty, MaxDifficulty);
std::cin.clear(); //Clears any erros
std::cin.ignore(); // Discards the buffer
if (bLevelComplete)
{
++LevelDifficulty;
MaxTry = 5;
}
else
{
--MaxTry;
std::cout << "\n\n ***** You have " << MaxTry << " tries left! ***** \n";
}
}
}
if (MaxTry == 0)
{
PrintYouLooseGame();
}
else
{
PrintYouWinGame();
}
return 0;
}
This is where I am as of lecture 29:
#include <iostream>
void PrintIntroduction(int Difficulty)
{
std::cout << "\n\n ____\n [____]\n |o |\n | |\n / o \\\n /______\\ TripleX\n / o \\\n / o \\\n '----------' \n";
std::cout << "You are a level " << Difficulty;
std::cout << " scientist experimenting with different ingredients...\nYou need to measure out the correct quantities to produce satisfactory results...\n\n";
}
bool PlayGame(int Difficulty)
{
PrintIntroduction(Difficulty);
const int CodeA = rand();
const int CodeB = rand();
const int CodeC = rand();
const int CodeSum = CodeA + CodeB + CodeC;
const int CodeProduct = CodeA * CodeB * CodeC;
// Print CodeSum and CodeProduct to the terminal
std::cout << "+ There are 3 ingredients you need to measure out";
std::cout << "\n+ The quantities add-up to: " << CodeSum;
std::cout << "\n+ The quantities 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 players guess is correct
if (GuessSum == CodeSum && GuessProduct == CodeProduct)
{
std::cout << "\nYou created a stable concotion, earning yourself a promotion!";
return true;
}
else
{
std::cout << "\nYour concoction blew up, no promotion for you... :^(";
return false;
}
}
int main()
{
int LevelDifficulty = 1;
const int MaxDifficulty = 5;
while (LevelDifficulty <= MaxDifficulty) // Loop game until all levels completed
{
bool bLevelComplete = PlayGame(LevelDifficulty);
std::cin.clear(); // Clears any errors
std::cin.ignore(); // Discards the buffer
if (bLevelComplete)
{
++LevelDifficulty;
}
}
std::cout << "\n\nCongratulations! You've become a master scientist!";
return 0;
}
I’m probably going to rewrite the actual printed text because I’m not quite satisfied with it yet.
Here’s how my code looks right now.
#include <iostream>
void PrintIntroduction(int Difficulty)
{
//Print welcome message on the screen
std::cout << "\n\nYou are a secret agent breaking into a level " << Difficulty;
std::cout << " server room...\nEnter the correct code to continue...\n\n";
}
bool PlayGame(int Difficulty)
{
PrintIntroduction(Difficulty);
//Declaring 3 numbers in the code
int CodeA = 4;
int CodeB = 3;
int CodeC = 2;
int CodeSum = CodeA + CodeB + CodeC;
int CodeProduct = CodeA * CodeB * CodeC;
//Print sum and product to the terminal
std::cout << "There are 3 numbers in the code" ;
std::cout << "\nThe codes add-up to: " << CodeSum;
std::cout << "\nThe codes multipled to: " << CodeProduct << std::endl;
int GuessA, GuessB, GuessC;
std::cin >> GuessA >> GuessB >> GuessC;
int GuessSum = GuessSum = GuessA + GuessB + GuessC;
int GuessProduct = GuessA * GuessB * GuessC;
//Check if the guess is correct
if (CodeSum == GuessSum && GuessProduct == CodeProduct)
{
std::cout << "\nYou guessed right and successfully completed this level!";
return true;
}
else
{
std::cout << "\nYou guessed wrong please try the level again!";
return false;
}
}
int main()
{
int LevelDifficulty = 1;
const int MaxDifficulty = 5;
while (LevelDifficulty <= MaxDifficulty){ //Loop
bool bLevelComplete = PlayGame(LevelDifficulty);
std::cin.clear();
std::cin.ignore();
if (bLevelComplete)
{
++LevelDifficulty;
}
}
std::cout << "\nCongratulations you have completed the game!";
return 0;
}
Good job looks great!
Awesome work people!
Not overly creative, but I am working on it.
#include
void PrintIntroduction(int Difficulty)
{
// Print welcome to terminal
std::cout << " ____ ____ ____ ____ ____ __ ____ _ " << std::endl;
std::cout << " ( )( _ \( )( _ \( _ \( ) ( ___)( \/ )" << std::endl;
std::cout << " )( ) / )( )/ )/ )( )) ) ( " << std::endl;
std::cout << " () ()\)(____)() (_) ()()(/\)" << std::endl;
std::cout << “\nYou are a secret agent breaking into a \n”;
std::cout << “server with multiple firewalls. The current\n”;
std::cout << "firewall is level " << Difficulty + 1 << “\n”;
std::cout << std::endl;
std::cout << “Enter the correct code to break past this firewall…\n”;
}
bool PlayGame(int Difficulty)
{
PrintIntroduction(Difficulty);
const int CodeA = 4;
const int CodeB = 3;
const int CodeC = 2;
/* Multi
Line
Comment */
const int CodeSum = CodeA + CodeB + CodeC;
const int CodeProduct = CodeA * CodeB * CodeC;
// Print sum/product to terminal
std::cout << std::endl;
std::cout <<"* 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 << "\n";
int GuessA, GuessB, GuessC;
// Store player guess
std::cout << "Please enter your 3 number guess separated by spaces\n";
std::cin >> GuessA >> GuessB >> GuessC;
std::cout << "You entered: \n" << GuessA << GuessB << GuessC << std::endl;
int GuessSum = GuessA + GuessB + GuessC;
int GuessProduct = GuessA * GuessB * GuessC;
// Compare guess to code. Print results to terminal
if (GuessSum == CodeSum && GuessProduct == CodeProduct)
{
std::cout << "\nYou got that code! Let's move on.\n";
return true;
}
else
{
std::cout << "\nYou did not choose the right code!\n";
std::cout << "Be more careful and try again!\n";
return false;
}
}
int main()
{
int LevelDifficulty = 0;
const int MaxLevel = 4;
while (LevelDifficulty <= MaxLevel) // Loop game until all levels completed
{
bool bLevelComplete = PlayGame(LevelDifficulty);
std::cin.clear(); // clears any erros
std::cin.ignore(); // discards the buffer
if (bLevelComplete)
{
++LevelDifficulty;
}
}
std::cout << "\nCongratuations! You broke through all of the firewalls.\n";
std::cout << "Get the files and get out of there!";
return 0;
}
#include
void PrintIntroduction(int Difficulty)
{
std::cout << " _ _ \n" << " ( )_ \n" << " ( )
) \n";
std::cout << " (_ (_ . _) _) \n";
std::cout << “–>Enemies have our secreat file.\n”;
std::cout << “–>You have the task to infilrate in to the enemy’s servers.\n”;
std::cout << “–>You are a secret agent breaking into a level " << Difficulty << " secure server room.\n\n\n”;
std::cout << “+ You have to access the system without others knowing your presence.\n”;
std::cout << “+ You need to enter the correct codes to continue…\n”;//What to do to pass the level
std::cout << “+ If the code entered is wrong, it will alert the enemy\n\n”;
}
bool PlayGame(int Difficulty)
{
PrintIntroduction(Difficulty);
// Declare the 3 numbers of code.
const int CodeA = 4;
const int CodeB = 3;
const int CodeC = 2;
const int CodeSum = CodeA + CodeB + CodeC;
const int CodeProduct = CodeA * CodeB * CodeC;
// Print hints to the terminal.
std::cout << "- According to sources the following information is known:\n";
std::cout << "- There are three numbers in the code.\n";
std::cout << "- The code add-up to: " << CodeSum ;
std::cout << "\n- The product of code's digits is: "<< CodeProduct ;
// Store plyer Guess.
int GuessA , GuessB , GuessC;
std::cout << "\n\n+ Enter the code: ";
std::cin >> GuessA >> GuessB >> GuessC;
int GuessSum = GuessA + GuessB + GuessC;
int GuessProduct = GuessA * GuessB * GuessC;
// Checks if the players guess is correct.
if ( GuessSum == CodeSum && GuessProduct == CodeProduct )
{
std::cout << "\nWell done!! You cleared the level.\n" << std::endl;
return true;
}
else
{
std::cout << "\n The enemies are alerted!! Try again.\n" << std::endl;
return false;
}
}
int main() // The control function.
{
int LevelDifficulty = 1;
const int MaxDifficulty = 5;
while ( LevelDifficulty <= MaxDifficulty ) // Loop until
{
bool bLevelComplete = PlayGame(LevelDifficulty);
std::cin.clear(); // Clear any error.
std::cin.ignore(); // Discard the buffer.
if (bLevelComplete)
{
++LevelDifficulty;
}
}
std::cout << "\n\n You cleared all the levels and injected the virus in the enemy servers\n\n MISSION ACCOMPLISHED\n\n";
return 0;
}
#include
void PrintIntroduction (int Difficulty)
{
// ASCI art here
std ::cout << R"(
, ,
,-{-
/
,-~ , \ {-~~-,
,~ , ,,-~~-,
,
,, { { } } }/ ; ,--/
\ \ / / }/ /,/
; ,-./ \ \ { { ( /,; ,/ ,/
; / } }
, -
-.___ / , ,/
,/
| ,,
~.___,---} / ,
,/ ,,;
{ { __ / ,/ ,
,;
/ \ \ _,,
{ ,{
,;
{ } } /~\ .-:::-. (–, ;\ ,}
,; \\._./ / /
, \ ,:::::::::, ~; \},/
,; ,-=-
-…-/.
._ ;:::::::::::; ,{ /
,; { / , ~ . ^
~\:::::::::::<<~>-,,
, -, ``,_ } /~~ .
. ~ , .~~\:::::::; _-~ ;__,
,-/
\ /~, . ~ , ’ , .
::::;<<<~``` ``-,,__ ; /
.\ /
. ^ , ~ , . . ~\~ \\,
,
/ , ,
. ~ , ^ ,
~ . . ~~~`, `-`--, \ / , ~ . ~ \ , ` . ^ ` , . ^ . , ` .`-,___,---,__
/ . ~ .
\
~ , . ,
, . ~ ^ , . ~ , .~---,___ /
. , . ~ , \
~ , . ^ , ~ . , ~ . ^ , ~ .
-,
-Daniel Hunt-)" << '\n';
std::cout << std::endl;
//story of the game below
std::cout << “The code to the secret genetic enhancer mechanism was hidden in the safe…\n”;
std::cout << “It was the most secure safe of that era, it required an unprecedented effort to crack this code…\n”;
std::cout << “a team of 15 people attempted to open the safe, unfortunately the security mechanism killed the majority of them…\n”;
std::cout << “it was left to one individual to make one last attempt to open the safe…\n”;
std::cout << "You need to enter the correct level “<< Difficulty;
std::cout << " codes to continue…\n”;
//story ends here
}
bool PlayGame (int Difficulty)
{
PrintIntroduction (Difficulty);
//declare 3 number code
const int CodeA = rand();
const int CodeB = rand();
const int CodeC = rand();
/*
This is
a multi-line
coment
*/
const int CodeSum = CodeA + CodeB + CodeC;
const int CodeProduct = CodeA * CodeB * CodeC;
// Print sum and product to the terminal
std::cout << std::endl;
std::cout << "There are 3 numbers in the code\n";
std::cout << "\nThe codes add up to: " << CodeSum;
std::cout << "\nThe codes multiply to give: " << CodeProduct;
std::cout << std::endl;
// Store player Guess
int GuessA, GuessB, GuessC;
std::cin >> GuessA >> GuessB >> GuessC;
// Check if players guess in correct
int GuessSum = GuessA + GuessB + GuessC;
int GuessProduct = GuessA * GuessB * GuessC;
if (GuessSum == CodeSum && GuessProduct == CodeProduct)
{
std::cout << "You have entered the correct code, try at the next level";
return true;
}
else
{
std::cout << "\nThis is the wrong code, the facility will now self destruct...\n";
std::cout << "But I will resurrect you to try again";
return false;
}
}
int main()
{
int LevelDifficulty = 1;
int const MaxLevel = 5;
while (LevelDifficulty <= MaxLevel) // Loop game until all levels completed
{
bool bLevelComplete = PlayGame(LevelDifficulty);
std::cin.clear(); //Clears any errors
std::cin.ignore(); // Discards the buffer
if (bLevelComplete)
{
++LevelDifficulty;
}
}
std::cout << "\nCongratulations you have opened the safe!";
return 0;[quote="Gavin_Milroy, post:1, topic:104625, full:true"]
Well done for making it this far into the section!
How is the code for your game looking so far?
Post below!
[/quote]
Current progress
}
I added a system to mine that tells you how many levels are left, rather than just telling you which level you’re on, it took me way longer than I thought it would because the grammar never sounded right, it still isn’t perfect, but it works. Also I’m not sure how to fix the fact that whenever I copy/paste code from VS it only loads certain parts of it in the code format…
#include
void PrintIntroduction(int Difficulty, int MaxLevel, int LevelsUntilMax)
{
if(LevelsUntilMax >= 1)
{
std::cout << “[#] You have been taken by an unknown alien species to a space lab designed for human testing, you must escape if you want to survive, there are a total of 5 doors you must pass by \nentering the correct code into the lock, after all 5 you will reach the escape pods, hop in one of them and you’re home free.”;
std::cout << "\n[#] You are on door " << Difficulty << “, only " << LevelsUntilMax << " to go.”;
}
else
{
std::cout << “[#] You have been taken by an unknown alien species to a space lab designed for human testing, you must escape if you want to survive, there are a total of 5 doors you must pass by \nentering the correct code into the lock, after all 5 you will reach the escape pods, hop in one of them and you’re home free.”;
std::cout << "\n[#] You are on door " << Difficulty << “, only 1 to go”;
}
}
bool PlayGame(int Difficulty, int MaxLevel, int LevelsUntilMax)
{
PrintIntroduction(Difficulty, MaxLevel, LevelsUntilMax);
//Declare 3 number code
const int CodeA = 4;
const int CodeB = 7;
const int CodeC = 2;
const int CodeSum = CodeA + CodeB + CodeC;
const int CodeProduct = CodeA * CodeB * CodeC;
//Print sum and product to terminal
std::cout << std::endl;
std::cout << "[!] There are 3 numbers in the code";
std::cout << "\n[!] The numbers add up to: " << CodeSum;
std::cout << "\n[!] The numbers multilpy to: " << 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's guess is correct
if(GuessSum == CodeSum)
{
std::cout << "\nThe light on the lock blinks green\n\n";
return true;
}
else
{
std::cout << "\nThe light on the lock blinks red, try again.\n\n";
return false;
}
}
int main()
{
int LevelDifficulty = 1;
//Maximum game level
int const MaxLevel = 5; /LevelDifficulty and MaxLevel are passed into the occurance of PlayGame in the while loop, then passed
up to PlayGame and then down to PrintIntroduction so it can be used within that/
while(LevelDifficulty <= MaxLevel) //Loop game until all levels are complete
{
int LevelsUntilMax = MaxLevel - LevelDifficulty + 1;
bool bLevelComplete = PlayGame(LevelDifficulty, MaxLevel, LevelsUntilMax);
std::cin.clear(); //Clears any errors
std::cin.ignore(); //Discards the buffer
if (bLevelComplete)
{
++LevelDifficulty;
}
}
std::cout << "You have escaped, Earth is waiting for you!";
return 0;
}
//Title: tripleXGame.cpp
//Course: Udemy Unreal Engine C++ Developer
//Author: Ryn Ellis
//Date: 2019-05-17
//Description: Code guessing game
//Libraries
#include <iostream>
#include <ctime>
#include <stdlib.h>
using namespace std;
//Function Definitions
void Opening(); //Title and story message function
int Calculations(int first, int second, int third); //Calculations for code riddle function
int PlayerCalculations(int first, int second, int third); //Calculations for code riddle function
int PlayerInput(); //Player code input function
void Boom(); //Game ending if wrong code guessed
void Win(); //Game ending if all the codes are guessed correctly
int Comparison(int codeSum, int playerSum, int codeProduct, int playerProduct); //Comparison function to compare sums and products
bool PlayGame(); //Actual game play function
bool ReplayGame(); //To ask player if they would like to play again
void LevelIncrease(); //To increase the level of difficulty if player gets the correct code
int RandomNumberGenerator();
//Variables and Arrays
//Level Variables
int Difficulty = 1;
const int MaxDifficulty = 4;
bool bPlay = true;
//Code Number Array
int CodeNumber[3] = { 0, 0, 0 };
int Interval = 0;
//Mathematical Variables
int SumCodeTotal = 0;
int ProductCodeTotal = 0;
int PlayerSum = 0;
int PlayerProduct = 0;
//Player Information
int PlayersGuess[3] = { 0, 0, 0 };
//Main Program
int main()
{
while (bPlay == true)
{
PlayGame();
std::cin.clear(); //Clears any errors
std::cin.ignore(); //Discards the buffer
}
return 0;
}
//Functions
void Opening()
{
//This function prints the title of the game and the story behind the game.
system("CLS");
std::cout << " _________ _______ _________ _______ _ _______ " << std::endl;
std::cout << " \\__ __/( ____ )\\__ __/( ____ )( \\ ( ____ \\ |\\ /|" << std::endl;
std::cout << " ) ( | ( )| ) ( | ( )|| ( | ( \\/ ( \\ / )" << std::endl;
std::cout << " | | | (____)| | | | (____)|| | | (__ \\ (_) / " << std::endl;
std::cout << " | | | __) | | | _____)| | | __) ) _ ( " << std::endl;
std::cout << " | | | (\\ ( | | | ( | | | ( / ( ) \\ " << std::endl;
std::cout << " | | | ) \\ \\_____) (___| ) | (____/\\| (____/\\ ( / \\ )" << std::endl;
std::cout << " )_( |/ \\__/\\_______/|/ (_______/(_______/ |/ \\|" << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << " *************************************************************************************************" << std::endl;
std::cout << " * You are a rookie bomb defuser. There is a crisis in the building with many bombs to defuse. *" << std::endl;
std::cout << " * You need to enter the correct codes on the devices in order to defuse them! *" << std::endl;
std::cout << " * Hurry before time runs out! *" << std::endl;
std::cout << " * *" << std::endl;
std::cout << " * There are three numbers in the code in order to defuse the bomb. *" << std::endl;
std::cout << " * Player Difficulty Level " << Difficulty << " *" << std::endl;
std::cout << " * *" << std::endl;
while (Interval < 2)
{
CodeNumber[Interval] = RandomNumberGenerator();
++Interval;
}
Calculations(CodeNumber[0], CodeNumber[1], CodeNumber[2]);
std::cout << " * The sum of the three numbers equal " << SumCodeTotal << ". *" << std::endl;
std::cout << " * The product of the three numbers equal " << ProductCodeTotal << ". *" << std::endl;
std::cout << " * *" << std::endl;
std::cout << " * You only have one chance to get this right and save everyone! *" << std::endl;
std::cout << " *************************************************************************************************" << std::endl;
std::cout << std::endl;
}
int Calculations(int First, int Second, int Third)
{
//This function calculates the sum and the product of the three numbers for the code sequence
int Sum = First + Second + Third;
SumCodeTotal = Sum;
int Product = First * Second * Third;
ProductCodeTotal = Product;
return 0;
}
int PlayerInput()
{
//This function collects the player's guess
int I = 0; //Used for incrementing through the array
int Input = 0;
std::cout << " What is the code to defuse the bomb? ";
while (I< 3)
{
std::cin >> PlayersGuess[I];
I++;
}
PlayerCalculations(PlayersGuess[0], PlayersGuess[1], PlayersGuess[2]);
return 0;
}
void Boom()
{
//This function is for when the player guesses the wrong code
system("CLS");
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << " ======== ==== ==== ==== ====" << std::endl;
std::cout << " || \\\\ // \\\\ // \\\\ || \\ / ||" << std::endl;
std::cout << " ||_____// || || || || || V ||" << std::endl;
std::cout << " ||~~~~~\\\\ || || || || || ||" << std::endl;
std::cout << " || // \\\\ // \\\\ // || ||" << std::endl;
std::cout << " ======== ==== ==== == ==" << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << " You guessed wrong........." << std::endl;
std::cout << " You just killed yourself and everyone else in a five block radius." << std::endl;
bPlay = ReplayGame();
}
void Win()
{
//This function is for when the player guesses all the codes correctly
system("CLS");
std::cout << " |\\ /|\\__ __/( ( /|( ( /|( ____ \\( ____ )" << std::endl;
std::cout << " | ) ( | ) ( | \\ ( || \\ ( || ( \\/| ( )| " << std::endl;
std::cout << " | | _ | | | | | \\ | || \\ | || (__ | (____)| " << std::endl;
std::cout << " | |( )| | | | | (\\ \\) || (\\ \\) || __) | __)" << std::endl;
std::cout << " | || || | | | | | \\ || | \\ || ( | (\\ (" << std::endl;
std::cout << " | () () |___) (___| ) \\ || ) \\ || (____/\\| ) \\ \\__" << std::endl;
std::cout << " (_______)\\_______/|/ )_)|/ )_)(_______/|/ \\__/ " << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << " *************************************************************************" << std::endl;
std::cout << " * Congradulations Rookie! *" << std::endl;
std::cout << " * You have successfuly defused all the bombs and saved everyone! *" << std::endl;
std::cout << " * You will definitely be getting a promotion and raise for this. *" << std::endl;
std::cout << " *************************************************************************" << std::endl;
std::cout << std::endl;
bPlay = ReplayGame();
}
int PlayerCalculations(int First, int Second, int Third)
{
//This function calculates the player sum and the player product of the three numbers for the code sequence
int Sum = First + Second + Third;
PlayerSum = Sum;
int Product = First * Second * Third;
PlayerProduct = Product;
return 0;
}
int Comparison(int CodeSum, int PlayerSum, int CodeProduct, int PlayerProduct)
{
if (CodeSum == PlayerSum && CodeProduct == PlayerProduct)
{
LevelIncrease();
}
else
{
Boom();
}
return 0;
}
bool PlayGame()
{
Opening();
PlayerInput();
Comparison(SumCodeTotal, ProductCodeTotal, PlayerSum, PlayerProduct);
return true;
}
bool ReplayGame()
{
bool bPlayersChoice = true;
char PlayerReplay = 0;
std::cout << std::endl;
std::cout << " Would you like to play again? ";
std::cin >> PlayerReplay;
if ((PlayerReplay == 89) || (PlayerReplay == 121))
{
bPlayersChoice = true;
Difficulty = 1;
}
else
{
bPlayersChoice = false;
}
return bPlayersChoice;
}
void LevelIncrease()
{
if (Difficulty == MaxDifficulty)
{
Win();
}
else
{
++Difficulty;
}
}
int RandomNumberGenerator()
{
int Number = rand();
return Number;
}
Reached the end of the video, here is my code so far:
#include <iostream>
void PrintIntroduction(int CodeProduct, int CodeSum, int LevelDifficulty)
{
//print introduction/menu to the user
std::cout << "_________ _______ _________ _______ _ _______ \n";
std::cout << "\\__ __/( ____ )\\__ __/( ____ )( \\ ( ____ \\ |\\ /|\n";
std::cout << " ) ( | ( )| ) ( | ( )|| ( | ( \\/ ( \\ / )\n";
std::cout << " | | | (____)| | | | (____)|| | | (__ \\ (_) / \n";
std::cout << " | | | __) | | | _____)| | | __) ) _ ( \n";
std::cout << " | | | (\\ ( | | | ( | | | ( / ( ) \\ \n";
std::cout << " | | | ) \\ \\_____) (___| ) | (____/\\| (____/\\ ( / \\ )\n";
std::cout << " )_( |/ \\__/\\_______/|/ (_______/(_______/ |/ \\|\n\n";
std::cout << "You're a whistle-blower hacking into the CEO's computer,\n";
std::cout << "hack the LEVEL " << LevelDifficulty << " security code to retrieve the evidence...\n\n";
std::cout << "Here's the information you know about the security codes...\n";
std::cout << " * There are three numbers in the codes\n";
std::cout << " * The codes multiply to give " << CodeProduct << "\n";
std::cout << " * The codes add-up to " << CodeSum << "\n\n";
std::cout << "Enter the correct code to continue.\n";
}
bool PlayGame(int LevelDifficulty)
{
//declare int variables to use for the three numbered code
const int CodeA = rand();
const int CodeB = rand();
const int CodeC = rand();
//declare int variables to hold sum, prod, div, and mod calculations
const int CodeSum = CodeA + CodeB + CodeC;
const int CodeProduct = CodeA * CodeB * CodeC;
PrintIntroduction(CodeProduct, CodeSum, LevelDifficulty);
//store player gues
int GuessA, GuessB, GuessC;
std::cin >> GuessA >> GuessB >> GuessC;
int GuessSum = GuessA + GuessB + GuessC;
int GuessProduct = GuessA * GuessB * GuessC;
//check if the players guess is correct
if ((GuessSum == CodeSum) && (GuessProduct == CodeProduct))
{
std::cout << "****Success! Continue on to the next level!****\n";
return true;
}
else
{
std::cout << "**** Wrong code! Try again, you can do this!****\n";
return false;
}
}
int main()
{
int LevelDifficulty = 1;
const int MaxLevel = 5;
while (LevelDifficulty <= MaxLevel) //loop the game 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 << "Amazing job, you made it through all of the security levels! Now download the files and get out quick! This guy is going down!\n";
return 0;
}
Here is my code so far as of lesson 31
I am still kind of changing up the story as I go… New stories come and go in my brain, this one is a simple throwback to my childhood but modified to be a much shorter storyline
#include <iostream>
#include <ctime>
void PrintMainIntroduction()
{
// Print intro message to terminal
std::cout << "\nMaster Aqua, \n";
std::cout << "\nI am entrusting you with a mission...\n";
std::cout << "\nYou must repair Kingdom Hearts... Xheanort is doing everything in his power to stop us from our goal\n";
std::cout << "We will defeat him, but only if we unlock the keyblades true power.\n";
std::cout << "\nYou must figure out the individual 3-digit kingdom codes, there are 5...\n";
}
void PrintLevelIntroduction(int Difficulty)
{
std::cout << "\n\nUnlock Kingdom # " << Difficulty << std::endl;
}
bool PlayGame(int Difficulty)
{
PrintLevelIntroduction(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 the CodeSum and CodeProduct to terminal
std::cout << "+ There are 3-digits in this particular kingdom code";
std::cout << "\n+ The 3-digits add-up to: " << CodeSum;
std::cout << "\n+ The 3-digits multiply to give: " << CodeProduct << std::endl;
// Store player guess'
int GuessA, GuessB, GuessC;
std::cout << "[1] ";
std::cin >> GuessA;
std::cout << "[2] ";
std::cin >> GuessB;
std::cout << "[3] ";
std::cin >> GuessC;
int GuessSum = GuessA + GuessB + GuessC;
int GuessProduct = GuessA * GuessB * GuessC;
// Determines if the player guessed correctly
if (GuessSum == CodeSum && GuessProduct == CodeProduct)
{
std::cout << "\nYou have gained access to kingdom number " << Difficulty;
return true;
}
else
{
std::cout << "\nIncorrect! Xheanort draws near... You are now sad :( \nTry again...";
return false;
}
}
int main()
{
PrintMainIntroduction();
srand(time(NULL)); // Create new random sequence based on time of day
int LevelDifficulty = 1;
const int MaxDifficulty = 5;
while (LevelDifficulty <= MaxDifficulty) // Loop game until all levels have been completed
{
bool bLevelComplete = PlayGame(LevelDifficulty);
std::cin.clear(); // Clears any errors
std::cin.ignore(); // Discard the buffer
if (bLevelComplete)
{
++LevelDifficulty;
}
}
std::cout << "\n\nWell done Aqua! You have collected all the kingdom codes and have saved Kingdom Hearts.\n";
return 0;
}
Hey there! My code so far!
#include <iostream>
void PrintIntroduction(int Difficulty)
{
// Print welcome messages to the terminal
std::cout << "You're the mighty Hero of Tyria who tries to get access to a level " << Difficulty << " vault of Balthazar!\n";
std::cout << "Enter the correct codes to break the Seal of Determination..\n\n";
}
bool PlayGame(int Difficulty)
{
PrintIntroduction(Difficulty);
// Declare 3 number code
const int CodeA = rand();
const int CodeB = rand();
const int CodeC = rand();
const int CodeSum = CodeA + CodeB + CodeC;
const int CodeProduct = CodeA * CodeB * CodeC;
// Print sum and product to the terminal
std::cout << "+ There are 3 numbers in the code for the seal\n";
std::cout << "+ The codes add-up to: " << CodeSum << "\n";
std::cout << "+ The codes multiply to give: " << CodeProduct << "\n\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 players guess is correct
if(GuessSum == CodeSum && GuessProduct == CodeProduct)
{
std::cout << "*** You broke the seal, hero! But.. there are more seals to break before you can loot the treasure! ***\n\n";
return true;
}
else
{
std::cout << "*** Muahaha.. you're not worthy to the vault! Try harder! ***\n\n";
return false;
}
}
int main()
{
int LevelDifficulty = 1;
int const MaxLevelDifficulty = 5;
while(LevelDifficulty <= MaxLevelDifficulty) // Loop the game until all levels completed
{
bool bLevelComplete = PlayGame(LevelDifficulty);
std::cin.clear();
std::cin.ignore();
if(bLevelComplete)
{
++LevelDifficulty;
}
}
std::cout << "*** YOU DID IT! Get all the loot, drink all the booze!! ***\n";
return 0;
}
Great Job!
Code so far:
#include <iostream>
void PrintIntroduction(int Difficulty)
{
std::cout << "\n\n _=,_\n";
std::cout << " o_/6 /# |\n";
std::cout << " \\__ |##/ PUPPY 911\n";
std::cout << " ='|-- |\n";
std::cout << " / #'-.\n";
std::cout << " \\#|_ _'-. /\n";
std::cout << " |/ \\_( # |'' \n";
std::cout << " C/ ,--___/\n";
//prints initial story text to terminal
std::cout << "\n*****\n";
std::cout << "You work at Puppy 911 Emergency Services. You receive an EMERGENCY ALERT.\n";
std::cout << "A puppy's beloved chewing shoe -- which he's definitely allowed to chew on -- has been locked away!\n";
std::cout << "You must CRACK A LEVEL " << Difficulty << " SECURITY CODE.\n";
}
bool PlayGame(int Difficulty)
{
//Prints intro scene
PrintIntroduction(Difficulty);
//declares rando three-number code
const int CodeA = rand();
const int CodeB = rand();
const int CodeC = rand();
//declares three-number code sum and product
const int CodeSum = CodeA + CodeB + CodeC;
const int CodeProduct = CodeA * CodeB * CodeC;
//prints sum and product to the terminal
std::cout << "\n+ There are three numbers in the code.\n";
std::cout << "+ The numbers add up to " << CodeSum << ".\n";
std::cout << "+ The numbers multiply to give " << CodeProduct << ".\n";
std::cout << "*****\n";
//declares player guess variables
int GuessA, GuessB, GuessC;
//intakes and prints player guess
std::cin >> GuessA >> GuessB >> GuessC;
std::cout << "\nYou entered: " << GuessA << GuessB << GuessC << ".";
//declares guess sum and guess product
int GuessSum = GuessA + GuessB + GuessC;
int GuessProduct = GuessA * GuessB * GuessC;
//prints string if player guesses right or wrong
if(GuessSum == CodeSum && GuessProduct == CodeProduct)
{
std::cout << "\n*Click*\nYou got the shoe! Crisis averted -- wait. \nYou're getting another emergency call.\nAnother puppy's shoe has been locked up across town. You rush to the scene.\n";
return true;
}
else
{
std::cout << "\nIt didn't work! The puppy whines. Let's try again!\n";
return false;
}
}
int main()
{
int LevelDifficulty = 1;
const int MaxDifficulty = 5;
while(LevelDifficulty <= MaxDifficulty) // loops game until all levels completed
{
bool bLevelComplete = PlayGame(LevelDifficulty);
std::cin.clear(); //clears errors
std::cin.ignore(); //discards buffer
if (bLevelComplete)
{
++LevelDifficulty;
}
}
std::cout << "\n\n\nAll puppies in the area are chewing happily on their shoes!\nGreat work, operator. You can clock out.\n...until tomorrow, anyway.\n";
return 0;
}
Nice!