Triple X Portal Game progress

Hi there I finished the Triple X lessons and tried to create the game from stretch again without watching the video to memorize everything. I come from 0 scripting experience… I am trying to add a few functions to my version of the game by giving the player coordinates after each correct solved task and at the end have the player type in all the collected coordinates to ultimately finish the game.
Question how can I improve this script and how do I add the function of checking if the player has entered the correct coordinates at the end. Pretty sure Ill have to declare the coordinates and then at the end add a if/else function to check if the correct numbers where typed in and send a message accordingly… Every-time I try doing it I get various errors…
Here is the script without any errors so far but with the last missing function.

#include<iostream> 
#include<ctime> 

void GameIntroduction() //Game Introduction Message
{
 std::cout << "___________________________________________________________________\n";
 std::cout << "|                                                                 |\n";
 std::cout << "|                         WELCOME STRANGER                        |\n";
 std::cout << "|                                                                 |\n";
 std::cout << "| Looks like your Portal Navigation Processor broke down!         |\n";
 std::cout << "| You have to manually overwrite the system by decoding the last  |\n";
 std::cout << "| 5 module sequences in this Terminal                             |\n";
 std::cout << "|                                                                 |\n";
 std::cout << "| If the Sequence is correct you will be givin the first numbers  |\n";
 std::cout << "| of the cooardinates. At the end you will have to fill in all    |\n";
 std::cout << "| numbers in the master console to rewrite the Portal Device.     |\n";
 std::cout << "|                                                                 |\n";
 std::cout << "| Once you have the cooardinates you will be able to teleport back|\n";
 std::cout << "| to your ship!                                                   |\n";
 std::cout << "|                                                                 |\n";
 std::cout << "| Good Luck Stranger!                                             |\n";
 std::cout << "|_________________________________________________________________|\n";
 std::cout << std::endl;
 std::cout << std::endl;
 std::cout << "\n======================================================================\n";
 std::cout <<   "|                                                                    |\n";
 std::cout <<   "|                  Press ENTER to start decoding...                  |\n";
 std::cout <<   "|                                                                    |\n";
 std::cout <<   "======================================================================\n\n";
}

void GameStartMessage(int Difficulty) // Prints the Starting message with instructions on how to play
{
   std::cout << "\nMODULE: " << Difficulty;
    std::cout << "\n======================================================================\n";
    std::cout <<   "|    You need to decode this Module                                   |\n";
    std::cout <<   "|    Enter the correct 3 digit code get access to the cooardinates    |\n";
    std::cout <<   "|    Seperate each digit by one space                                 |\n";
    std::cout <<   "======================================================================\n\n";
}

bool PlayGame(int Difficulty)
{


    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 CodeMulti = CodeA * CodeB * CodeC;

    //print CodeSum and CodeMulti to the terminal
    std::cout << std::endl;
    std::cout << "\nMODULE: " << Difficulty;
    std::cout << "\n======================================================================\n";
    std::cout <<   "|    You need to decode this Module                                   |\n";
    std::cout <<   "|    Enter the correct 3 digit code get access to the cooardinates    |\n";
    std::cout <<   "|    Seperate each digit by one space                                 |\n";
    std::cout <<   "|    The code numbers add up to: " << CodeSum << "                    |\n";
    std::cout <<   "|    The code multiplies to " << CodeMulti << "                       |\n";
    std::cout <<   "======================================================================\n\n";
    std::cout << std::endl;
    std::cout << std::endl;
    
    //Store player guess
    int GuessA, GuessB, GuessC;    
    std::cin >> GuessA >> GuessB >> GuessC;

    int GuessSum = GuessA + GuessB + GuessC;
    int GuessMulti = GuessA * GuessB * GuessC;

    if (GuessSum == CodeSum && GuessMulti == CodeMulti && Difficulty == 1)
    {
        std::cout << "\n======================================================================\n";
        std::cout <<   "|               Cooardinates Decoding... 41 ### ## # ###             |\n";
        std::cout <<   "======================================================================\n\n";
        return true;
    }

    else
    {
        std::cout << "\n======================================================================\n";
        std::cout <<   "|               Wrong Code! Decoding process canceled                  |\n";
        std::cout <<   "======================================================================\n\n";
        return false;

    }
    if (GuessSum == CodeSum && GuessMulti == CodeMulti && Difficulty == 2)
    {
        std::cout << "\n======================================================================\n";
        std::cout <<   "|               Cooardinates Decoding... 41 223 ## # ###             |\n";
        std::cout <<   "======================================================================\n\n";
        return true;
    }

    else
    {
         std::cout << "\n======================================================================\n";
        std::cout <<   "|               Wrong Code! Decoding process canceled                  |\n";
        std::cout <<   "======================================================================\n\n";
        return false;

    }
    if (GuessSum == CodeSum && GuessMulti == CodeMulti && Difficulty == 3)
    {
        std::cout << "\n======================================================================\n";
        std::cout <<   "|               Cooardinates Decoding... 41 223 17 # ###             |\n";
        std::cout <<   "======================================================================\n\n";
        return true;
    }

    else
    {
        std::cout << "\n======================================================================\n";
        std::cout <<   "|               Wrong Code! Decoding process canceled                  |\n";
        std::cout <<   "======================================================================\n\n";
        return false;

    }
    if (GuessSum == CodeSum && GuessMulti == CodeMulti && Difficulty == 4)
    {
        std::cout << "\n======================================================================\n";
        std::cout <<   "|               Cooardinates Decoding... 41 223 17 8 ###             |\n";
        std::cout <<   "======================================================================\n\n";
        return true;
    }

    else
    {
          std::cout << "\n======================================================================\n";
        std::cout <<   "|               Wrong Code! Decoding process canceled                  |\n";
        std::cout <<   "======================================================================\n\n";
        return false;

    }
    if (GuessSum == CodeSum && GuessMulti == CodeMulti && Difficulty == 5)
    {
        std::cout <<  "\n======================================================================\n";
        std::cout <<   "|               Decoding Completed!  41 223 17 8 197                   |\n";
        std::cout <<   "|      Please enter the Cooardinates below to activate the Portal      |\n";
        std::cout <<   "======================================================================\n\n";
        return true;
    }

    else
    {
          std::cout << "\n======================================================================\n";
        std::cout <<   "|               Wrong Code! Decoding process canceled                  |\n";
        std::cout <<   "======================================================================\n\n";
        return false;

    }

}

