Main: My Two Cents

Just starting the course so here’s what I think…

The main function sets the starting and maximum difficulty for the game. Also sets the difficulty to increase if the player passes the level and tells the game to not increase the difficulty when they reach the maximum level.

std::cout << I think this is displayed when the player passes the main level, printing the message “Wow…” for the player to see.

Maybe. Maybe not!

Const declares a variable that will not change throughout the code

Preprocessor Directive: Is running already written code before running our code.

Main function: This is a declaring a function which we have named ‘Main’

Expression Statements 1: Are introducing a scenario for the gameplay to the player.

Declaration Statements: We’re declaring integers that will be used to create the gameplay. We are declaring new variables that add or get the product of the integers, which will form one part of the gameplay.

Expression Statements 2: We are printing these new variables to the terminal… maybe they’ll be what the player needs to use? Or the answer?

Return Statement: When the player wins / Game over.

int main()
{
  // Print welcome messages to the terminal
  std::cout << std::endl;
  std::cout << "You are EU President Ursula von der Leyern.\n The EU ombudsman wants to see your phone...\n...to read your SMS messages with Albert Bourla, CEO of Pfizer.\n You desparately need to delete them pronto...\n...and of course you've forgotten the pin for your phone.\nYou better be quick, Ursula, the EU ombudsman...\n...will be here any second now! Good luck...\n...using our latest PIN code hacker software: TRIPLE X!";
  std::cout << std::endl;
  std::cout << "Enter the correct code to continue..." << std::endl;
  
  // Declare 3 number code
  const int CodeA = 1;
  const int CodeB = 2;
  const int CodeC = 3;

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

  // Print Sum and Product to the terminal
  std::cout << std::endl;
  std::cout << "+ There are 3 numbers in the code" << std::endl;
  std::cout << "+ The codes add-up to: " << CodeSum << std::endl;
  std::cout << "+ The codes multiply to give: " << CodeProduct << std::endl;

  int GuessA, GuessB, GuessC;

  std::cout << std::endl;
  std::cin >> GuessA;
  std::cin >> GuessB;
  std::cin >> GuessC;
  std::cout << std::endl;

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

  if (GuessSum == CodeSum && GuessProduct == CodeProduct)
  {
    std::cout << "You're in! You delete the SMS messages and keep your job. Congrats!";
  }
  else
  {
    std::cout << "It's too late, the Ombudsman is right behind you! And she doesn't look too happy...";
  }

  return 0;
}

#include <iostream>

#include <ctime>

// Print welcome messages to the terminal

void PrintIntroduction(int Difficulty)

{

  std::cout << std::endl;

  std::cout << "\n\n ! ! ! ! ! =^_^=;; ! ! ! ! !\nYou are EU President Ursula von der Leyern.\n The EU ombudsman wants to see your phone...\n...to read your SMS messages with Albert Bourla, CEO of Pfizer.\n You desparately need to delete them pronto...\n...and of course you've forgotten the pin for your phone.\nYou better be quick, Ursula, the EU ombudsman...\n...will be here any second now! Good luck...\n...using our latest PIN code hacker software: TRIPLE X!\n\n";

  std::cout << "Enter the correct code to continue... Level: " << Difficulty << std::endl << std::endl;

}

bool PlayGame(int Difficulty)

{

  PrintIntroduction(Difficulty);

    // Declare 3 number 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 Sum and Product to the terminal

  std::cout << "+ There are 3 numbers in the code";

  std::cout << "\n+ The codes add-up to: " << CodeSum;

  std::cout << "\n+ The codes multiply to give: " << CodeProduct << std::endl;

  // Store player guess

  int GuessA, GuessB, GuessC;

  std::cin >> GuessA >> GuessB >> GuessC;

  int GuessSum = GuessA + GuessB + GuessC;

  int GuessProduct = GuessA * GuessB * GuessC;

  // Check if the players guess is correct

  if (GuessSum == CodeSum && GuessProduct == CodeProduct)

  {

    std::cout << "\nCode-Hack <<-SUCCESS->>";

    return true;

  }

  else

  {

    std::cout << "Eek! No bueno! But it's not too late. Try again! Quickly!";

    return false;

  }

}

int main()

{

  srand(time(NULL));

 

  int LevelDifficulty = 1;

  int const MaxDifficulty = 5;

  while (LevelDifficulty <= MaxDifficulty) // Loop the game until all levels are completed

  {

 

    bool bLevelComplete = PlayGame(LevelDifficulty);

    std::cin.clear();

    std::cin.ignore();

    if (bLevelComplete)

    {

      ++LevelDifficulty;

    }

  }

  std::cout << "\n\n*** Congrats, you get to keep your job. ***";

  return 0;

}

Privacy & Terms