Had a lot of fun writing this program, I like that even though it’s simple it involves pretty much all the basics of programming.
My personal style involves double indenting main() and other functions(), this is so that I have room in the left margin for what I believe is a much more clean commenting practice of offsetting the // back one indent level.
This means that // acts as a visual bullet to guide the eye when surveying code, as well as allowing for up to a size six ////// bullet at the highest level to imply greater significance.
I’d be interested to know if anyone else finds this style appealing.
/*- --- ---
|| A fun, yet very challenging math guessing game.
|| Don't be discouraged by how hard it eventually gets
|| that's where the good times are :D
||
|| -- qaptoR
||
\*- --- --- --- -*/
#include <iostream>
#include <ctime>
using namespace std;
bool fnPlayGame( int );
//// MAIN
int main() {
// create new random sequence based on time of day
srand(time(NULL));
// set game difficulty range
int iDif = 2, iMaxDif = 10;
// loop program
while ( iDif <= iMaxDif ){
bool bWinCheck = fnPlayGame(iDif);
cin.clear();
cin.ignore();
if (bWinCheck){
iDif++;
}
}
cout << "\n\n"
<< " Congratulations! You've been released from your involuntary confinement\n"
<< " because of you're great intellect!\n\n";
return 0;
}
/*- --- --- fnPlayGame
|| Game Logic
||
\*- --- --- --- -*/
bool fnPlayGame ( int iDif ) {
// new game/level prompt
if (iDif == 2){
cout << "\n\n"
<< " You have been kidnapped by an insane mathematician!\n"
<< " You must find the solutions to his puzzles in order to be set free\n\n\n";
} else {
cout << "\n\n"
<< "=====***~~~---__...'''...__---~~~***=====\n"
<< " Your captor has a new puzzle for you!\n\n";
}
// generate random codes by way of increasing difficulty
int iCodeA = (rand() %(3+iDif) ) +iDif, iCodeB = (rand() %(3+iDif) ) +iDif, iCodeC = (rand() %(3+iDif) ) +iDif;
int iCSum = iCodeA + iCodeB + iCodeC;
int iCProd = iCodeA * iCodeB * iCodeC;
// reveal win conditions
cout << " + There are three(3) numbers in the solution\n"
<< " + The sum of the numbers is: " << iCSum << "\n"
<< " + and their product is: " << iCProd << "\n\n"
<< " :|: What is your guess? (terminate with x) :|: ";
int iGuess, iGSum = 0, iGProd = 1;
// gather player input
while ( cin >> iGuess ) {
iGSum += iGuess;
iGProd *= iGuess;
}
// determine success
if ( iGSum == iCSum && iGProd == iCProd ) {
cout << "\n\n"
<< " Fantastic! Your one step closer to freedom\n"
<< " now that you've just beat puzzle #" << iDif -1 << "\n\n";
return true;
} else {
cout << "\n\n"
<< " Too bad... I guess you'll be captive a while longer\n"
<< " while you're stuck on puzzle #" << iDif -1 << "\n\n";
return false;
}
}