#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
const void Introduction(unsigned int level, unsigned int &lives);
string GetCodeUser(unsigned int &CodeA, unsigned int &CodeB,unsigned int &CodeC, unsigned int &Sum, unsigned int &Multiply);
string GetCodePrison(unsigned int &CodeA, unsigned int &CodeB, unsigned int &CodeC, unsigned int &Sum, unsigned int &Multiply, unsigned int difficulty);
void PlayGame(unsigned int& Level, unsigned int &Lives);
const void PrintLives(unsigned int Lives);
int main()
{
//level that user is currently on
unsigned int CurrentLevel = 1;
//final level in the game
const unsigned int FinalLevel = 10;
//player lives
unsigned int Lives = 3;
while(CurrentLevel <= FinalLevel && Lives > 0)
{
PlayGame(CurrentLevel, Lives);
cin.clear();
cin.ignore();
}
if(Lives > 0)
{
cout << "\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/" << endl;
cout << "\/\/ \/\/" << endl;
cout << "\/\/ YOU ESCAPED! \/\/" << endl;
cout << "\/\/ \/\/" << endl;
cout << "\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/" << endl;
}
else
{
cout << "!!!!!!!!!!!!!!!!!!!!!!!" << endl;
cout << "! !" << endl;
cout << "! YOU DIED! !" << endl;
cout << "! !" << endl;
cout << "!!!!!!!!!!!!!!!!!!!!!!!" << endl;
}
return 0;
}
const void Introduction(unsigned int level, unsigned int &lives)
{
system("CLS");
//give background to the game
cout << "***************************************************************" <<endl;
cout << "*You are a prisoner in a high facility prison trying to escape*" <<endl;
cout << "* *Enter the correct code to escape* *" <<endl;
cout << "* LEVEL: " << level << " *" <<endl;
cout << "* Lives Remaining: " << lives <<" *" <<endl;
cout << "***************************************************************" <<endl;
cout << "\n";
}
string GetCodeUser(unsigned int &CodeA, unsigned int &CodeB,unsigned int &CodeC, unsigned int &Sum, unsigned int &Multiply)
{
//user guess for the code
string Guess = "";
//ask user for a guess & print it
cout << "Enter your guess: ";
cin >> Guess;
//cout << "You Entered: " << Guess << endl;
CodeA = Guess[0] - '0';
CodeB = Guess[1] - '0';
CodeC = Guess[2] - '0';
Sum = CodeA + CodeB + CodeC;
Multiply = CodeA * CodeB * CodeC;
return Guess;
}
string GetCodePrison(unsigned int &CodeA, unsigned int &CodeB, unsigned int &CodeC, unsigned int &Sum, unsigned int &Multiply, unsigned int difficulty)
{
srand(time(NULL));
//difficulty=+3;
difficulty++;
CodeA = (rand() % difficulty) + (difficulty - 1);
CodeB = (rand() % difficulty) + (difficulty - 1);
CodeC = (rand() % difficulty) + (difficulty - 1);
if(CodeA >= 10)
{
do
{
//cout << "CA = " << CodeA << endl;
CodeA = (CodeA - (CodeA % 9)) - (rand() % 3);
}while(CodeA >= 10);
}
if(CodeB >= 10)
{
do
{
//cout << "CB = " << CodeB << endl;
CodeB = CodeB - (CodeB % 9) - (rand() % 3);
}while(CodeB >= 10);
}
if(CodeC >= 10)
{
do
{
//cout << "CC = " << CodeC << endl;
CodeC = CodeC - (CodeC % 9) - (rand() % 3);
}while(CodeC >= 10);
}
Sum = CodeA + CodeB + CodeC;
Multiply = CodeA * CodeB * CodeC;
cout << "+There are 3 numbers in the code" << endl;
cout << "+The codes add-up to: " << Sum << endl;
cout << "+The codes multiply to: " << Multiply << endl;
cout << "\n";
/*
cout << "Difficulty = " << difficulty << endl;
cout << "Code A = " << CodeA << endl;
cout << "Code B = " << CodeB << endl;
cout << "Code C = " << CodeC << endl;
*/
string PrisonCode = to_string(CodeA) + to_string(CodeB) + to_string(CodeC);
return PrisonCode;
}
void PlayGame(unsigned int& Level, unsigned int &Lives)
{
//users code and Sum and Multiply
unsigned int UserCodeA, UserCodeB, UserCodeC, UserSum, UserMultiply;
//cout << "116 Difficulty = " << Level << endl;
//Prison code and Sum and Multiply
unsigned int PrisonCodeA, PrisonCodeB, PrisonCodeC, PrisonSum, PrisonMultiply;
//cout << "120 Difficulty = " << Level << endl;
//user and pron code as string
string UserCode = "";
string PrisonCode = "";
bool Incorrect = true;
bool IncorrectOrder = true;
while(Incorrect && Lives > 0)
{
//function to print the introduction to the game
Introduction(Level, Lives);
//cout << "133 Difficulty = " << Level << endl;
//function to get code from the prison
PrisonCode = GetCodePrison(PrisonCodeA, PrisonCodeB, PrisonCodeC, PrisonSum, PrisonMultiply, Level);
//cout << "137 Difficulty = " << Level << endl;
//cout << "Code = " << PrisonCode<<endl;
//function to get code from the user
UserCode = GetCodeUser(UserCodeA, UserCodeB, UserCodeC, UserSum, UserMultiply);
//logic---idk what i made
if(UserSum == PrisonSum && UserMultiply == PrisonMultiply)
{
if(UserCode != PrisonCode)
{
cout << "So Close!" << endl;
Lives--;
PrintLives(Lives);
while(IncorrectOrder && Lives > 0)
{
UserCode = GetCodeUser(UserCodeA, UserCodeB, UserCodeB, UserSum, UserMultiply);
if(UserCode != PrisonCode)
{
if(Lives == 1)
{
Lives--;
break;
}
else
{
cout << "So Close!" << endl;
Lives--;
PrintLives(Lives);
}
}
else
{
IncorrectOrder = false;
Incorrect = false;
Level++;
}
}
}
else
{
cout << "Access Granted!" <<endl;
if(Lives < 5)
{
Lives++;
}
Level++;
Incorrect = false;
}
}
else
{
cout << "Access Denied!" << endl;
Lives--;
}
}
}
const void PrintLives(unsigned int Lives)
{
cout << "***********************" << endl;
cout << "* *" << endl;
cout << "* Lives Remaining: " << Lives << " *" << endl;
cout << "* *" << endl;
cout << "***********************" << endl;
}
I see you also made it that the code has to be in the correct order as well. Noice.
I tried your game I donât understand a few things about your code though, and it got me interested.
I will use your GetCodeUser()
as an example:
-
unsigned int &CodeA
: I net that unsigned is just a positive number while variable still takes 4 bytes but what does â&â before variable stand for? -
You use string to store Guess, and then you use
CodeA = Guess[0] - '0'
; I understand that you substract information from string and it ends up in integer contanairs, but donât quite know how it happens and why.
I will be glad if you could answear this for me
Yeah so basically what â&â does is that it passes the variable by reference, this means that instead of the compiler making a copy of the variable it takes the actual address of the variable as a parameter, this allows me to make changes to the variable inside the function even if the variable wasnât declared in the function⌠hopefully that makes sense.
For the CodeA = Guess[0] - â0â part basically, there is this table called the âASCIIâ and all the characters that you can type are on there including backspace, etc. and so basically Guess[0] is a character from the string which then correlates to an ASCII number, and so if i subtract â0â (which is the smallest integer) i basically âconvertâ (not really convert) a char into an int.
^
|_ More info on ASCII : https://www.quora.com/Why-does-char-0-successfully-convert-a-char-to-int-in-C
UPDATE:
I updated my code because the random number was giving me numbers larger than 9 so I had to debug that and as well I just changed the âYou Escapedâ message so it actually looks ok when printed⌠Please ignore all extra comments of code that was just me trying to debug. O and i also added âsystem(âCLSâ)â which just clears the terminal so that if you get two of the same you cant look back and see what you did previously
Thank you.
Hi, welcome to the community!
Nice job implementing that extra feature. Might I suggest changing the section of your thread, this is being posted on the Unity forum, not the Unreal forum, posting in the correct section might increase the interaction with the pertinent community. You can do that by simply editing your post and changing the section with the dropdown menus.
Thanks for replying, thanks to you I understand bit more about C++ now
Using the fact that ASCII indexing numbers after another seems to be usefull in some cases.
Thank you