My first C++ game

#include <iostream>
#include <ctime>

// Function to print the introduction message
void PrintIntroduction(int& LevelDifficulty) 
{
  // Print welcome message to the user, and introduce to the story.
  std::cout << "\n\nYou are a secret agent breaking into a secret lab. Level difficulty is " << LevelDifficulty << "." << std::endl;
  std::cout << "You find a keypad and a code.\n";
  std::cout << "Enter the code to get in: ";
}

// Function to print the game complete message
void PrintGameComplete(int& LevelDifficulty) 
{
  // Print the game complete message
  std::cout << "\n\nYou have successfully completed the game.\n";
  std::cout << "You have completed level " << LevelDifficulty << "." << std::endl;
  std::cout << "Thank you agent for your service.\n";
}

// Function to generate a random number by level difficulty
int RandomIntegerGenerator(int& LevelDifficulty) 
{
  // Generate a random number by LevelDifficulty
  return rand() % LevelDifficulty + LevelDifficulty;
}

// Initialize the game variables
void InitializeTheGameVariables(int& LevelDifficulty, int& CodeSum, int& CodeProduct)
{
  // Randomly Initialize the CodeA, CodeB, CodeC variables by LevelDifficulty
  const int CodeA = RandomIntegerGenerator(LevelDifficulty);
  const int CodeB = RandomIntegerGenerator(LevelDifficulty);
  const int CodeC = RandomIntegerGenerator(LevelDifficulty);

  // Calculate the sum and product of the code values
  // and store them in the variables CodeSum and CodeProduct
  CodeSum = CodeA + CodeB + CodeC;
  CodeProduct = CodeA * CodeB * CodeC;

  // Print the sum and product of the CodeSum and CodeProduct
  std::cout << "There are 3 numbers in the code.\n";
  std::cout << "The codes add up to " << CodeSum << ".\n";
  std::cout << "The codes multiply to " << CodeProduct << ".\n";
}

// Get the user's guess and put them in the variable GuessSum and GuessProduct
void GetTheUserGuess(int& GuessSum, int& GuessProduct)
{
  // Store player guesses, GuessA, GuessB, GuessC
  int GuessA, GuessB, GuessC;
  std::cin >> GuessA >> GuessB >> GuessC;

  // Calculate the sum and product of the guess values
  // and store them in the variables GuessSum and GuessProduct
  GuessSum = GuessA + GuessB + GuessC;
  GuessProduct = GuessA * GuessB * GuessC;

  // Print the sum and product of the GuessSum and GuessProduct
  std::cout << "You have guessed " << GuessSum << " and " << GuessProduct << ".\n";
}

// Function to play the round
bool PlayRound(int& LevelDifficulty)
{
  // Call PrintIntroduction function to print the introduction message
  PrintIntroduction(LevelDifficulty);
  
  // Declare variables for the CodeSum, CodeProduct, GuessSum, GuessProduct
  int CodeSum, CodeProduct, GuessSum, GuessProduct;

  // Initialize the game variables
  InitializeTheGameVariables(LevelDifficulty, CodeSum, CodeProduct);
  
  // Get the user's guess
  GetTheUserGuess(GuessSum, GuessProduct);

  // Check if the user's guess is correct
  if (GuessSum == CodeSum && GuessProduct == CodeProduct)
  {
    // Print the correct message
    std::cout << "You have guessed correctly!\n";
    return true;
  }
  else
  {
    // Print the incorrect message
    std::cout << "You have guessed incorrectly!\n";
    return false;
  }
}

// Function to play the game
void PlayGame()
{
  // Initialize LevelDifficulty variable
  int LevelDifficulty = 1;

  // Initialize the MaxLevelDifficulty variable
  const int MaxLevelDifficulty = 5;

  while(LevelDifficulty <= MaxLevelDifficulty)
  {
    // Call PlayRound function to play the round
    bool bLevelComplete = PlayRound(LevelDifficulty);
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    // Check if the user has completed the level
    if (bLevelComplete)
    {
      // Print the level complete message
      std::cout << "You have completed the level!\n";

      // Increase the level difficulty
      LevelDifficulty++;
    }
    else
    {
      // Print the level failed message
      std::cout << "You have failed the level!\n";

      // Decrease the level difficulty
      LevelDifficulty--;
    }
  }

  // Print the game complete message
  PrintGameComplete(--LevelDifficulty);
}

// The main function, which is the entry point of the program.
int main()
{ 
  // Seed the random number generator
  srand(time(NULL));

  // Call the PlayGame function to play the game
  PlayGame();

  // Return 0 to the operating system
  return 0;
}
// The end of the main function.

Privacy & Terms