int main()
{
    GameIntroduction();
    {
        std::cin.get(); // Waits for player input

    }
    srand(time(NULL));
    int LevelDifficulty = 1;
    int const MaxDifficulty = 5;

    while (true)
    {
        bool bLevelComplete = PlayGame(LevelDifficulty);
        std::cin.clear(); // Clears any errors
        std::cin.ignore(); // Discards the buffer

        if (bLevelComplete)
        {
           ++LevelDifficulty; // increase the level difficulty
        }
        
    }
    

    std::cin.get(); // Waits for player input


   
       return 0;

}
1 Like

Hi and Welcome to the course. Sorry for the lateness of the reply. I hope you’re enjoying it so far.

Are you talking about separating the code out into smaller functions? So far it looks like you’re heading in the right direction.

You’re repeating yourself quite a bit. One example is this

if (GuessSum == CodeSum && GuessMulti == CodeMulti && Difficulty == 1)
{
    // code
}
else
{
    std::cout << "\n======================================================================\n";
    std::cout <<   "|               Wrong Code! Decoding process canceled                  |\n";
    std::cout <<   "======================================================================\n\n";
    return false;
}

The condition GuessSum == CodeSum && GuessMulti == CodeMulti and the code within the else is repeated multiple times. It can be simplied to

if (GuessSum == CodeSum && GuessMulti == CodeMulti)
{
    if (Difficulty == 1)
    {
        // message
    }
    else if( Difficulty == 2)
    {
        // message
    }
    // rest of difficulties
    return true;
}
else
{
    // error mesage
    return false;
}

The single line boarder can also be easily simplified with C++20 and <format>

#include <format>
#include <iostream>
#include <string_view>

void PrintMessage(std::string_view Messsage)
{
   	std::cout << "\n======================================================================\n";
	std::cout << std::format("|{:^68}|\n", Message);
	std::cout << "======================================================================\n\n";
}

int main()
{
    PrintMessage("Cooardinates Decoding... 41 ### ## # ###");
}

For example that would print the message for Difficulty == 1.

  • The parameter std::string_view is a little hard to explain to a beginner but in a nutshell it allows to efficiently pass in any kind of string.
  • Breaking down std::format("|{:^68}|\n", Message)
    • The first argument is for the “format string” which is the | for the border on either side.
    • {:^68} the entire {} is to replace this with Message, the second argument to std::format here.
    • The : part separates the positional argument (starts at 0) with the formatting part.
       std::format("{1} {0}", 1, 2) // 2 1
                          ^---^0 
                     ^------------^1
      
      You can omit it and it will default to the corresponding position of that {}
    • ^ is to centre the replacement text, 68 is for the width and will use spaces as the fill character. I counted your boarder to be a width of 70, so minus two | it should be 68.

It requires the latest version of Visual Studio (other implementations haven’t implemented std::format at the time of writing); and compiling with /std::c++latest e.g.

cl triplex.cpp /std:c++latest

I think it would be helpful if you show what you had that gave you the errors.

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

Privacy & Terms