更新内容:新的难度
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include<windows.h>
using namespace std;
void Start();
void GetResults();
int i, j, maxrand;
int life;
char c;
void color(int a){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);
}
void Start() {
i = 0;
j = 0;
life = 0;
maxrand = 6;
color(7);
cout << "Select difficulty mode:\n"; // the user has to select a difficutly level
cout << "1 : Easy (0-15)\n";
cout << "2 : Medium (0-30)\n";
cout << "3 : Difficult (0-50)\n";
color(4);
cout << "4 : THE MOST DUFFICULT (0-60000)\n";
cout << "5 : MUCH MORE DIFFICULT (0-100000)\n";
color(7);
cout << "or type another key to quit\n";
c = 30;
cin >> c; // read the user's choice
cout << "\n";
switch (c) {
case '1':
maxrand = 15; // the random number will be between 0 and maxrand
break;
case '2':
maxrand = 30;
break;
case '3':
maxrand = 50;
break;
case '4':
maxrand = 60000;
break;
case '5':
maxrand = 100000;
break;
default:
exit(0);
break;
}
life = 57666; // number of lifes of the player
srand((unsigned)time(NULL)); // init Rand() function
j = rand() % maxrand; // j get a random value between 0 and maxrand
GetResults();
}
void GetResults() {
if (life <= 0) { // if player has no more life then he loses
// cout << "You lose !\n\n";
// Start();
life=2000000;
}
cout << "Type a number: \n";
color(2);
cout<<"The answer is:"<<j<<endl;
color(7);
cin >> i;
if((i>maxrand) || (i<0)) { // if the user number isn't correct, restart
cout << "Error: number not between 0 and \n" << maxrand;
GetResults();
}
if(i == j) {
color(6);
cout << "YOU WIN!\n\n"; // the user found the secret number
color(7);
Start();
} else if(i>j) {
color(4);
cout << "Too BIG\n";
life = life - 1;
color(14);
cout << "Lives remaining: " << life << "\n\n";
color(7);
GetResults();
} else if(i<j) {
color(10);
cout << "Too SMALL\n";
life -= 1;
color(14);
cout << "Lives remaining: " << life << "\n\n";
color(7);
GetResults();
}
}
int main() {
//color(2);
cout << "** Jackpot game **\n";
cout << "The goal of this game is to guess a number.\n";
cout << "Jackpot will tell you if the number is too big or too\n";
cout << "small compared to the secret number to find.\n\n";
//color(7);
Start();
return 0;
}