Issues with FailState for game

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;
}

And here’s the if else statement that I added to try changing the sentence that was prompted.

// Checks if player's answer is correct
	if (Difficulty == MaxDifficulty)
	{
		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;
		}
	}
	else
	{
		if (GuessMult(GuessA, GuessB, GuessC) == CodeMult(CodeA, CodeB, CodeC) && GuessSum(GuessA, GuessB, GuessC) == CodeSum(CodeA, CodeB, CodeC))
		{
			cout << "\nCongratulations, you entered the code correctly. Now on to the next room!\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;
		}
	}```

I’m not quite sure what the problem is.

Entering 2 2 2 and then 1 1 1 gives expected output which is to say the levl starts again.

P.S. your OP could do with better formatting for that first paragraph to be easier to read.

I apologize, I will work on the formatting next time.

The problem does still persist. What I’m trying to solve is when the else statements in this thread’s second comment return false, I want it to go to main and then return 0, therefore forcing an exit.

What’s happening, though, is when the else statement gets encountered with the return false line in it, while it does prompt the message “Sorry, you entered the code incorrectly…” it also does what I don’t want it to do, which is just restarts the game from the beginning, without returning 0 from main.

I would post the results from the Terminal if it’d help explain it better, but I’m not sure what the best way to go about doing that would be.

I’m supposing the problem could lie in the while loop of main, because the error resets the difficulty level from whatever it’s currently at back to 1 rather than recognizing the fail state and then exiting the program.

Perhaps because it hasn’t reached the max difficulty level like the parameters of the while loop state, it doesn’t ever truly hit its failstate and force the return 0 line?

The only way to exit the terminal at this point after encountering a failstate is restarting and winning, exiting with control + c, or exiting with the X at the top of the terminal. What I want is for the player to be forced to exit the program and restart it fresh.

Okay I think I get what you mean. If I do understand you correctly you should just return true/false if the guess is correct in PlayGame and then handle all messages and control flow in main.


I do have some code comments.

GuessMult is identical to CodeMulti. Same with the Sum functions. You don’t need two.
P.S. you don’t need a variable in the function, you can just return the expression. i.e.

return Num1 + Num2 + Num3;

I’d argue that having repeated use of the functions instead of storing it in a variable makes the code too verbose and harder to read than the alternative.

It was a bit difficult to get what you were saying but I think one of these two options should work:

If you're trying to exit the program on a false some of the time but not always:

If you don’t want to change the structure too much (i.e. keeping the guess check in the play game function, you could instead return an integer from it instead of a bool, (e.g. -1 for quit game, 0 for the equivalent of false, and 1 for the equivalent of true), and check the output slightly differently in the while loop.

If you're trying to exit the program on EVERY false:

Add an else statement after if (bLevelComplete) and just put break; in it which will terminate the while loop.
Or add continue; to the existing if statement to skip to the next iteration of the loop and put break; after the if statement, but not in the else statement.

Thank you very much, both of you. I appreciated the simplicity with which you simplified my CodeMulti and CodeSum, DanM. I completely forgot that I could just re-use the same function for both the guess and the answers, but when you mentioned it it made me feel like facepalming. Unfortunately, and this is more on me than you, the initial question wasn’t answered. However, 100PixelsSquared, your ‘break;’ solution worked perfectly, so I appreciate that greatly. Thanks for your help, again! :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms