没错,本羊羔又回来了!
听了HYC的建议,我对猜数游戏进行了完善
1.“人猜”部分中的范围进行了调整,无法输入奇怪的数
2.改善了惩罚部分的细节,将其变成了函数,使它应用更广泛
一大波代码来袭!
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<Windows.h>
#include<stdio.h>
#include<conio.h>
using namespace std;
void cf_time();
int main()
{
system("color 0F");
cout<<"欢迎来到猜数游戏!\n";
cout<<"输入0开始游戏\n输入1查看规则\n";
int t;
cin>>t;
if(t==1)
{
system("cls");
cout<<"先给出一个范围,在范围内取一个数,猜\n";
cout<<"猜到了就赢了;\n直到最后还没猜到,你就乖乖受惩罚吧!!!";
cout<<"\n可以是你猜,也可以是我猜";
cout<<"\n按0键开始游戏吧!\n" ;
cin>>t;
}
if(t==0)
{
system("cls");
cout<<"选择模式:\n";
cout<<"按1人猜\n按0机猜\n";
cin>>t;
if(t==1)
{
system("cls");
int a,b;
cout<<"请输入范围:";
cin>>a>>b;
int cnt2=0;
while(a-b<=2)
{
cnt2++;
if(cnt2>3)
{
cout<<"\n话说事不过三,你该接受惩罚了!\n";
srand((unsigned)time(NULL));
bool tem=rand()%(2-1+1)+1;
if(tem)
{
cf_time();
cout<<"\n\n哼,这就是戏弄我的下场!";
return 0;
}
else
{
cout<<"\n面壁思过一下吧,等你反省好了再来玩!\n";
return 0;
}
}
int te;
srand((unsigned)time(NULL));
te=rand()%(12-1+1)+1;
if(te==1) cout<<"\n认真点,重来\n";
else if(te==2) cout<<"\n想什么呢……\n";
else if(te==3) cout<<"\n我就不信这能玩!\n";
else if(te==4) cout<<"\n别逗我了,好好玩\n";
else if(te==5) cout<<"\n我也是有耐心的!\n";
else if(te==6) cout<<"\n我还不会你自创的新玩法呢\n";
else if(te==7) cout<<"\n不要明目张胆地挑衅我,我是有底线的\n";
else if(te==8) cout<<"\n我的耐心是有限的\n";
else if(te==9) cout<<"\n不会玩就看规则,不要自以为是\n";
else if(te==10) cout<<"\n你怕不是脑袋进水了吧?\n";
else if(te==11) cout<<"\n再这样我就强制关闭游戏了!\n";
else if(te==12) cout<<"\n再这样我就惩罚你了\n";
cin>>a>>b;
}
cnt2=0;
while(a>b)
{
cnt2++;
if(cnt2>3)
{
cout<<"\n有句古话说得好:“人不犯我,我不犯人;人若犯我,我必斩草除根!”\n";
cout<<"\n最开始我以为你不懂,我还给你讲……\n结果你就是在搞事情,对不起了!\n";
srand((unsigned)time(NULL));
bool tem=rand()%(2-1+1)+1;
if(tem)
{
cf_time();
cout<<"\n看你下次还敢不敢!";
return 0;
}
else
{
cout<<"\n面壁思过一下吧,等你反省好了再来玩!";
return 0;
}
}
cout<<"\n输入规则是第一个数小于第二个数哦(a<b)!\n";
cin>>a>>b;
}
srand((unsigned)time(NULL));
int ans=rand()%(b-1-a+1+1)+a+1;
cout<<"\n游戏开始!";
Sleep(1500);
system("cls");
cout<<"开始输入你猜的数吧!(输入-1停止游戏)";
int c;
printf("\n%d到%d选一个数\n",a,b);
int cnt=0;
while(true)
{
if((ans==a+1 && ans==b-1))
{
cout<<"你输了哈哈哈,你运气也太差了!";
printf("\n你用了%d次,输了,该练练了~",cnt);
cout<<"\n\n惩罚时间!\n嗯…";
Sleep(1500);
cf_time();
return 0;
}
cin>>c;
if(c==-1)
{
cout<<"\n溜了溜了";
return 0;
}
cnt++;
if(c==ans && a!=c+1 && b!=c-1)
{
if(cnt<3)
{
cout<<"\n神速啊,你简直是大佬!";
printf("\n你只猜了%d次就猜到了,崇拜!",cnt);
cout<<"\n\n什么时候能有你的实力啊……我先闭关修炼去了,再见!";
return 0;
}
else if(cnt>=3 && cnt<=8)
{
printf("棒!你猜了%d次,666哇!",cnt);
cout<<"\n\n我要休息了,期待与你再次重逢!拜拜~";
return 0;
}
else
{
printf("你终于赢了……");
printf("\n不过也只猜了%d次嘛,哼!",cnt);
cout<<"\n\n我累了,拜拜了您嘞~";
return 0;
}
}
else if(c<a || c>b)
{
cnt--;
printf("当前数不在范围内哦,注意是%d到%d\n",a,b);
}
else
{
if(c>ans)
{
b=c;
printf("\n大了,请在%d至%d范围内选一个数\n",a,b);
}
else if(c<ans)
{
a=c;
printf("\n小了,请在%d到%d中选一个数\n",a,b);
}
}
}
}
if(t==0)
{
system("cls");
cout<<"在你心目中像一个数~不许耍赖!";
cout<<"\n输入规则:1(大了),0(小了),2(猜对了),3(我输了)";
cout<<"先输入你想的数的范围吧:";
int a,b;
cin>>a>>b;
cout<<"\n好的,我开始猜喽~";
int c,ans;
while(c!=2 && c!=3)
{
srand((unsigned)time(NULL));
ans=rand()%(b-1-a+1+1)+a+1;
printf("\n是%d吗?",ans);
cin>>c;
if(c==1)
{
b=ans;
}
else if(c==0)
{
a=ans;
}
}
if(c==2)
{
cout<<"\n嘿嘿,我的技术还是蛮不错的嘛!";
}
if(c==3)
{
cout<<"\n哎,我还得加油啊!";
cout<<"\n那正确答案是什么呢?";
cin>>ans;
printf("\n%d?就差一点了……",ans);
cout<<"\n下次我一定能赢!";
}
}
}
Sleep(3000);
return 0;
}
void cf_time()
{
srand((unsigned)time(NULL));
int cf=rand()%(3-0+1)+0;
if(cf==0)
{
cout<<"就罚你关窗口吧……";
system("start");
system("start");
system("start");
system("start");
system("start");
Sleep(80000);
cout<<"\n还没完!";
system("start");
system("start");
system("start");
system("start");
system("start");
cout<<"\n你不会天真地以为你关完了吧?";
Sleep(80000);
system("start");
system("start");
system("start");
system("start");
system("start");
cout<<"\n谁让你自甘受罚呢!";
Sleep(8000);
system("start");
system("start");
system("start");
system("start");
system("start");
cout<<"\n最后一波来袭~";
Sleep(8000);
system("start");
system("start");
system("start");
system("start");
system("start");
cout<<"\n你现在一定很崩溃,关了20多个窗口呢!";
cout<<"\n略略略,不服来战!";
}
else if(cf==1)
{
cout<<"就让你的电脑睡一觉吧……";
Sleep(1000);
system("rundll32.exe user32.dll,LockWorkStation");
cout<<"\n放心,我还是有点人性的!只锁屏,没关机~";
Sleep(1000);
cout<<"\n虚惊一场!";
}
else if(cf==2)
{
cout<<"我就来秀一波我新学的法术吧……";
Sleep(500);
cout<<"\n定鼠标术!";
while (!_kbhit())
{
SetCursorPos(1, 1);
}
cout<<"\n你刚刚一定很懵逼,再来一次吧!";
while (!_kbhit())
{
SetCursorPos(1, 1);
}
cout<<"\n相信你一定掌握诀窍了,不妨来试试水?";
while (!_kbhit())
{
SetCursorPos(1, 1);
}
cout<<"\n你竟然破除了法术,我还会回来的!";
}
else if(cf==3)
{
cout<<"那我就用魔法打败魔法!";
for (int i = 0; i < 10; i++)
{
system("color A0");
Sleep(50);
system("color B0");
Sleep(50);
system("color C0");
Sleep(50);
system("color D0");
Sleep(50);
system("color E0");
Sleep(50);
system("color F0");
Sleep(50);
}
system("color 0F");
cout<<"\n没闪瞎你的眼睛吧?";
cout<<"\n对不起,我下次还敢!略略略";
}
return;
}
鸣谢:H.Y_C