#include #include #include #include //new one here, this is so we can use the random function using namespace std; //just do it //GLOBAL VARIABLES this is where we define all the global variables #define EASY 15 #define MEDIUM 30 #define HARD 50 //these are our difficulty levels int life; //the integer to hold the player's life int answer; //the integer to hold the randomly created answer int guess; //the integer to hold the player's guess void TakeInput(); //Gets the user's guess and passes it on to CheckResults void CheckResults(); //our function to check the results and decide what to do void Quit(); //our quit function int main() { life = 5; //this initialises life to 5, so that when the player restarts his life will always be 5 int difficulty; //to hold the difficulty level cout << "****************** JackPot ***********************" << endl << "Welcome to JackPot... choose your difficulty level:" << endl << endl << "1: Easy: Maximum number is " << EASY << endl << "2: Medium: Maximum number is " << MEDIUM << endl << "3: Hard: Maximum number is " << HARD << endl; //asks the user to choose difficulty level cin >> difficulty; //gets input //converts input into a usable number for our random number function switch(difficulty) { case '1' : difficulty = EASY; break; case '2' : difficulty = MEDIUM; break; case '3' : difficulty = HARD; break; default: difficulty = EASY; break; } srand( (unsigned) time ( NULL ) ); //our random function. the unsigned bit means it can't be negative. It's answer = rand() % difficulty; //based on the clock, hence the time.h library. TakeInput(); //Go to TakeInput(); function return 0; } void TakeInput() { if (life <= 0) //if life is equal to or less than 0. { cout << "You lose!" << endl; Quit(); //go to the quit function } cout << "life: " << life << endl << "What is your guess? "; //asks for user's input cin >> guess; //gets user input CheckResults(); //go to CheckResults() function return; } void CheckResults() { //check the results and do your stuff! if (guess == answer) { cout << "Correct - You win!" << endl; Quit(); //if correct, say so and go to Quit() } if (guess < answer) { cout << "Too low! Try again... " << endl; life = life - 1; TakeInput(); //if too low, say so, lose 1 life and go to TakeInput() } if(guess > answer) { cout << "Too high! Try again... " << endl; life = life - 1; TakeInput(); //if too high, say so, lose 1 life and go to TakeInput() } else { cout << "Invalid input." << endl; system("PAUSE"); return; } //if user puts something weird in, say so and quit } void Quit() { system("PAUSE"); //safety net return; }