Here’s my final TripleX game. I made an adjustment so if the user types in numbers that match the product/sum but aren’t the specific numbers chosen, they lose. I don’t know if the product & sum of a set of 3 numbers is a one-to-one function or not, but I haven’t gotten it to execute that particular clause yet.
Also meant I had to check if the guesses matched the code in any order.
#include <iostream>
#include <ctime>
const int DIFFICULTY_SCALE = 2; // overall difficulty
const int MAX_DIFFICULTY = 5; // number of levels
/**
* Determines if guess of 3 numbers is correct, in any order
*/
bool IsCorrectGuess(const int Code[], int Guess[])
{
// I don't want to implement pop for an array, so here's a brute force solution
// grab every permutation of guess
int Permutations[6][3] = { { Guess[0], Guess[1], Guess[2] },
{ Guess[0], Guess[2], Guess[1] },
{ Guess[1], Guess[2], Guess[0] },
{ Guess[1], Guess[0], Guess[2] },
{ Guess[2], Guess[0], Guess[1] },
{ Guess[2], Guess[1], Guess[0] } };
// check each permutation against Code
for (int i = 0; i < 6; i++) {
if (Permutations[i][0] == Code[0] && Permutations[i][1] == Code[1] && Permutations[i][2] == Code[2]) {
return true; // if it passes, exit with true
}
}
return false; // otherwise, return false
}
/**
* Drives each level of the game
*/
int PlayGame(int Difficulty)
{
// initialize answer numbers
const int Code[3] = {
1 + (rand() % (Difficulty * DIFFICULTY_SCALE)),
1 + (rand() % (Difficulty * DIFFICULTY_SCALE)),
1 + (rand() % (Difficulty * DIFFICULTY_SCALE))
};
// calculate code sum & product
const int CodeSum = Code[0] + Code[1] + Code[2];
const int CodeProduct = Code[0] * Code[1] * Code[2];
// print the prompt, listing CodeSum and CodeProduct
std::cout << "Guess a set of three and I may set you free!\n";
std::cout << "This set of three is of difficulty " << Difficulty << std::endl;
std::cout << "The sum of these is a mere " << CodeSum << std::endl;
std::cout << "But the product of these is an unfathomable " << CodeProduct << std::endl;
// read in user guess
int Guess[3];
std::cin >> Guess[0] >> Guess[1] >> Guess[2];
// calculate guess sum & product
int GuessSum = Guess[0] + Guess[1] + Guess[2];
int GuessProduct = Guess[0] * Guess[1] * Guess[2];
// if the numbers match in any order, user can proceed
bool bActuallyCorrect = IsCorrectGuess(Code, Guess);
// if the sums & products match, user is kinda correct but still has to redo the level
bool bKindaCorrect = GuessSum == CodeSum && GuessProduct == CodeProduct;
// use booleans to return outcome code
if (bActuallyCorrect) {
return 0;
} else if (bKindaCorrect) {
return 1;
} else {
return 2;
}
}
/**
* This game prompts the user for a correct set of numbers
*/
int main()
{
srand(time(NULL)); // seed random number generator with current time
// print intro messages to terminal
std::cout << "A withered wisp of a man beneath a silly green cloak approaches you, unprompted.\n";
std::cout << "\"RIDDLES AND CHALLENGES YOU MUST FACE, IF YOU ARE TO LEAVE THIS PLACE.\",\n";
std::cout << "he says, in an obnoxiously high pitched voice.\n\n";
const int MaxDifficulty = 5;
int LevelDifficulty = 1;
while(LevelDifficulty <= MaxDifficulty) {
int LevelOutcome = PlayGame(LevelDifficulty);
std::cin.clear(); // clears any errors
std::cin.ignore(); // discards the buffer
// use outcome code to determine win or loss
if (LevelOutcome == 0) {
++LevelDifficulty; // increase level difficulty
if (LevelDifficulty <= MaxDifficulty) {
std::cout << "You are correct, this time...\nBut now you must guess another!\n\n";
}
} else if (LevelOutcome == 1) {
std::cout << "Oh, well, yes--but those weren't the numbers I was thinking of.\nTry again!\n\n";
} else {
std::cout << "You are incorrect! Now you must die!\nActually just try again.\n\n";
}
}
std::cout << "\"That's all the numbers I can think of,\" says the filthy man.\n";
std::cout << "\"Carry on with your day.\"\n\n\n";
return 0; // return success
}