希望大家能采用。(*_*)
第一次发布,真的不容易!
啥也不说了,上代码!
#include<bits/stdc++.h>
using namespace std;
int main()
{
char j;
srand(time(0));
cout<<"Game name: guess numbers"<<endl;//备注
cout<<"Producer: Wang Yunhao"<<endl;
cout<<"Production completion time: 14:46:07, July 7, 2022"<<endl;
cout<<"Do not copy without permission!"<<endl;
cout<<"___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ "<<endl;
do
{ int n,x,l,m,k,i,a1=0,b1=0;
char y,a[6],b[6];
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
cout<<"Please choose the difficulty you want:"<<endl;//选择难度
cout<<"If you want a simple mode, enter 'A';"<<endl;
cout<<"If you want normal mode, enter 'B';"<<endl;
cout<<"If you want the difficult mode, enter 'C'"<<endl;
cout<<"If you want to customize the difficulty, enter'D'"<<endl;//选D为自定义
cout<<"If you enter other ones,"<<endl;
cout<<"the game will end directly!"<<endl;
cout<<"Prompt: 'A', 'B', 'C' ,'D' all need to be capitalized!"<<endl;//提示输入要求
cin>>y;
if(y=='A') l=1,m=100,x=rand()%100+1,k=25;//设置最大值,最小值,次数上限
else if(y=='B') l=1,m=1000,x=rand()%1000+1,k=50;
else if(y=='C') l=1,m=10000,x=rand()%10000+1,k=100;
else if(y=='D')
{
cout<<"Please enter the minimum and maximum values!"<<endl;//输入提示
cout<<"The minimum value must be bigger than -1 and less than the maximum value!"<<endl;
cout<<"The maximum value must be less than 10000 and bigger than the minimum value!"<<endl;
cout<<"If the number entered does not meet the requirements or you enter other ones,"<<endl;
cout<<"the game will end directly!"<<endl;
cout<<"Note: enter the minimum value first, and then the maximum value!"<<endl;
scanf("%s%s",a,b);//输入最大,最小值
int p=strlen(a),q=strlen(b),r=p-1,o=q-1,r1=1,o1=1;
while(r>=0)
{
if(a[r]>='0'&&a[r]<='9') a1+=(a[r]-'0')*r1;//将字符型变成整型
else
{
cout<<"Input error, the game is over!"<<endl;//如果不是数字,结束游戏
return 0;
}
r--;r1*=10;
}
while(o>=0)
{
if(b[o]>='0'&&b[o]<='9') b1+=(b[o]-'0')*o1;
else
{
cout<<"Input error, the game is over!"<<endl;
return 0;
}
o--;o1*=10;
}
if(a1<=-1||a1>=b1||b1<=a1||b1>=10000)//判断是否超出范围
{
cout<<"Input error, the game is over!"<<endl;
return 0;
}
l=a1,m=b1;
do
{
x=rand()%m+1;
}while(x<=l||x>=m);//循环刷出合适的数
if(m>=100) k=100;
else k=10;
cout<<"OK!Setup complete!"<<endl;
}
else
{
cout<<"Input error, the game is over!"<<endl;//输出错误,结束游戏
return 0;
}
cout<<"Ready? Start!"<<endl;//开始猜数
for(i=1;i<=k;i++)
{
cout<<"Please enter the word you guessed";//提示猜数
if(i>1) cout<<" again!"<<endl;
else cout<<"!"<<endl;
cout<<"Range from "<<l<<" to "<<m<<":"<<endl;//提示范围
if(i>1) cout<<"Tip: you have guessed "<<i-1<<" times!"<<endl;//提示已猜次数
cin>>n;
if(n>x)//偏小
{
cout<<"You guessed wrong!"<<endl;
cout<<"Smaller then "<<n<<" !"<<endl;
m=n;//改变范围
}
else if(n<x)//偏大
{
cout<<"You guessed wrong!"<<endl;
cout<<"Bigger than "<<n<<" !"<<endl;
l=n;
}
else//猜对了
{
cout<<"Congratulations! You guessed right!"<<endl;
cout<<"You guessed a total of "<<i<<" times!"<<endl;
break;
}
}
if(i>k) cout<<"You guessed too many times! Come on next time!"<<endl;//提示猜太多次
cout<<"The game is over!"<<endl;
cout<<"If you want another round, please enter '1', otherwise enter '2':"<<endl;//询问是否再来一局
cout<<"Prompt:if you enter other ones, the game will end directly!"<<endl;
cin>>j;
if(j=='1') cout<<"OK, let's have another round!"<<endl;
}while(j=='1');
if(j=='2') cout<<"OK, come again next time!"<<endl;//结束游戏
else cout<<"Input error, the game is over!"<<endl;
return 0;
}