Sec 2 Part 29 - my code so far

Here is my code for section 2 part 29.

I would like some suggestions on handling the play again function. I know this is outside the scope of the course at this point but any ideas would be appreciated. The issue is that I can seem to find a way to restart the whole game again. I tried calling main() however it doesn’t work due to in coming before the main function. The inverse happens if I move PlayAgain() after main().

I have found forums topics that offer some suggestions however they go deeper into the C++ than I’m ready for, if I can get the answer within iostream that would be ideal

--------------------------------------------------------------------------------------------------------------------------

For anyone interested in the bits I added that weren’t discussed in the course. (The more knowledgeable, feel free to correct me where I could have done or explained better. This is as I understand them)

PLAYER PROMPT / PAGE PAUSE
std::cout << “enter to continue…”; (you know this line)

std::cin.get(); (similar to the one used to course. .get() reads the white-space allowing you to use ENTER instead of doing nothing until you enter something)

if (true) (I used this if statement as the pause. For some reason the std::cin.get() would steamroll along, but asking the program to see if it the std::cin.get () was true would stop it cold. Don’t know if it was right but it worked)
{
system(“CLS”); ( this clears the terminal screen. The system command is classified as an OS command so I am guessing that it is built in the C++ itself where as everything else was created by outside contributors? )
}

The "\n"x 10: I did this as spacers. I figured out that my screen displays 40 lines of code so I used this to to help with the aesthetics. This works on my screen… can’t say it would work on yours. Again if there was a better way to do this, please let me know.

Lastly, if you want to run the code yourself get rid of the PlayAgain() function. Its not working. Last time I checked everything else did.

-Cheers
--------------------------------------------------------------------------------------------------------------------------

#include<iostream>

int AsciiArtTitle()
{
    //NEW SCREEN: PAGE 1 (TITLE SCREEN)
    std::cout << 
     "----------------------------------------------------------------------------------------------------\n"
     "\n"
     "   *@@@@@@@@@@@@@@@@@@@@@@@@&,   /@@@@@@@@@@@@@&,  .%@@@@@@@@@@@@@#  .%@@@@@@@@@@@@@@@@@@@@@@@@*    \n"
     "   %@@@@%/,(@@@@@@@@@*,(&@@@@(     .&@@@@@@@@%        /@@@@@@@@@/       @@@@@@@@@@@#,...*#&@@@@&   \n"
     " *@&*     /@@@@@@@@@,     #@&.    .&@@@@@@@@*        *@@@@@@@@@*       @@@@@@@@@%.          (@@*    \n"
     " #(       /@@@@@@@@@,      .%/    .&@@@@@@@@*        *@@@@@@@@@*       @@@@@@@@@%.      %%    #&    \n"
     " .        /@@@@@@@@@,             .&@@@@@@@@@@@@@@@@@@@@@@@@@@@*       @@@@@@@@@@@@@@@@@@@,    .    \n"
     "          /@@@@@@@@@,             .&@@@@@@@@*        /@@@@@@@@@*       &@@@@@@@@&**** %@@@(         \n"
     "          /@@@@@@@@@,             .&@@@@@@@@*        /@@@@@@@@@*       &@@@@@@@@%.      .%@.        \n"
     "          /@@@@@@@@@,             .%@@@@@@@@*        /@@@@@@@@@*       &@@@@@@@@%.        ,.        \n"
     "          /@@@@@@@@@,             .%@@@@@@@@/        /@@@@@@@@@*       &@@@@@@@@%.              .&* \n"
     "          /@@@@@@@@@,             .%@@@@@@@@/        /@@@@@@@@@*       &@@@@@@@@%.             %@/  \n"
     "          /@@@@@@@@@,             .%@@@@@@@@/        /@@@@@@@@@*       &@@@@@@@@@(         .(@@@#   \n"
     "         .%@@@@@@@@@#             /@@@@@@@@@%.       #@@@@@@@@@#      ,@@@@@@@@@@@@@&&&&@@@@@@@#    \n"
     "      .#&&&&&&&&&&&&&&&#        *%%%%%%%%%%%%%%,  .#%%%%%%%%%%%%%(  ,%%%%%%%%%%%%%%%%%%%%%%%%&&.    \n" 
    <<std::endl << std::endl;
 
    std::cout <<
     "   /@@@@@@@@@@@,    @@@@&@@@@@@&@@@(         /@@@@@@*         .&@@@@&%&@@@@     @@@@@@,    &@@@@@.  \n"
     " ,@@@@@/      &#   ,%    @@@@@%   .@        ,@@@@@@@@.       %@@@@@      .@.    #@@@@@     (@@@@@   \n"
     " /@@@@@@@@&*             @@@@@%            ,@@  @@@@@@       @@@@@@@@@%*        #@@@@@@@@@@@@@@@@   \n"
     "   @@@@@@@@@@@@@#        @@@@@%           ,@@%/..@@@@@@       ,@@@@@@@@@@@@@,   #@@@@@     %@@@@@   \n"
     "./      .(@@@@@@@(       @@@@@%          ,@@,.,&@@@@@@@%    #       .#@@@@@@@.  #@@@@@     %@@@@@   \n"
     " @&        #@@@@@,       @@@@@%         ,@@*      @@@@@@(   *@(        @@@@@@   #@@@@@     %@@@@@   \n"
     " @@@@@@&&@@@@@@,        /@@@@@@        #@@&       ,@@@@@@(   @@@@@@@@@@@@@%     @@@@@@,    @@@@@@,  \n" 
     "\n"
     "-----------------------------------------------------------------------------------------------------\n"
    <<std::endl << std::endl;
 
    std::cout << 
     "\n\n\n\n\n\n\n\n\n\n"
     "enter to continue...";
     std::cin.get();
     if (true)
     {
     system("CLS"); 
     }

    return 0;
}

