利用循环结构,我们可以让玩家反复输入数字并比较大小,由此可以实现基本的猜数字功能。
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int a,b=10;
while (cin >> a)
{
if (a > b)
{
cout << "偏大" << endl;
}
else if (a < b)
{
cout << "偏小" << endl;
}
else
{
cout << "Yes!" << endl;
}
}
return 0;
}
然后我们可以给要猜的数字一点随机性,利用Windows的时间来生成一个随机数字。
srand((unsigned int )time (NULL));
int a=rand()%100+1;
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int num2;
srand((unsigned int)time(NULL));
int num1 = rand() % 100 + 1;
cout << "请猜出随机生成的数字(0~100)" << endl;
start = clock();
while (1)
{
cin >> num2;
if (num2 > num1)
{
cout << "很遗憾,数字偏大" << endl;
}
else if (num2 < num1)
{
cout << "很遗憾,数字偏小" << endl;
}
else if (num2 = num1)
{
cout << "恭喜,游戏胜利" << endl;
break;
}
}
return 0;
}
这样一个可以玩起来的猜数字游戏就完成了。
最后,可以通过Windows的记录时间的功能来调整难度(对猜数字的时间有所限制)
#include<iostream>
#include<cstdio>
#include<time.h>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>//windows编程头文件
using namespace std;
int main()
{
time_t start=0, end=0;
int num2;
srand((unsigned int)time(NULL));
int num1 = rand() % 100 + 1;
char level;
cout << "请猜出随机生成的数字(0~100)" << endl << endl << endl << endl;
cout << "请选择难度" << endl;
cout << "A.困难(10s) B.普通(15s) C.简单(20s) D.?????" << endl;
cin >> level;
start = clock();
if (level == 'A')
{
cout << "请开始猜数字" << endl;
while (1)
{
cin >> num2;
if (num2 > num1)
{
cout << "很遗憾,数字偏大" << endl;
}
else if (num2 < num1)
{
cout << "很遗憾,数字偏小" << endl;
}
else if (num2 = num1)
{
end = clock();
if ((end - start) / 1000 > 10)
{
cout << "超时,请再试一次" << endl<<"用时"<< (end - start) / 1000 << "s" << endl;
break;
}
else
{
cout << "恭喜,游戏胜利" << endl;
cout << "您共用时" << (end - start) / 1000 << "s" << endl;
break;
}
}
}
}
else if (level == 'B')
{
cout << "请开始猜数字" << endl;
while (1)
{
cin >> num2;
if (num2 > num1)
{
cout << "很遗憾,数字偏大" << endl;
}
else if (num2 < num1)
{
cout << "很遗憾,数字偏小" << endl;
}
else if (num2 = num1)
{
end = clock();
if ((end - start) / 1000 > 15)
{
cout << "超时,请再试一次" << endl << "用时" << (end - start) / 1000 << "s" << endl;
break;
}
else
{
cout << "恭喜,游戏胜利" << endl;
cout << "您共用时" << (end - start) / 1000 << "s" << endl;
break;
}
}
}
}
else if (level == 'C')
{
cout << "请开始猜数字" << endl;
while (1)
{
cin >> num2;
if (num2 > num1)
{
cout << "很遗憾,数字偏大" << endl;
}
else if (num2 < num1)
{
cout << "很遗憾,数字偏小" << endl;
}
else if (num2 = num1)
{
end = clock();
if ((end - start) / 1000 > 20)
{
cout << "超时,请再试一次" << endl << "用时" << (end - start) / 1000 << "s" << endl;
break;
}
else
{
cout << "恭喜,游戏胜利" << endl;
cout << "您共用时" << (end - start) / 1000 << "s" << endl;
break;
}
}
}
}
else if (level == 'D')
{
cout << "请开始猜数字" << endl;
srand((unsigned int)time(NULL));
int num3 = rand() % 100+1;
if (num3 <= 10)
{
cout << "You only have " << num3 << "s " << "to finish it !!!!!" << endl;
}
else if(num3<=50)
{
cout << "You will have" << num3 << " s " << "to finish it." << endl;
}
else
{
cout << "You will have enough time to finish." << endl;
}
while (1)
{
cin >> num2;
if (num2 > num1)
{
cout << "很遗憾,数字偏大" << endl;
}
else if (num2 < num1)
{
cout << "很遗憾,数字偏小" << endl;
}
else if (num2 = num1)
{
end = clock();
if ((end - start) / 1000 > num3)
{
cout << "超时,请再试一次" << endl << "用时" << (end - start) / 1000 << "s" << endl;
break;
}
else
{
cout << "恭喜,游戏胜利" << endl;
cout << "您共用时" << (end - start) / 1000 << "s" << endl;
break;
}
}
}
}
else
{
cout << "What are you doing?" << endl << endl;
system("Pause");
cout << "It looks like you are bored with this game. " << endl;
system("Pause");
cout << "Let's try some new things." << endl;
}
return 0;
}
这样不同难度就区分出来了
甚至还可以利用随机的那段代码来实现随机时间限制。