How would you add a guess limit to Triplex

Hi,

Reached the end of the Triplex section of the updated course and I’m trying to further improve the game now it’s completed while waiting for the next updated section, however, 1 of the first things I wanted to do was add a guess limit but I just can’t seem to get it to work properly. I’ve tried using FOR in multiple places but most of the time either it has no effect or makes it so even if you give the right answer it’ll continue wanting you to add in more guesses until you hit the guess limit.

Is there something else you need to add in so that it’ll have the set number of guesses but recognise when you’ve got the right answer and increase the difficulty or am I just placing things in the wrong order? Best result so far has been the below but that’s the one that makes it take the full 5 guesses before moving on. I can see the issue is that it’s just being told to take the guesses until it hits 5 but tried adding an OR to the IF and that just seemed to make it not even know if the code was right or not :frowning:

int GuessA, GuessB, GuessC;
    int MaxGuesses = 5; 

    for (size_t i = 1; i <= MaxGuesses; i++)
    {
        std::cin >> GuessA >> GuessB >> GuessC;
        std::cout << std::endl;
    }
    
#include <iostream>
#include <ctime>

bool PlayGame(int Difficulty);
void PrintIntroduction();
void PrintNextLevel(int Difficulty);

int main()
{
    srand(time(NULL));

    int LevelDifficulty = 1;
    const int MaxDifficulty = 5; 

    PrintIntroduction();

      while (LevelDifficulty <= MaxDifficulty) //Loop Until All Levels Completed
    {
        bool bLevelComplete = PlayGame(LevelDifficulty);
        std::cin.clear();
        std::cin.ignore();

        if (bLevelComplete)
        {
            ++LevelDifficulty; 
        }              
    }
    std::cout << "You've obtained the information! now to get back to base so we can look over those specs.\n\n";
    return 0; 
}

void PrintIntroduction()
{                                      
    std::cout << std::endl;
    std::cout << "   @@@@@@@@@@@@  @@@@@@@@@@@@    @@@@@@@@@@@   @@@@@@@@@@    @@@           @@@@@@@@@@@@    @@@               @@@      @@@ \n";                                                       
    std::cout << "       @@@       @@@      @@@        @@@       @@@      @@@  @@@           @@@                @@@         @@@         @@@ \n";                                       
    std::cout << "       @@@       @@@      @@@        @@@       @@@      @@@  @@@           @@@                   @@@   @@@            @@@ \n";                                       
    std::cout << "       @@@       @@@@@@@@@           @@@       @@@@@@@@@@    @@@           @@@@@@@@@@@@             @@@               @@@ \n";                                                
    std::cout << "       @@@       @@@    @@@          @@@       @@@           @@@           @@@                   @@@   @@@            @@@ \n";                                
    std::cout << "       @@@       @@@     @@@         @@@       @@@           @@@@@@@@@@@@  @@@                @@@         @@@             \n";                                        
    std::cout << "       @@@       @@@      @@@    @@@@@@@@@@@   @@@           @@@@@@@@@@@@  @@@@@@@@@@@@    @@@               @@@      @@@ \n"; 

    //Printing introduction To Terminal
    std::cout << std::endl;
    std::cout << "Greetings, you are the number 1 spy in Biotech, and we have a job for you...\n";
    std::cout << "Your target is the plans for Tangent Technologies latest in plasma weaponry, we can't rely on just our implants to keep us ahead!\n";
    std::cout << "You are at the 1st layer of firewall, you need to work out the correct code to break through!\n\n";
}

void PrintNextLevel(int Difficulty)
{  
        std::cout << "Success you've cracked this layer! now you just need to crack the level " <<Difficulty +1;
        std::cout << " firewall... hurry before those TT scumbags catch you.\n\n";   
}

bool PlayGame(int Difficulty)
{         

    //Declaring 3 Digit Code 
    const int CodeA = rand() % Difficulty +Difficulty; 
    const int CodeB = rand() % Difficulty +Difficulty;  
    const int CodeC = rand() % Difficulty +Difficulty;

    const int CodeSum = CodeA + CodeB + CodeC;
    const int CodeProduct = CodeA * CodeB * CodeC;

    //Print CodeSum And CodeProduct To Terminal
    std::cout << "- There are 3 digits in the code\n";
    std::cout << "- The codes add-up to: " << CodeSum << std::endl; 
    std::cout << "- And multiplies to give: " << CodeProduct << std::endl << std::endl;

    //Store Player Guess
    int GuessA, GuessB, GuessC;
    std::cin >> GuessA >> GuessB >> GuessC;
    std::cout << std::endl; 
    
    int GuessSum = GuessA + GuessB + GuessC;
    int GuessProduct = GuessA * GuessB * GuessC;

    //Checking Player Guess
    if (GuessSum == CodeSum && GuessProduct == CodeProduct) 
    {
        PrintNextLevel(Difficulty);  
        return true;
    }   
    else
    {    
        std::cout << "Rats that's not the right code... what else might it be?\n\n";  //Add Set Number Of Tries
        return false;       
    }
    
    
}

Added entire thing in case it helps.

Anybody got an idea :frowning:

Sorry I missed this, though I’m not sure what you’re after.

Increasing the difficulty after X amount of tries?

Hi,

No what I’m looking for is a way to make it so at each level you have a set number of times to get the correct code and if you fail to often you lose and need to start over. Currently you have an infinite amount of guesses

I don’t want to give you the complete answer but I will tell you about a way that will potentially make your current code be able to work.

Screenshot%20(11)

Thanks :slight_smile: I’ll give it a go with that.

This topic was automatically closed after 5 days. New replies are no longer allowed.

Privacy & Terms