void GameIntro()
{
    //NEW SCREEN: PAGE 2 (GAME INTRO)
    AsciiArtTitle();
    
    std::cout <<
     "----------------------------------------------------------------------------------------------------------------------\n"
     "\n"
     "It's 1980 and puberty has hit you hard during the night...\n" "\n"
     "The day before you thought the opposite gender gross and icky but now you're curious to know more about them.\n"
     "You sit there in you Star Wars jammies, one size too small, wondering where you would find the answers you need.\n"
     "Then you remembered that your older siblings, those muppets, have something called a stash...\n" 
     "\n"
     "You've only ever seen it in passing, a breif glimpse through a crack in the door, sometimes poking out from under the bed\n"
     "always stuffed with so many things that only they and their friends could look at and use.\n"
     "Strange little plastic bags, weird shaped objects, shiny pieces of foil and the things that really caught your interest\n" 
     "\n"
     "...magazines...\n" 
     "\n"
     "Those covers hinted that everything you wanted to know was within those pages, but to get to them, to get that knowledge,\n" 
     "you had to into their room , get past the combination locks and not get caught!\n"
     "If you did... whatever happened to you would have the word ATOMIC in its name\n" 
     "\n"
     "Your goal was clear. Figure out the correct combinations to get deeper into the STASH.\n"
     "\n"
     "----------------------------------------------------------------------------------------------------------------------\n"
    << std::endl << std::endl;

    std::cout << 
     "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
     "enter to continue...";
     std::cin.get();
    
     if (true)
        {
         system("CLS"); 
        }
    return;
}

void GameCompleteOutro()
{
     std::cout <<
     "----------------------------------------------------------------------------------------------------------------------\n"
     "CONGRATULATIONS!\n"
     "\n"
     "With the last lock off you now have access to the stash.\n"
     "You rummage through tge various bric-a-brac until you get to the bottom where you find the magazines.\n"
     "Should you look now or wait till you're safely behind the lock door of your room?\n"
     "Curiosity was always going to get the better of you..."
     "\n"
     "\n"
     "A golden glow bathes you as you open the magazine...\n"
     "----------------------------------------------------------------------------------------------------------------------\n"
    << std::endl;
}

