Enjoyed this exercise, it’s a great introduction to C++
My screen output:
PORTAL X
.,-:;//;:=,
. :H@@@MM@M#H/.,+%;,
,/X+ +M@@M@MM%=,-%HMMM@X/,
-+@MM; $M@@MH+-,;XMMMM@MMMM@+-
;@M@@M- XM@X;. -+XXXXXHHH@M@M#@/.
,%MM@@MH ,@%= .---=-=:=,.
=@#@@@MX ., -%HX$$%%%+;
=-./@M@M$ .;@MMMM@MM:
X@/ -$MM/ .+MM@@@M$
,@M@H: :@: . =X#@@@@-
,@@@MMX, . /H- ;@M@M=
.H@@@@M@+, %MM+..%#$.
/MMMM@MMH/. XM@MH; =;
/%+%$XHH@$= , .H@@@@MX,
.=--------. -%H.,@@@@@MX,
.%MM@@@HHHXX$$$%+- .:$MMX =M@@MM%.
=XMMM@MM@MM#H;,-+HMM@M+ /MMMX=
=%@M@M#@$-.=$@MM@@@M; %M%=
,:+$+-,/H#MMMMMMM@= =,
=++%%%%+/:-.
You have been teleported through 5 rooms with seemingly
no way out other than back through the portals themselves.
The portal devices have deactivated but each appear to have
a numeric keypad requiring a numeric input to activate them.
Using your technology to aid you make your way back through
the portals. Note: The numbers will get harder to crack with
each portal!
Number of portals remaining: 5
Your concealed hacking tool reveals portal 5 requires three key
numbers to be entered in any order, along with the following
information:
The numbers add up to: 3
The numbers multiply to give: 1
Enter the correct 3 numbers (in any order) to escape back
through portal 5 (leave a space between each number) …
1 1 1
The portal flashes briefly then bursts into life. You get to
step through the portal
Press Enter key to continue
Number of portals remaining: 4
Your concealed hacking tool reveals portal 4 requires three key
numbers to be entered in any order, along with the following
information:
The numbers add up to: 7
The numbers multiply to give: 12
Enter the correct 3 numbers (in any order) to escape back
through portal 4 (leave a space between each number) …
2 2 3
The portal flashes briefly then bursts into life. You get to
step through the portal
Press Enter key to continue
Number of portals remaining: 3
Your concealed hacking tool reveals portal 3 requires three key
numbers to be entered in any order, along with the following
information:
The numbers add up to: 12
The numbers multiply to give: 60
Enter the correct 3 numbers (in any order) to escape back
through portal 3 (leave a space between each number) …
3 4 5
The portal flashes briefly then bursts into life. You get to
step through the portal
Press Enter key to continue
Number of portals remaining: 2
Your concealed hacking tool reveals portal 2 requires three key
numbers to be entered in any order, along with the following
information:
The numbers add up to: 19
The numbers multiply to give: 245
Enter the correct 3 numbers (in any order) to escape back
through portal 2 (leave a space between each number) …
5 7 7
The portal flashes briefly then bursts into life. You get to
step through the portal
Press Enter key to continue
Number of portals remaining: 1
Your concealed hacking tool reveals portal 1 requires three key
numbers to be entered in any order, along with the following
information:
The numbers add up to: 20
The numbers multiply to give: 270
Enter the correct 3 numbers (in any order) to escape back
through portal 1 (leave a space between each number) …
5 6 9
The portal flashes briefly then bursts into life. You get to
step through the portal
Press Enter key to continue
You stumble out of the final portal back into the room you
started from. Congratulations!!!
Press Enter key to exit program
and my code:
// Portal X game (based on TripleX) to demonstrate some fundamentals of a simple C++ program
// Preprocessor Directives - instructions to the compiler
#include <iostream> // header file for input output stream functionality
#include <ctime> // header file for time functionality
// Our PrintIntroduction function
void PrintIntroduction(int NumOfPortals)
{
std::cout << " PORTAL X\n\n";
std::cout << " .,-:;//;:=, \n";
std::cout << " . :H@@@MM@M#H/.,+%;, \n";
std::cout << " ,/X+ +M@@M@MM%=,-%HMMM@X/, \n";
std::cout << " -+@MM; $M@@MH+-,;XMMMM@MMMM@+- \n";
std::cout << " ;@M@@M- XM@X;. -+XXXXXHHH@M@M#@/. \n";
std::cout << " ,%MM@@MH ,@%= .---=-=:=,. \n";
std::cout << " =@#@@@MX ., -%HX$$%%%+; \n";
std::cout << " =-./@M@M$ .;@MMMM@MM: \n";
std::cout << " X@/ -$MM/ .+MM@@@M$ \n";
std::cout << " ,@M@H: :@: . =X#@@@@-\n";
std::cout << " ,@@@MMX, . /H- ;@M@M=\n";
std::cout << " .H@@@@M@+, %MM+..%#$.\n";
std::cout << " /MMMM@MMH/. XM@MH; =; \n";
std::cout << " /%+%$XHH@$= , .H@@@@MX, \n";
std::cout << " .=--------. -%H.,@@@@@MX, \n";
std::cout << " .%MM@@@HHHXX$$$%+- .:$MMX =M@@MM%. \n";
std::cout << " =XMMM@MM@MM#H;,-+HMM@M+ /MMMX= \n";
std::cout << " =%@M@M#@$-.=$@MM@@@M; %M%= \n";
std::cout << " ,:+$+-,/H#MMMMMMM@= =, \n";
std::cout << " =++%%%%+/:-. \n\n";
//std::cout << "Press Enter key to continue\n";
//std::cin.get();
// Expression statements
std::cout << "You have been teleported through " << NumOfPortals << " rooms with seemingly\n"; // std is a namespace, :: is a scope operator, << is an insertion operator which inserts chrs into the output stream
std::cout << "no way out other than back through the portals themselves.\n" ; // \n (escape sequence) starts a new line, you could have used << std::endl at the end of the line instead
std::cout << "The portal devices have deactivated but each appear to have\n";
std::cout << "a numeric keypad requiring a numeric input to activate them.\n\n";
std::cout << "Using your technology to aid you make your way back through\n";
std::cout << "the portals. Note: The numbers will get harder to crack with\n";
std::cout << "each portal!\n\n";
}
// Our PlayGame function
bool PlayGame(int NumOfPortals, int CurrentPortalNum, bool bIntroDisplayed) // void is used here as the return type as there is no return data in this function
{
if (not bIntroDisplayed)
{
PrintIntroduction(NumOfPortals);
}
std::cout << "Number of portals remaining: " << CurrentPortalNum << "\n\n";
// Used to increase difficulty as you pass through portals
int Difficulty = NumOfPortals - CurrentPortalNum + 1;
// Variable declarations - Note the naming convention used by Unreal Engine (UpperCase letter for each word with no underscores)
const int NumA = ((rand() % Difficulty) + 1) + Difficulty - 1; // Initialised to a pseudo random number, Const ensures that the variable cannot be changed after it's been declared and initialised
const int NumB = ((rand() % Difficulty) + 1) + Difficulty - 1; // Initialised to a pseudo random number
const int NumC = ((rand() % Difficulty) + 1) + Difficulty - 1; // Initialised to a pseudo random number
const int NumSum = NumA + NumB + NumC; // Use of the arithmetic operator +
const int NumProduct = NumA * NumB * NumC;
// Expression statements
std::cout << "Your concealed hacking tool reveals portal " << CurrentPortalNum << " requires three key\n";
std::cout << "numbers to be entered in any order, along with the following\n";
std::cout << "information:\n\n";
std::cout << "The numbers add up to: " << NumSum << "\n";
std::cout << "The numbers multiply to give: " << NumProduct << "\n\n";
std::cout << "Enter the correct 3 numbers (in any order) to escape back\n";
std::cout << "through portal " << CurrentPortalNum << " (leave a space between each number) ...\n";
int GuessA;
int GuessB;
int GuessC;
int GuessSum;
int GuessProduct;
// Get player input
std::cin >> GuessA >> GuessB >> GuessC; // cin captures player kbd input, >> << is an extraction operator which extracts chrs from the input stream
GuessSum = GuessA + GuessB + GuessC;
GuessProduct = GuessA * GuessB * GuessC;
if (GuessSum == NumSum && GuessProduct == NumProduct) // == is the equality operator (comparison operator), && is the boolean AND operator
{
std::cout << "\nThe portal flashes briefly then bursts into life. You get to\n";
std::cout << "step through the portal :-)\n\n";
return true;
}
else
{
std::cout << "\nThe portal remains lifeless. Looks like you need to try \n";
std::cout << "again.\n\n";
return false;
}
}
// main function
int main()
{
int NumOfPortals = 5;
int CurrentPortalNum = NumOfPortals;
bool bIntroDisplayed = false;
srand(time(NULL));
// Loop game until all levels completed
while (CurrentPortalNum > 0)
{
bool bLevelComplete = PlayGame(NumOfPortals, CurrentPortalNum, bIntroDisplayed); // Unreal naming convention has bool variables prefixed with a lower base b
bIntroDisplayed = true;
std::cin.clear(); // Clears the error status flags
std::cin.ignore(256, '\n'); // removes all characters in the stream buffer up to the new line 256, '\n'
std::cout << "Press Enter key to continue\n";
std::cin.get();
if (bLevelComplete)
{
--CurrentPortalNum;
}
}
std::cout << "You stumble out of the final portal back into the room you\n";
std::cout << "started from. Congratulations!!!\n\n";
std::cout << "Press Enter key to exit program\n";
std::cin.get();
// Return statement
return 0;
}