引用 | 编辑
wps
2011-11-15 21:35 |
楼主
▼ |
||
x0
设计一猜数字游戏,让使用者有三个等级可选择:初级—猜0到99中任一数字,给使用者提示猜的太大或太小,直到猜到为止。 中级—猜0到99中任一数字,给使用者提示猜的太大或太小,最多猜五次。 高 .. 访客只能看到部份内容,免费 加入会员 x0
|
引用 | 编辑
tw2twtw
2011-11-24 23:00 |
1楼
▲ ▼ |
在Dev-C++找到现成的...(修一下就可用了...zz)
复制程式 #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 << "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"; 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; 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 lose { cout << "You lose !\n\n"; Start(); } cout << "Type a number: \n"; cin >> i; // read user's number 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) { cout << "YOU WIN !\n\n"; // the user found the secret number Start(); } else if (i>j) { cout << "Too BIG\n"; life = life - 1; // -1 to the user's "life" cout << "Number of remaining life: " << life << "\n\n"; GetResults(); } else if (i<j) { cout << "Too SMALL\n"; life = life - 1; cout << "Number of remaining life:\n" << life << "\n\n"; GetResults(); } } int main () { cout << "** Jackpot game **\n"; cout << "The goal of this game is to guess a number. You will be ask to type\n"; cout << "a number (you have 5 guess)\n"; cout << "Jackpot will then tell you if this number is too big of too small compared to the secret number to find\n\n"; Start(); return 0; } 要用我写的也是可以... 复制程式 #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; int main() { int Guess_number = 0; int Guess_total = 0; int difficult = 0; int Guess_Key = 0; char Answer; int Cycle = 0; /* 初级—猜0到99中任一数字,给使用者提示猜的太大或太小,直到猜到为止。 中级—猜0到99中任一数字,给使用者提示猜的太大或太小,最多猜五次。 高级—猜0到999中任一数字,给使用者提示猜的太大或太小,最多猜三次。 */ cout << "猜数字 (由JOT制作)" << endl; cout << "1:初级 (1~99,无限次数)" << endl; cout << "2:中级 (1~99,限猜10次)" << endl; cout << "3:高级 (1~999,限猜10次)" << endl; cout << "请选择难度:"; do { cin >> Answer; Cycle = 0; if( Answer == '1') { difficult = 1; srand((unsigned)time(NULL)); // init Rand() function Guess_Key = rand()%100; cout << "*** 难度一(数字随机1到99,无限次数) ***" << endl; } else if( Answer == '2') { difficult = 2; srand((unsigned)time(NULL)); // init Rand() function Guess_Key = rand()%100; cout << "*** 难度二(数字随机1到99,限猜10次) ***" << endl; } else if( Answer == '3') { difficult = 3; srand((unsigned)time(NULL)); // init Rand() function Guess_Key = rand()%1000; cout << "*** 难度三(数字随机1到999,限猜10次) ***" << endl; } else { Cycle = 1; cout << "请输入数字: (1~3)" << endl; } }while(Cycle); do { Guess_total += 1; if(difficult == 2 && Guess_total > 10) { cout << "十次机会已用完." << endl; cout << "正确答案为 " << Guess_Key << endl; break; } if(difficult == 3 && Guess_total > 10) { cout << "十次机会已用完." << endl; cout << "正确答案为 " << Guess_Key << endl; break; } cout << "请输入你的幸运数字:"; cin >> Guess_number; if(Guess_number > Guess_Key) { cout << " --//此数字小于 " << Guess_number << endl; Cycle = 1; } else if(Guess_number < Guess_Key) { cout << " --//此数字大于 " << Guess_number << endl; Cycle = 1; } else { cout << "*** 恭喜你得到正确答案 ***" << endl; cout << "*** 总计猜了 " << Guess_total << " 次 ***" << endl; Cycle = 0; } }while(Cycle); system("PAUSE"); return 0; } x1 |
引用 | 编辑
iamboss123
2014-01-12 14:34 |
3楼
▲ |
高手果然多啊! 源码拿去了!
x0 |