bool PlayGame(int Difficulty)
{
    const int CodeA = 4;
    const int CodeB = 3;
    const int CodeC = 5;
    const int CodeSUM = CodeA + CodeB + CodeC;
    const int CodePRODUCT = CodeA * CodeB * CodeC;

    //NEW SCREEN: PAGE 3 (GAME START)
    std::cout << 
     "Written on a scrap paper stuck to the back of the box is a note, a reminder for them, but a clue for you...\n"
     "\n" 
     "-------------------------------------------\n"
     "- There are three (3) numbers in the code -\n"
     "- The SUM of the numbers is: "<< CodeSUM << "           -\n"
     "- The PRODUCT of the numbers is: " << CodePRODUCT <<"       -\n"
     "-------------------------------------------\n" 
    << std::endl;
    
    std::cout << std::endl;
    std::cout << std::endl;
    
    std::cout <<
     "     .-----------.    \n"
     "    |  .------.  |    \n"
     "    | |        | |    \n"
     "    | |        | |    \n"
     "   _| |________| |_   \n"
     " .' |_|        |_| '. \n"
     " '._____ ____ _____.' \n"
     " |     .'____'.     | \n"
     " '.__.'.'    '.'.__.' \n"
     " '.__  |  0"
     << Difficulty <<
     "  |  __.' \n"
     " |   '.'.____.'.'   | \n"
     " '.____'.____.'____.' \n"
     " '.________________.'" 
    <<std::endl;

    std::cout << std::endl;
    std::cout << std::endl;

    int GuessA; 
    int GuessB; 
    int GuessC;
        
    std::cout <<
     "You take the lock nervously in your hands and begin to fidget with it.\n" 
     "\n"
     "You turn the first dial...\n"
     "then the second...\n"
     "and finally the third dail to: \n"
     "\n"
    << std::endl;
     
    std::cin >> GuessA;
    std::cin >> GuessB;
    std::cin >> GuessC;

    std::cin.get();
    
    if(true)
    {
        system("CLS"); 
    }
    
    //NEW SCREEN: PAGE 4 (NUMBER CONFIRMATION)
    std::cout << 
     "The dials reads: "
     << GuessA << GuessB << GuessC <<
     "\n" 
     "\n"
     "\n"
     "AND..."
     "\n"
     "\n"
     "\n"
     "\n"
    << std::endl;
    
    int GuessSUM = GuessA + GuessB + GuessC;
    int GuessPRODUCT = GuessA * GuessB * GuessC;

    std::cout << 
     "\n\n\n\n\n\n\n\n\n\n"
     "\n\n\n\n\n\n\n\n\n\n"
     "\n\n\n\n\n\n\n\n\n\n"
     "enter to continue...";
    std::cin.get();
    
    if(true)
    {
        system("CLS"); 
    }
    
    // NEW SCREEN: PAGE 5 (GAME RESULTS)
    if(GuessSUM == CodeSUM && GuessPRODUCT == CodePRODUCT)
    {
        std::cout <<
         "The spring pops on the first lock! You are hit with a wave of relief and excitment.\n"
         "\n"
         "You think that you might just get away with this afterall....\n"
         "\n\n\n\n\n\n\n\n\n\n"
         "\n\n\n\n\n\n\n\n\n\n"
         "\n\n\n\n\n\n\n\n\n\n"
         "\n\n\n\n"
         "Pick up the next Lock\n"
         "enter to continue...";
        return 1;
    }
    else
    {
        std::cout << 
         "----------------------------------------------------------------------------------------------------------------------\n"
         "\n"
         "Nothing happens.\n" 
         "\n"
         "Before you can try again the world goes dark.\n"
         "Strange voices echo through the darkness, followed by a shooting pain that makes a laps from your neck down your back and up again\n"
         "\n"
         "\"I'm sorry ma'am... there is no telling when the coma will end.\"\n"
         "\n"
         "\"Doctor please there must be something you can tell me...\" That sounds like your mother...\n"
         "\n"
         "\"Sadly there isn't. There has never been an ATOMIC WEDGIE of this scale ever record in any medical literature.\n"
         "All we can do is wait and hope...\"\n"
         "\n"
         "----------------------------------------------------------------------------------------------------------------------"
         "\n\n\n\n\n\n\n\n\n\n"
         "\n\n\n\n\n\n\n\n\n\n"
         "\n\n\n"
         "Try again in the next life\n"
         "enter to continue...";        
        return 0;
    }

}

bool PlayAgain()
{
    char PlayerChoice;
    bool RoundTwo;

    std::cout <<
     "Would you like to play again? Y / N"
    << std::endl;

    std::cin >> PlayerChoice;

    if(PlayerChoice == 'Y' || PlayerChoice == 'y' )
    {
        RoundTwo = 1;
    }
    else if(PlayerChoice == 'N' || PlayerChoice == 'n')
    {
        RoundTwo = 0;         
    }

}

int main()
{
    system("CLS");

    GameIntro();

    int LevelDifficulty = 1;
    const int NumberOfLvls = 3;

    while(LevelDifficulty <= NumberOfLvls)
    {
     bool bLevelComplete = PlayGame(LevelDifficulty);

     std::cin.clear();
     std::cin.ignore();

        if(bLevelComplete)
        {
            ++LevelDifficulty;

            system("CLS");

            if(LevelDifficulty > NumberOfLvls)
            {
                GameCompleteOutro();
                
                std::cin.get();

                return 0;
            }
        }
        else 
        {
            system("CLS");
            return 0;
        }
    }

    PlayAgain();

    return 0;
}

Just to note, never call main(). The C++ standard forbids it so you would have non-conforming code
https://eel.is/c++draft/basic.start#main-3


  1. PlayAgain() doesn’t return a value.
  2. What if Y/y or N/n isn’t entered?
  3. return statements return from functions i.e. exit them. Meaning
    if(LevelDifficulty > NumberOfLvls)
    {
        GameCompleteOutro();                
        std::cin.get();
        return 0;
    }
    
    And the others you have in main will terminate the program at the return.
  4. You aren’t doing anything with PlayAgain after you call it.

Thank you, it gives me something to think about.

Privacy & Terms