Hi,
I have a question about my code here. I implemented a system where it tracks the number of attempts a player takes and will stop the game at a certain amount of guesses (the AttemptedGuesses() function). However, I have found that when I answer (either correct or incorrect) it always somehow calls the function twice.
For example, if I enter in the correct code, it prints something like:
You entered: 111
— Correct! Move on to the next vault for even MORE money! —
— Oh no! Wrong code, try again but be careful. You only have 3 total attempts! —
— Attempts Left: 3 —
And when I enter the wrong code it prints something like:
You entered: 112
— Oh no! Wrong code, try again but be careful. You only have 3 total attempts! —
— Attempts Left: 2 —
— Oh no! Wrong code, try again but be careful. You only have 3 total attempts! —
— Attempts Left: 2 —
I’m not sure why this is happening. Could someone read over my code and tell me the issue?
Thank you so much!
P.S. I’m not sure if this is the most effective way of sharing my code in C++, if there is a better way, please let me know! Thanks
#include <iostream>
#include <ctime>
// Any variables I need throughout code
int Attempts;
void PrintIntroduction(int Difficulty)
{
// Story for the game
std::cout << "\n--- You're a bank robber currently at the door of a level " << Difficulty;
std::cout << " vault. ---\n--- You must find the code that opens the vault door in order to get your money... ---\n";
std::cout << "--- You will have 3 attempts at each level. ---\n--- If you fail on your third attempt, the police will catch up and you will fail the mission. ---\n\n";
}
// Code to determaine how many guesses they are at
bool AttemptedGuesses()
{
if (Attempts == 0) {
return true;
}
else {
std::cout << "\n--- Oh no! Wrong code, try again but be careful. You only have 3 total attempts! ---\n";
std::cout << "--- Attempts Left: " << Attempts << " ---" << std::endl << std::endl;
return false;
}
}
bool PlayGame(int Difficulty)
{
PrintIntroduction(Difficulty);
// Declare the 3 number code
const int CodeA = rand() % Difficulty + 1;
const int CodeB = rand() % Difficulty + 1;
const int CodeC = rand() % Difficulty + 1;
const int CodeSum = CodeA + CodeB + CodeC;
const int CodeProduct = CodeA * CodeB * CodeC;
// Print the sum and product to the terminal
std::cout << CodeA << CodeB << CodeC;
std::cout << "There are 3 numbers in the code\n";
std::cout << "The codes add up to: " << CodeSum;
std::cout << "\nThe codes multiply to give: " << CodeProduct << std::endl;
// Store player guess
int GuessA, GuessB, GuessC;
std::cout << "\nFirst Number of the Code: ";
std::cin >> GuessA;
std::cout << "Second Number of the Code: ";
std::cin >> GuessB;
std::cout << "Third Number of the Code: ";
std::cin >> GuessC;
std::cout << "\n\nYou entered: " << GuessA << GuessB << GuessC;
int GuessSum = GuessA + GuessB + GuessC;
int GuessProduct = GuessA * GuessB * GuessC;
// Check player message
if (GuessSum == CodeSum && GuessProduct == CodeProduct)
{
// std::cout << std::endl << Attempts << std::endl;
std::cout << "\n--- Correct! Move on to the next vault for even MORE money! ---\n";
Attempts = 3;
return true;
}
else {
Attempts = Attempts - 1;
AttemptedGuesses();
return false;
}
}
int main()
{
srand(time(NULL));
int LevelDifficulty = 1;
int const MaxDifficulty = 10;
// To display what level player failed on
int CurrentLevel = 0;
CurrentLevel = LevelDifficulty;
while (LevelDifficulty <= MaxDifficulty) // Loop game until all levels are completed.
{
bool bLevelComplete = PlayGame(LevelDifficulty);
std::cin.clear();
std::cin.ignore();
if (bLevelComplete)
{
++LevelDifficulty;
}
if (AttemptedGuesses())
{
LevelDifficulty = 15;
}
}
// To determain a win or loss
if (LevelDifficulty == 11)
{
std::cout << "--- Congrats! You emptied out the bank and are driving away in your runaway care to live a life of luxury on a private island! ---\n\n";
return 0;
}
else
{
std::cout << "\n\n--- You failed your third attempt and were caught by the police! Oh no! Better luck next time! :) ---\n";
std::cout << "--- You were on level: " << CurrentLevel << ". ---\n\n";
return 0;
}
}