#include <iostream>
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 PrintYouLoseGame()
{
std::cout << "======================================================================\n\n";
std::cout << "| |\n";
std::cout << "| ___ ___ _ _ ___ ___ ___ ___ |\n";
std::cout << "| | __ |___| | \\/ | |___ | | \\ / |___ |___| |\n";
std::cout << "| |___| | | | | |___ |___| \\\/ |___ | \\ |\n";
std::cout << "======================================================================\n\n";
}
void PrintIntroduction(int Difficulty)
{
std::cout <<"\n\nAre you secret agent breaking into a level " << Difficulty;
std::cout <<" secure server room..\nEnter the correct code to coninue..\n\n";
}
bool PlayGame(int Difficulty,int MaxDifficulty)
{
PrintIntroduction(Difficulty);
int CodeA = rand();
int CodeB = rand();
int CodeC = rand();
int CodeSum = CodeA + CodeB + CodeC;
int CodeProduct = CodeA * CodeB * CodeC;
//Print CodeSum and CodeProduct to the terminal
std::cout << "+ There are 3 number in the code";
std::cout << "\n+ The codes add-up to: " << CodeSum;
std::cout << "\n+ The codes multyply 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 is correct
if(GuessSum == CodeSum && GuessProduct == CodeProduct)
{
std::cout << "\n*** You win!\nLevel completed!!!Keep going ***";
return true;
}
else
{
std::cout << "\n*** You entered the wrong code! Careful agent..\nTry again!! ***";
return false;
}
}
int main()
{
int LevelDifficulty = 1;
int const MaxDifficulty = 9;
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*** Great work agent! You got all the files! Now get out of there!! ***";
return 0;
}```
#include <iostream>
void PrintIntroduction(int Difficulty)
{
//Print intro dialogue to the terminal
std::cout << "\nYou wake up in a dank cellar and see a frail old man.\n";
std::cout << "\"You must complete our tests if you wish to escape this cursed spire.\"\n";
std::cout << "The level of difficulty for this floor is " << Difficulty << std::endl;
}
bool PlayGame(int Difficulty)
{
PrintIntroduction(Difficulty);
//Declaring 3 numbers
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 << "\n+ There are three numbers in each test,\n";
std::cout << " you must discover each of them before moving on to the next floor.\n";
std::cout << "+ When all three are combined, they add up to: " << CodeSum;
std::cout << "\n+ When the numbers are multiplied together they equal: " << 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 << "\n\n*** \"You are correct and may move up the staircase,\n";
std::cout << "There, you will find your next challenge.\" ***\n";
return true;
}
else
{
std::cout << "\n\n*** You are incorrect you fool, try again... ***\n";
return false;
}
}
int main()
{
int LevelDifficulty = 1;
int const MaxDifficulty = 5;
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;
}
}
std::cout << "\n*** Congratulations, youve made it out of the spire. ***";
return 0;
}
I went on a bit of a terminal ANSI color spree.
#include <iostream>
#include <string>
#include <array>
#include <chrono>
#include <random>
#if WIN32
#include <windows.h>
bool EnableAnsiEscapes() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if(hConsole != INVALID_HANDLE_VALUE) {
DWORD currentMode = 0;
if(GetConsoleMode(hConsole, ¤tMode)) {
SetConsoleMode(hConsole, currentMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
}
}
// Used to return false on error, but that breaks support for terminals like MINGW, so if the Windows version
// does not support ansi escape codes, ANSI support will not be disabled, so ANSI escapes will still be printed.
return true;
}
#else
bool EnableAnsiEscapes() { return true; }
#endif //WIN32
bool ansiSupported = true;
std::mt19937 random;
namespace Ansi {
inline std::string Reset() {
if (!ansiSupported)
return "";
return "\033[0m";
}
inline std::string Escape(const std::string& flag, const std::string& val) {
if (!ansiSupported)
return "";
return "\x1B[" + flag + val + "m";
}
std::string Clear() {
if (!ansiSupported)
return "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
// Clear whole screen and reset cursor to 0, 0
return Reset() + "\x1B[2J\x1B[0;0H";
}
std::string Bg(int val) {
return Escape("48;5;", std::to_string(val));
}
std::string Fg(int val) {
return Escape("38;5;", std::to_string(val));
}
}
long GetTime() {
auto now = std::chrono::system_clock::now();
return std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
}
void DrawCorrect() {
auto start = GetTime();
bool redraw = true;
bool invert = false;
auto last = GetTime();
while (GetTime() - start < 1500) {
if (GetTime() - last > 500) {
invert = !invert;
redraw = true;
}
if (redraw) {
std::cout << Ansi::Clear();
if (!invert)
std::cout << Ansi::Fg(2);
else
std::cout << Ansi::Fg(0) << Ansi::Bg(2);
std::cout << R"(/=======================================\)" << "\n"
<< R"(| _____ _ |)" << "\n"
<< R"(| / ____| | | |)" << "\n"
<< R"(| | | ___ _ __ _ __ ___ ___| |_ |)" << "\n"
<< R"(| | | / _ \| '__| '__/ _ \/ __| __| |)" << "\n"
<< R"(| | |___| (_) | | | | | __/ (__| |_ |)" << "\n"
<< R"(| \_____\___/|_| |_| \___|\___|\__| |)" << "\n"
<< R"(| |)" << "\n"
<< R"(\=======================================/)" << Ansi::Reset() << std::endl;
last = GetTime();
redraw = false;
}
}
}
void DrawWrong(std::array<int, 3> numbers) {
std::string correctNumbers;
for(int num : numbers) {
if(!correctNumbers.empty())
correctNumbers += ", ";
correctNumbers += std::to_string(num);
}
auto start = GetTime();
bool redraw = true;
bool invert = false;
auto last = GetTime();
while (GetTime() - start < 2500) {
if (GetTime() - last > 500) {
invert = !invert;
redraw = true;
}
if (redraw) {
std::cout << Ansi::Clear();
std::cout << "The correct numbers were: " << correctNumbers << "\n\n";
if (!invert)
std::cout << Ansi::Fg(1);
else
std::cout << Ansi::Fg(0) << Ansi::Bg(1);
std::cout << R"(/======================================\)" << "\n"
<< R"(| __ __ |)" << "\n"
<< R"(| \ \ / / |)" << "\n"
<< R"(| \ \ /\ / / __ ___ _ __ __ _ |)" << "\n"
<< R"(| \ \/ \/ / '__/ _ \| '_ \ / _` | |)" << "\n"
<< R"(| \ /\ /| | | (_) | | | | (_| | |)" << "\n"
<< R"(| \/ \/ |_| \___/|_| |_|\__, | |)" << "\n"
<< R"(| __/ | |)" << "\n"
<< R"(\======================================/)" << Ansi::Reset() << std::endl;
last = GetTime();
redraw = false;
}
}
}
bool SimulateGame(int level, int lives) {
std::cout << Ansi::Fg(3) << "You are in a bank trying to break into a " << Ansi::Fg(5) << "level " << level
<< Ansi::Fg(3) << " vault... \n"
<< Ansi::Fg(3) << "You have " << Ansi::Fg(5) << lives << Ansi::Fg(3) << " lives remaining.\n"
<< "Enter the correct code to continue..." << Ansi::Reset() << "\n\n";
// Generates a random distribution based on level and how many lives the player lost
std::uniform_int_distribution<int> distrib(level + (level - 1), level * 2 - ((3 - lives) / 3));
const std::array<int, 3> numbers{distrib(random), distrib(random), distrib(random)};
const int sum = numbers[0] + numbers[1] + numbers[2];
const int product = numbers[0] * numbers[1] * numbers[2];
std::cout << Ansi::Fg(3) << "* There are three numbers in the code\n"
<< "* The codes add-up to: " << Ansi::Fg(5) << sum << Ansi::Fg(3) << "\n"
<< "* The codes multiply to give: " << Ansi::Fg(5) << product << Ansi::Fg(3) << "\n"
<< "\nPlease enter your guess: " << Ansi::Fg(5);
std::flush(std::cout);
std::array<int, 3> guesses{};
for (int& guess : guesses) {
std::cin >> guess;
std::cin.clear(); // Clear any potential errors
}
const int guessSum = guesses[0] + guesses[1] + guesses[2];
const int guessProduct = guesses[0] * guesses[1] * guesses[2];
bool wasCorrect = false;
std::cout << "\n";
if (sum == guessSum && product == guessProduct) {
DrawCorrect();
wasCorrect = true;
} else {
DrawWrong(numbers);
}
std::cout << Ansi::Reset() << std::endl;
return wasCorrect;
}
int main() {
#if WIN32
ansiSupported = EnableAnsiEscapes();
#endif //WIN32
// Initialize randomizer
random = std::mt19937(GetTime());
constexpr int maxLevel = 10;
int level = 1;
int lives = 3;
while (level <= maxLevel) {
std::cout << Ansi::Clear();
bool correct = SimulateGame(level, lives);
std::cin.ignore(std::cin.rdbuf()->in_avail());
std::cout << Ansi::Clear();
if (correct)
level++;
else {
lives--;
}
if (lives <= 0) {
std::cout << Ansi::Fg(1) << "The police have found you, game over!";
break;
}
}
if (level > maxLevel)
std::cout << Ansi::Fg(2) << "You have successfully opened the last vault, the riches are yours!";
// Always reset the console formatting at the end, some consoles don't do it for us
// looking at you powershell
std::cout << Ansi::Reset() << std::endl;
std::cin.clear();
std::cin.ignore(std::cin.rdbuf()->in_avail());
std::cout << "\nPress enter to exit...";
std::cin.get(); // Prevent terminal from closing until player presses enter
return 0;
}
Here’s my code so far. I used a for loop for my code summing/multiplication but functionally its is the same. I also moved the message printed on completion to be inside of the PlayGame function in order for a different message to be printed to the user as they no longer need to proceed to the next level as the game has finished
#include <iostream>
// Method for finding the sum of an input array of integers
int Sum(int inputs[]) {
int n, InputSum=0;
for ( n=0 ; n<3 ; ++n )
{
InputSum += inputs[n];
}
return InputSum;
}
// Method for finding the product of an input array of integers
int Product(int inputs[]) {
int m, InputProduct=1;
for ( m=0 ; m<3 ; ++m )
{
InputProduct *= inputs[m];
}
return InputProduct;
}
// Print welcome messages to the terminal
void PlayIntroduction(int Difficulty)
{
std::cout << R"(
_______ ___ ___ _________ ________ ________ ________ _________ _______ ________
|\ ___ \ |\ \ / /|\___ ___\\ __ \|\ __ \|\ ____\\___ ___\\ ___ \ |\ ___ \
\ \ __/| \ \ \/ / ||___ \ \_\ \ \|\ \ \ \|\ \ \ \___\|___ \ \_\ \ __/|\ \ \_|\ \
\ \ \_|/__ \ \ / / \ \ \ \ \ _ _\ \ __ \ \ \ \ \ \ \ \ \_|/_\ \ \ \\ \
\ \ \_|\ \ / \/ \ \ \ \ \ \\ \\ \ \ \ \ \ \____ \ \ \ \ \ \_|\ \ \ \_\\ \
\ \_______\/ /\ \ \ \__\ \ \__\\ _\\ \__\ \__\ \_______\ \ \__\ \ \_______\ \_______\
\|_______/__/ /\ __\ \|__| \|__|\|__|\|__|\|__|\|_______| \|__| \|_______|\|_______|
|__|/ \|__|
)" << '\n';
std::cout << "You awake as a woman with no name or memory.\n";
std::cout << "The room you are in is bare and only the colour white.\n";
std::cout << "There is a door with a keypad and screen. It looks like a code must be entered to open it.\n";
std::cout << "The current difficulty is " << Difficulty;
}
bool PlayGame(int Difficulty, int MaxDifficulty)
{
PlayIntroduction(Difficulty);
// Declare 3 number code
const int CodeA = rand();
const int CodeB = rand();
const int CodeC = rand();
int CodeInputs [] = {CodeA, CodeB, CodeC};
int CodeSum = Sum(CodeInputs);
int CodeProduct = Product(CodeInputs);
// Print sum and product to the terminal
std::cout << std::endl;
std::cout << "\n\n+ There are " << (sizeof(CodeInputs)/sizeof(*CodeInputs)) << " 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;
std::cout << "You entered: " << GuessA << GuessB << GuessC << std::endl;
int GuessInputs [] = {GuessA, GuessB, GuessC};
int GuessSum = Sum(GuessInputs);
int GuessProduct = Product(GuessInputs);
// Check if the players guess is correct
if ((GuessSum == CodeSum) && (GuessProduct == CodeProduct))
{
if (Difficulty == MaxDifficulty)
{
std::cout << "\nThe door opens and you exit the facility."; // single quotes are used only for single characters, while double quotes are used for string literals
std::cout << "You are free, still unsure of where or who you are...";
}
else
{
std::cout << "\nThe door slides open. You proceed to the next level.\n\n";
}
return true;
}
else
{
std::cout << "\nThe screen reads 'ACCESS DENIED'. You must try this level again...\n\n";
return false;
}
}
int main()
{
int LevelDifficulty = 1;
int const MaxDifficulty = 4;
// loop game until all levels completed
while (LevelDifficulty <= MaxDifficulty) // doesn't matter to compiler but include a space between while keyword and argument parenthesis
{
bool bLevelComplete = PlayGame(LevelDifficulty, MaxDifficulty);
std::cin.clear();
std::cin.ignore();
if (bLevelComplete)
{
++LevelDifficulty;
}
}
return 0;
}
// TODOs
// - prevent user from inputting anything but a value (use a regex?)
// - increase immersion with more output statements
Hello,
This is my first post.
For Triple X, I used the same core mechanics as the course tutorial, the changes I made were more on how the game looked and ran through functions.
Here are some of the changes I made:
- Added a menu at the beginning of the game using functions and if statements;
- Fixed a compiler warning by using srand((unsigned int) time(NULL)) instead of srand(time(NULL));
- Added a title using ###;
- Changed the while loop from int main to a function;
- Customized the dialog.
Hope this is relevant.
Thank you for the opportunity.
Best,
// This program simulates a secret agent who needs to hack into a corporate server to bring light upon a series of unfortunate events.
// The player must decipher the code and enter the right numbers into the terminal in order to breach the security protocol and access the data.
// This game was made by Kirk Paradis following the Unreal Engine C++ Developper: Learn C++ and Make Video Games course.
#include <iostream>
#include <ctime>
using namespace std;
void PrintGameLevel(int Difficulty)
{
cout << "\nYou are on level " << Difficulty << endl;
}
void PrintTripleX()
{
cout << " ####### ###### ## ##### # ##### # # " << endl;
cout << " # # # ## # # # # # # " << endl;
cout << " # ###### ## ##### # #### # " << endl;
cout << " # # # ## # # # # # " << endl;
cout << " # # # ## # ###### ##### # #" << endl;
}
void PrintMenu()
{
cout << "\nP - Play" << endl;
cout << "Q - Quit" << endl;
cout << "C - Credits" << endl;
}
int ExitGame()
{
cout << "\nExiting game..." << endl;
return 0;
}
int GameCompleteMessage()
{
cout << "\nYou have made it to the core of the servers and start looking at the data..." << endl;
cout << "\nCould it be that... 2398rh239rh319r7g13897r1g39r8!@$#@$3hgr19378rhb319fdubn9317bd9p1....error@!#..." << endl;
cout << "You've got123804124dsxwe To..1e2sxsdcs geT..g.32e23...OUT!!!23rfd..." << endl;
return 0;
}
void PrintCredits()
{
cout << "*********************************************************************************************************************" << endl;
cout << "This game was made by Kirk Paradis following the Unreal Engine C++ Developper: Learn C++ and Make Video Games course." << endl;
cout << "*********************************************************************************************************************" << endl;
}
bool PlayGame(int Difficulty) //Runs the game & applies the codes
{
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;
int GuessA{}, GuessB{}, GuessC{};
cout << "\nAgent, we're almost out of time." << "\n\nYou must decipher the access code to the server's core before it's too late!" << endl;
cout << "\nThe code is made up of three numbers." << endl;
cout << "The sum of the numbers is " << CodeSum << endl;
cout << "The product of the numbers is " << CodeProduct << endl;
cout << "\nEnter the first number..." << endl;
cin >> GuessA;
cout << "Now on to the second..." << endl;
cin >> GuessB;
cout << "and the last...think carefully..." << endl;
cin >> GuessC;
int GuessSum = GuessA + GuessB + GuessC;
int GuessProduct = GuessA * GuessB * GuessC;
if (GuessSum == CodeSum && GuessProduct == CodeProduct)
{
cout << "\n****************" << endl;
cout << "\n***Well done!***" << endl;
cout << "\n****************" << endl;
return true;
}
else
{
cout << "\n**********************************************************" << endl;
cout << "\n***Careful, Agent! We almost got fried here. Try again.***" << endl;
cout << "\n**********************************************************" << endl;
return false;
}
}
void GameRunOnStart()
{
srand((unsigned int) time(NULL));
int LevelDifficulty = 1;
int const MaxLevel = 5;
while (LevelDifficulty <= MaxLevel)
{
PrintGameLevel(LevelDifficulty);
bool bLevelComplete = PlayGame(LevelDifficulty);
cin.clear(); // clear the failbits
cin.ignore(); // discards the buffer
if (bLevelComplete)
{
++LevelDifficulty;
}
}
}
void UserSelection()
{
char selection{};
cin >> selection;
if (selection == 'P' || selection == 'p')
{
GameRunOnStart();
}
else if (selection == 'Q' || selection == 'q')
{
ExitGame();
}
else if (selection == 'C' || selection == 'c')
{
PrintCredits();
PrintMenu();
UserSelection();
}
else
{
cout << "Wrong selection, please try again." << endl;
}
}
int main()
{
PrintTripleX();
PrintMenu();
UserSelection();
GameCompleteMessage();
return 0; // Exits the game
}
My code so far…
#include <iostream>
void PrintIntroduction(int Difficulty)
{
std::cout << "\nYou are a brave hero at floor " << Difficulty <<" of Dark Castle, fighting your way into a princess's chamber... \n";
std::cout << "Monster block your way with black magic tricks and you need to enter correct codes to brake them \n\n";
std::cout << "HINT: You need to put 3 numbers which add-up and will multiply as a values above \n\n";
}
bool PlayGame(int Difficulty)
{
// Background story printing to the console
PrintIntroduction(Difficulty);
const int CodeA = 4;
const int CodeB = 5;
const int CodeC = 6;
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 \n";
std::cout << "+ The codes add-up to: " << CodeSum;
std::cout << "\n+ The codes multiply to give: " << CodeProduct;
int GuessA, GuessB, GuessC;
// Store player guess
std::cout << "\n\nChoose your codes: ";
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 purged the black magic and move forward!\n";
return true;
}
else
{
std::cout << "\nMonster: \"You are not prepared!\"\n";
std::cout << "Monster deceived you, but you need to keep fighting! Try again and purge the witch!\n";
return false;
}
}
int main()
{
std::cout << R"(
_______________________________________
/ \
/ _ _ _ _ _ _ \
| | |_| |_| | _ _ _ | |_| |_| | |
| \ _ / | |_| |_| | \ _ / |
| | | | | \ / | | | | |
| | |_| |______| |______| |_| | |
| | ___ | |
| | _ _ ( ) _ _ | |
| | | | |_| ( ) |_| | | | |
| | |_| | | |_| | |
| / |_______| \ |
| |______________TripleX______________| /
\_______________________________________/
)" << std::endl;
int LevelDifficulty = 1;
const int MaxDifficulty = 5;
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;
}
}
std::cout << R"(
w*W*W*W*w
\"."."/
//`\\
(/. .\)
(\_-_/) THANK YOU BRAVE HERO!
.-~'='~-.
/`~`"Y"`~`\
/ /(_ * _)\ \
/ / ) ( \ \
\ \_/\\_//\_/ /
\/_) '*' (_\/
| |
| |
| |
| |
| |
| |
| |
| |
w*W*W*W*w
)";
std::cout << "\n Well done! You have saved your princess... now get your reward!";
return 0;
}
#include
#include
void PrintIntroduction(int Difficulty){
//writes strings and declares variables and shows them
std::cout << "\nYour girl is in the shower and you suspect her of cheating. You need to bypass the phonelock sequences to unlock the phone and check for evidence\n";
std::cout << "Enter the correct codes to pass level " << Difficulty << " sequence.\n";
}
bool PlayGame(int Difficulty)
{
PrintIntroduction(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 CodeMultiply = CodeA*CodeB*CodeC;
/* Writes codes to console */
std::cout << "\n+ There are 3 numbers in the password";
std::cout << "\n+ The numbers add up to: " << CodeSum;
std::cout << "\n+ The numbers multiply to: " << CodeMultiply;
//Store player's guess
int GuessA, GuessB, GuessC;
std::cout << "\n\nNumbers: ";
std::cin >> GuessA >> GuessB >> GuessC;
int GuessSum = GuessA + GuessB + GuessC;
int GuessProduct = GuessA * GuessB * GuessC;
if(GuessSum==CodeSum && GuessProduct==CodeMultiply)
{
std::cout << "You solved the sequence, Continue..\n";
return true;
}
else
{
std::cout << "Try Again\n";
return false;
}
}
int main()
{
srand(time(NULL)); //Creates new random sequence based on time of day
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();
std::cin.ignore();
if (bLevelComplete)
{
++LevelDifficulty;
}
}
std::cout << "\nYou found the evidence, your girl is for the streets >.<";
return 0;
}
#include
void PrintIntroduction(int Difficulty, int MaxDifficulty)
{
// Print welcome messages to the terminal
std::cout << "\n\n___________ .__ .__ ____ ___\n\\__ ___/______|__|_____ | | ____ \\ \\/ /\n | | \\_ __ \\ \\____ \\| | _/ __ \\ \\ / \n | | | | \\/ | |_> > |_\\ ___/ / \\ \n |____| |__| |__| __/|____/\\___ >___/\\ \\\n |__| \\/ \\_/\n\n\n";
std::cout << "| You are an agent trying to stop a terrorist attack.";
std::cout << "\n| A group of hackers accessed the Pentagon";
std::cout << "\n| They installed software to secretly launch nuclear missiles towards China";
std::cout << "\n| You managed to figure out where is the file that will allow you to stop the launch.";
std::cout << "\n| To access it you will have to break its " << MaxDifficulty << "-step security system";
std::cout << "\n| Each level has increased difficulty, you are currently at level " << Difficulty;
std::cout << "\n| Enter the correct code to continue...\n\n";
}
bool bPlayGame(int Difficulty, int MaxDifficulty)
{
PrintIntroduction(Difficulty, MaxDifficulty);
// 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 CodeSum and CodeProduct to the terminal
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 players guess is correct
if (GuessSum == CodeSum && GuessProduct == CodeProduct)
{
//std::cout << "\nAccess granted, you managed to cancel the launch. Congratulations!, humanity gets to see another day" << std::endl;
std::cout << "\nAcces granted, moving forward to the next verification...";
return true;
}
else
{
//std::cout << "\nAccess denied, the missiles were launched, WW3 started, humanity is doomed" << std::endl;
std::cout << "\nAcces denied, retry...";
return false;
}
}
int main()
{
int LevelDifficulty = 1;
const int MaxDifficulty = 5;
while (LevelDifficulty <= MaxDifficulty) // Loop game until all levels completed
{
bool bLevelComplete = bPlayGame(LevelDifficulty, MaxDifficulty);
std::cin.clear(); // Clears any errors
std::cin.ignore(); // Discards the buffer
if (bLevelComplete)
{
++LevelDifficulty;
}
}
std::cout << "\nVerification finished, you managed to cancel the launch. Congratulations!, humanity gets to see another day" << std::endl;
return 0;
}