#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
void Start();
void GetResults();
int i, j, life, maxrand;
char c;
void Start() {
i = 0;
j = 0;
life = 0;
maxrand = 6;
cout << "***选择难度***\n"; // the user has to select a difficutly level
cout << "难度1 : 简单 (0-30)\n";
cout << "难度2 : 一般 (0-50)\n";
cout << "难度3 : 困难 (0-100)\n";
cout << "请选择难度\n";
c = 30;
cin >> c; // read the user's choice
cout << "\n";
switch (c) {
case '1':
maxrand = 30; // the random number will be between 0 and maxrand
break;
case '2':
maxrand = 50;
break;
case '3':
maxrand = 100;
break;
default:
exit(0);
break;
}
life = 5; // 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 << "/*你输了*\ \n\n";
cout<<"正确答案是"<<j<<endl<<endl;
cout<<"//*还玩吗*\\"<<endl;
cout<<"1玩 2不玩"<<endl<<endl;
int yrtu;
cin>>yrtu;
if(yrtu==1) Start();
else{
cout<<"***/****再见****\***";
return ;
}
}
cout << "***请选择一个数字***: \n";
cin >> i;
if((i>maxrand) || (i<0)) { // if the user number isn't correct, restart
cout << "///你选择的数字超出了范围\\\ \n" << maxrand;
GetResults();
}
if(i == j) {
cout << "+++你赢了+++\n\n"; // the user found the secret number
Start();
} else if(i>j) {
cout << "*太大了*\n";
life = life - 1;
cout << "***你还有: " << life << "次尝试机会***\n\n";
GetResults();
} else if(i<j) {
cout << "*太小了*\n";
life = life - 1;
cout << "***你还有: " << life << "次尝试机会***\n\n";
GetResults();
}
}
int main() {
cout << "***猜数字小游戏***\n";
Start();
return 0;
}