I made my full version of that game with some different things.
In my version you can choose your Level and i added much more… (No spoiler here).
I am new to C++, and i developed this by using the Video-Teachings, Google, YouTube and my ideas… ( and my brain of course )
So try it out and have fun!!!
I recomment to cear the console and make it as big as possible before starting the game
And i am happy, if you leave a comment. Tell me what you think!
#include<iostream>
#include<ctime>
#include<mutex>
#include<string>
#include<thread>
#include<chrono>
//Name space res for the type-writer effect
using std::cout;
using std::flush;
using std::string;
using std::this_thread::sleep_for;
using std::chrono::milliseconds;
// Forward SlowPrint --- tbh i do not know why i have to do it, but the instruction i found told me so ;)
void SlowPrint(const string&, unsigned int);
void PrintIntroduction()
{
/* Some ascii aRT for the gamestart */
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << "\n .---. |";
std::cout << "\n = _/__~0_ _X . . * -O- '";
std::cout << "\n = = (__o___o___o_) . |";
std::cout << "\n . *";
std::cout << "\n * o . ' * . o";
std::cout << "\n __ ______________ __ ";
std::cout << "\n| | | |/_ _____/ __ _ ____ __ __ _/ |_ ";
std::cout << "\n| | | | | __) / _ ) ______ / _ | | | \\ __|";
std::cout << "\n| <_> | | | / <_> | /_____/ ( <_> )| | /| | ";
std::cout << "\n|______ | |___ | L_______/ | ___/ |____/ |__| ";
std::cout << std::endl;
std::cout << std::endl;
// This following output informs the player about the game (slowPrint)
string BeginnerMessage = "You are a prisoner of aliens in a space-ship. They think all humans are stupid.\nThe very friendly artificial intelligence AI-610 is watching your door-locks. \nIt is not allowed to tell you the codes, but the friendly AI decided to give you some hints! \nYou need to enter the correct door codes to escape....\n";
SlowPrint(BeginnerMessage, 1);
}
// here i set an empty flag "Gamestart" for the std::call_once function, to use later (you have to #include<mutex> in your Programm)
std::once_flag GameStart;
bool PlayGame(int Difficulty, int GuessCounter)
{
/*because the ASCII Art in PrintIntroduction() is to big
it should load only once in the game ;) */
std::call_once(GameStart, PrintIntroduction); // this calls the function "PrintIntroduction" and set the flag "GameStart" to used so it never starts again
/* declare 3 codes (notice: The unusual names in UpperCamelCase following the
Unreal4 naming conventions. */
const int CodeA = rand() % Difficulty + Difficulty;
const int CodeB = rand() % Difficulty + Difficulty;
const int CodeC = rand() % Difficulty + Difficulty;
// declare sum and product
const int CodeSum = CodeA + CodeB + CodeC ;
const int CodeProduct = CodeA * CodeB * CodeC;
// slowprint to sum/product to terminal
std::cout << std::endl;
string HintMessage1 = "*****************************************************\n** AI-610 : \n** You are now in a Level-";
SlowPrint(HintMessage1, 1);
cout << Difficulty;
string HintMessage2 = "-Difficulty-Room \n** There are 3 numbers in the code!\n** The codes adds-up to: ";
SlowPrint(HintMessage2, 1);
cout << CodeSum << std::endl;
string HintMessage3 ="** If you multiply the codes the product is: ";
SlowPrint(HintMessage3, 1);
cout << CodeProduct << std::endl;
string HintMessage4 = "*****************************************************\n";
SlowPrint(HintMessage4, 1);
// declare the Variables for player guesses
int GuessA, GuessB, GuessC;
// following i changed the teached program because i wanted the player to have more guesses!
while ( GuessCounter != 4 )
{
// Waiting for player input for the codes
std::cout << "Now enter your 3 numbers with spaces in between \n";
std::cout << "(like e.g: 1 2 3) and hit Return ! \n";
std::cin >> GuessA >> GuessB >> GuessC;
// print out the guess
string GuessPrint = "Your guess was: ";
SlowPrint(GuessPrint, 1);
cout <<GuessA << " " << GuessB << " " << GuessC << std::endl;
int GuessSum = GuessA + GuessB + GuessC;
int GuessProduct = GuessA * GuessB * GuessC;
// Checking if the players guess is right
if (GuessSum == CodeSum && GuessProduct == CodeProduct ) // so he is right
{
string OpenedDoor = "*********************************************\n** AI-610 : YOU OPENED THAT DOOR ! \n** You entered the next room \n**********************************************" ;
SlowPrint(OpenedDoor, 1);
return(true);
} else // he was wrong:
{
string WrongGuess1 ="*********************************************************\n** AI-610 : YOU ARE WRONG! \n** You have only 3 attempts\n** This was attempt number: ";
SlowPrint(WrongGuess1,1);
cout << GuessCounter;
string WrongGuess2 = "! \n**********************************************************\n";
SlowPrint(WrongGuess2, 1);
++GuessCounter;
if (GuessCounter == 4) // after 3 attemps: GAME OVER!
{
string GameOver = "*******************************************************************************\n** AI-610 : SO YOU LOST! \n** I am sorry, but that was wrong!\n** You can to try again later ! Maybe in a lower level?? \n*********************************************\n";
SlowPrint(GameOver, 25);
}
}
}
/* if the player lands here, it means he was 3 times wrong (guesscounter == 4)
and the function returns now "false" to end the game*/
std::cout << "The solution was : " << CodeA << " " << CodeB << " " << CodeC << "!";
return(false);
}
int main()
{
/* to initialize rand() with a different seed based
on the time of the day*/
srand(time(NULL));
// i want to let the player decide, on what level he think he is... ;)
int LevelQuery;
string WelcomeMessage = "\n*********************************************************\n Welcone to ALIEN-OUT !! Open 6 Doors to win! \nOn what level do you want to play that game?\nFor LOW enter a number between 1-5, MEDIUM 5-10; HARD = more than 10!\nHint : Enter only a positiv number or the game does not work properly ;) \n";
SlowPrint(WelcomeMessage, 1);
std::cin >> LevelQuery; //if the player enters not a number, the game will stop after a time = This is a Feature, not a BUG.;)
// declare needed variables
int LevelDifficulty = LevelQuery;
const int MaxDifficulty = LevelQuery + 6;
int GuessCounter = 1;
while (LevelDifficulty <= MaxDifficulty) // will Play the Game until maxLEvel is reached
{
bool bLevelComplete = PlayGame(LevelDifficulty, GuessCounter); // bools in Unreal should start with an lowercase b
std::cin.clear(); // clears any error
std::cin.ignore(); // Discards the buffer
if (bLevelComplete)
{
++LevelDifficulty;
}else
{
return(0);
}
}
// the player won and here is the Last message
string WinnerMessage = "**********************************************************************\n** AI-610 : GREAT!! YOU WON! YOU ESCAPED! HUMANS ARE NOT STUPID!!! **\n** You can use that small space-tender in front of you! **\n** I programmed the directions to your home into it. **\n** Have a nice flight. Good luck and goodbye! **\n**********************************************************************\n";
SlowPrint(WinnerMessage, 25);
return 0;
}
void SlowPrint(const string& Message, unsigned int MillisPerChar)
{
for(const char c:Message) // for every char in the message
{
cout << c << flush; //print the character and then ?flush?
sleep_for(milliseconds(MillisPerChar)); //self explaining ;)
}
}