Hello, I’m a little experienced in C++, but it’s been a few years since I’ve initially learned it, so I’m hopping back onto this course to both get a refresher as well as learn how to implement it into the game engine. I’m on the TripleX game, but I have run into a couple of issues. Given my previous experience, I took some minor liberties with the instruction, and seem to have dug myself into a hole. I’ve changed the scenario to a prison break. In order to increase difficulty, I had the initial cell and each room have the keypad, but I wanted to differentiate to the player when they’re in the last rooml versus the other rooms. At first it worked great, with the bool’s true and false statements inside an if else statement, and the sentence I have works okay for what I want, but I tried experimenting with differentiating between the last room and the rest of the rooms. I added an if else statement that incorporated the first one as well as a slightly varied new one, the new if statement having the parameters of (Difficulty == MaxDifficulty). I had the return true and return false statements within the new if statement and the new else statement, so two sets of trues and falses for the bool (I don’t know if that’s what caused the problem). I tested it and it worked fine until the very end of the development for the game. Testing it now causes it to just restart the game after failing rather than forcing the FailState. I deleted the new if else statement, which results in me just having one if else statement with only one return true and one return false, but still the problem persists. Not entirely sure what the issue is, and while it’s not a huge problem, I’d prefer being able to have people unfamiliar with how to quit out of the terminal (Ctrl + C) not have to worry about it.
Anyways, here’s the code, and a TL;DR
TL;DR: If else statement containing the bool return true and return false blocks of code keeps restarting the game rather than allowing the player to exit when the player runs into a failstate.
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;
void PrintIntroduction()
{
cout << "\n\n- - - - - PRISON BREAK - - - - -" << endl;
cout << "You're being held in a prison, but,\n";
cout << "like all prisoners, you are innocent.\n" << endl;
cout << "You've discovered that you can escape\n";
cout << "by entering in 3 sets of numbers on a keypad.\n" << endl;
cout << "The problem is that you didn't know how\n";
cout << "the sequence worked, but you pickpocketed\n";
cout << "the guard that walked by an hour ago. You\n";
cout << "found out that the code changes often, but the\n";
cout << "way to solve it is stays the same.\n" << endl;
cout << "You're given two results:\n";
cout << "-- one is attained by multiplying the three numbers\n";
cout << "-- the other is attained by adding them together.\n" << endl;
cout << "It seems that there's multiple rooms with similar\n";
cout << "keypads. You may have to enter in several codes,\n";
cout << "and at increasing degrees of difficulty.\n" << endl;
cout << "If you fail to enter any code correctly,\n";
cout << "alarms go off, you get caught by the guards,\n";
cout << "and it's game over.\n" << endl;
}
int CodeMult(const int Num1, const int Num2, const int Num3)
{
int Mult = Num1 * Num2 * Num3;
return Mult;
}
int CodeSum(const int Num1, const int Num2, const int Num3)
{
int Sum = Num1 + Num2 + Num3;
return Sum;
}
int GuessMult(const int Num1, const int Num2, const int Num3)
{
int Mult = Num1 * Num2 * Num3;
return Mult;
}
int GuessSum(const int Num1, const int Num2, const int Num3)
{
int Sum = Num1 + Num2 + Num3;
return Sum;
}
bool bPlayGame(const int Difficulty, const int MaxDifficulty)
{
if (Difficulty == 1)
PrintIntroduction();
// Variable declarations for CodeA, CodeB, & CodeC
int CodeA = rand() % Difficulty + Difficulty * 2;
int CodeB = rand() % Difficulty + Difficulty * 2;
int CodeC = rand() % Difficulty + Difficulty * 2;
if (Difficulty == 1)
{
// First sequence of numbers
cout << "Your cell's sequence is:" << endl;
cout << "-- three numbers muliplied together to get " << CodeMult(CodeA, CodeB, CodeC) << "," << endl; // Print CodeMult
cout << "-- three numbers added together to get " << CodeSum(CodeA, CodeB, CodeC) << "," << endl; // Print CodeSum
}
else
{
// First sequence of numbers
cout << "The sequence for room " << Difficulty << " is:" << endl;
cout << "-- three numbers muliplied together to get " << CodeMult(CodeA, CodeB, CodeC) << "," << endl; // Print CodeMult
cout << "-- three numbers added together to get " << CodeSum(CodeA, CodeB, CodeC) << "," << endl; // Print CodeSum
}
cout << endl;
// Player will enter numbers for these variables to find out if they correctly guessed the anser.
int GuessA, GuessB, GuessC;
cout << "Enter the three numbers, then press 'x.'\n" << endl;
cin >> GuessA;
cin >> GuessB;
cin >> GuessC;
// Checks if player's answer is correct
if (GuessMult(GuessA, GuessB, GuessC) == CodeMult(CodeA, CodeB, CodeC) && GuessSum(GuessA, GuessB, GuessC) == CodeSum(CodeA, CodeB, CodeC))
{
cout << "\nCongratulations, you entered the code correctly. Escape while you can!\n" << endl; // Displays if the entries are correct
return true;
}
else
{
cout << "\nSorry, you entered the code incorrectly and are stuck here until you're released.\n" << endl; // Displays if the entries are incorrect
return false;
}
}
int main()
{
srand(time(NULL));
int LevelDifficulty = 1;
const int MaxLevelDifficulty = 5;
while (LevelDifficulty <= MaxLevelDifficulty)
{
bool bLevelComplete = bPlayGame(LevelDifficulty, MaxLevelDifficulty);
//cin.clear(); // Clears any errors
//cin.ignore(); // Discards the buffer
if (bLevelComplete)
{
++LevelDifficulty;
}
}
return 0;
}