Spaceship escape - triplex code

I’ve just completed the TripleX section of the course and I’ve been keeping it updated throughout the section.

Here is the final code for the game:

#include

#include

void PrintIntroduction(int Difficulty){

std::cout << "\n\nYou have awoken from a coma in a fancy spaceship that is about to collide into a planet you are unfamilar with. \n";

std::cout << "\n\nYou have to get a parashute, but first you must get through 6 layers of security, by bypassing the locks using the keypad in Level " << Difficulty;

}

bool PlayGame(int Difficulty){

PrintIntroduction(Difficulty);

// This is the story of my game.



// This is a set of number variables I will use.

const int CodeA = rand() % Difficulty + 1 + 1;

const int CodeB = rand() % Difficulty + 1 + 1;

const int CodeC = rand() & Difficulty + 1 + 1;

// This does the calculation and provides an answer.

const int CodeSum = CodeA + CodeB + CodeC;

const int CodeProduct = CodeA * CodeB * CodeC;

std::cout << std::endl;

std::cout << "\nThere are 3 numbers in the code \n.";

std::cout << "\nThe numbers add up to: " << CodeSum;

std::cout << "\nThe numbers multiply to: " << CodeProduct << std::endl;

int playerGuess;

int GuessA, GuessB, GuessC;

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

std::cout << "You guessed: " << GuessA << GuessB << GuessC;

int GuessSum;

int GuessProduct;

GuessSum = GuessA + GuessB + GuessC;

GuessProduct = GuessA * GuessB * GuessC;

// Checks if player guess is correct.

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

{

    std::cout << "\nYou Win!\n";

    std::cout << "\nGoing to level " << Difficulty << "\n";

    return true;

}

else{

    std::cout << "\nYou lose!\n";

    std::cout << "\nRetrying level " << Difficulty << "\n";

    return false;

}

}

int main()

{

srand(time(NULL));

int LevelDifficulty = 1;

const int maxDifficulty = 14;

while (LevelDifficulty <= maxDifficulty) // loops the game until false.

{

    bool bLevelComplete = PlayGame(LevelDifficulty);

 std::cin.clear();

 std::cin.ignore();

 if (bLevelComplete)

 {

    ++LevelDifficulty;

 }

 

}

std::cout << "Congrats on completing the game";

return 0;

}

1 Like

Congratulations on completing the section!

Privacy & Terms