My Progress so far

I wasn’t happy that my game prints the welcome message everytime i enter code number, so i’ve modified my game so that the welcome message is printed once and within the levels only the messages corresponding to the level are displayed.

In addition I’ve realized that originally when finishing the last level PlayGame() prints a message that the current level has been completed but there are still levels left. So now when PlayGame() completes the last level, a different message is being displayed than in the other levels. To realize this, i’m passing the maximum difficulty to PlayGame():

#include <iostream>

void PrintIntroduction()
{
	//print welcome messages to the terminal

	std::cout << "__   __                             _                                  _ \n";
	std::cout << "\\ \\ / /                            | |                                | |\n";
	std::cout << " \\ V /___  _   _    __ _ _ __ ___  | |_ _ __ __ _ _ __  _ __   ___  __| |\n";
	std::cout << "  \\ // _ \\| | | |  / _` | '__/ _ \\ | __| '__/ _` | '_ \\| '_ \\ / _ \\/ _` |\n";
	std::cout << "  | | (_) | |_| | | (_| | | |  __/ | |_| | | (_| | |_) | |_) |  __/ (_| |\n";
	std::cout << "  \\_/\\___/ \\__,_|  \\__,_|_|  \\___|  \\__|_|  \\__,_| .__/| .__/ \\___|\\__,_|\n";
	std::cout << "                                                 | |   | |               \n";
	std::cout << "                                                 |_|   |_|               \n\n\n";

	std::cout << "School starts. You've been locked into a locker by bullies and need to brake the SkynetDigitalLock (tm) level  to exit\n";
	std::cout << "You need to enter the correct codes to continue...\n";

}


bool PlayGame(int Difficulty, int MaxDifficulty)
{
	//declare our three number code
	const int CodeA = 2; //rand();
	const int CodeB = 3;  //rand();
	const int CodeC =4; // rand();

	const int CodeSum = CodeA + CodeB + CodeC;
	const int CodeProduct = CodeA * CodeB * CodeC;
	
	//print sum an product to the terminal
	std::cout << "\n";
	std::cout << "* there are 3 numbers in the code\n";
	std::cout << "* The code adds up to: " << CodeSum << "\n";
	std::cout << "* The product of the code numbers is: " << CodeProduct << "\n";

	// Store player guess
	int GuessA, GuessB, GuessC;
	std::cin >> GuessA >> GuessB >> GuessC;
	std::cout << "\n";
	
	int GuessSum = GuessA + GuessB + GuessC;
	int GuessProduct =   GuessA * GuessB * GuessC;

	// check if the players guess is correct
	if (GuessSum == CodeSum && GuessProduct == CodeProduct)
	{
		// iv we've completed the last level, a different message is being displayed showing the user that the lock has been opened
		if(Difficulty == MaxDifficulty)
		{
			std::cout << "########## CLICK ########## \n\n\n";
		}
		else
		{
			std::cout << "*** You've completed level " << Difficulty << " but there are " << MaxDifficulty << " levels to open the lock! ***\n";
		}
		
		return true;
	}
	else
	{
		std::cout << "*** You have entered the wrong code, please try again! ***\n";
		//std::cout << "*** You failed to open the locker before class starts. You're still locked in! ***\n";
		//std::cout << "*** Since it is friday, you are going to stay in the locker for the whole weekend! ***\n\n\n";
		return false;
	}
}


int main()
{
	int LevelDifficulty = 1;
	const int MaxDifficulty = 5;

	PrintIntroduction();

	while (LevelDifficulty <= MaxDifficulty) // Loop game until all levels are completed
	{
		bool bLevelComplete = PlayGame(LevelDifficulty, MaxDifficulty);
		std::cin.clear(); // Clear any errors
		std::cin.ignore();// discards the buffer

		if (bLevelComplete)
		{
			++LevelDifficulty;
		}
	}

	std::cout << "*** You've opened the locker secretly and made it to class in time ! ***\n\n\n";

	return 0;
}
3 Likes

You don’t need to print that many lines for your ASCII art. I just found this method online and it works + it doesn’t flag the slashes any more / .

3 Likes

Genius! Thank you :sweat_smile::pray:t3:

Privacy & Terms