Here is where I’m at. The story isn’t much and I haven’t done anything to crazy with the code.
#include<iostream>
void PrintIntroduction(int Difficulty)
{
//prints story on screen
std::cout << "\n\nYou were captured by some pirates and are locked in a level "<< Difficulty <<" cell with a 3 digit combination lock...\n";
std::cout << "You have to enter the correct code to get out...\n";
std::cout << " ______ \n";
std::cout << " |_||_| \n";
std::cout << " |_||_| \n";
std::cout << "_|_||_|_\n\n";
}
bool PlayGame(int Difficulty, int MaxLevel)
{
PrintIntroduction(Difficulty);
//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/product to terminal
std::cout << "You see some text on them...";
std::cout << "\nWhat 3 numbers add up to " << CodeSum << "?";
std::cout << "?\nAnd multiplie into " << CodeProduct << "?";
//store player guess
int GuessA, GuessB, GuessC;
std::cout << "\nYour answer (Use spaces in between each): \n";
std::cin >> GuessA >> GuessB >> GuessC;
std::cout << "\nYou answered " << GuessA << GuessB << GuessC << "...";
int GuessSum = GuessA + GuessB + GuessC;
int GuessProduct = GuessA * GuessB * GuessC;
//checks to see if the player is right
if(CodeSum == GuessSum && CodeProduct == GuessProduct)
{
std::cout << "\nYou put in the number and the lock opens. You can now get out of the cell...";
if (Difficulty != MaxLevel)
{
std::cout << "\nExcept there is another door with another lock";
}
return true;
}
else
{
std::cout << "\nYou put in the number but it won't open. Must be wrong.\nYou should try again.";
return false;
}
}
int main()
{
int LevelDifficulty = 1;
int MaxLevel = 5;
while(LevelDifficulty <= MaxLevel) //stops game when MaxLevel is met
{
bool bLevelComplete = PlayGame(LevelDifficulty, MaxLevel);
std::cin.clear(); //clears any errors
std::cin.ignore(); //discards the buffer
if (bLevelComplete)
{
++LevelDifficulty;
}
}
std::cout << "You've escaped the cells and go up to the deck (luckily the ship is sill in harbor). You've escaped.";
return 0;
}