简介
这款原创的猜数字小游戏可以帮你打消无聊的时间哦~你可以猜一位数、两位数、三位数或四位数,看看你能不能快速的猜中!难度有三种:简单、中等和困难。如果你有足够的信心,可以挑战困难等级!同时这款小游戏有“我的主页”板块,可以看到自己赢了几局,输了几局,总共几局与第几次登录等等。如果你获胜率大于等于30%,并且你玩了超过或等于10局,就可以去看看自己的等级!你,到底是猜数新秀,还是进阶狂者?是初露锋芒,还是无限脑王?这个游戏还有存档功能,不怕数据丢失。还在等什么,快点来试试吧!
代码
#include<bits/stdc++.h>
#include<windows.h>
#include<stdlib.h>
#include<ctime>
using namespace std;
int win,lose,times;
string name,password;
void slowsay(string s,int x)
{
for(auto&i:s)
{
cout<<i;
Sleep(x);
}
cout<<endl;
return;
}
bool sign_in()
{
string _name,_pass;
string cum,num;
freopen("C:\\guess.txt","r",stdin);
cin>>_name>>_pass;
freopen("CON","r",stdin);
cin.clear();
cout.clear();
cout<<endl;
slowsay("请输入您的用户名:",50);
cin>>num;
slowsay("请输入您的密码:",50);
cin>>cum;
if(num==_name&&cum==_pass)
{
cout<<endl;
slowsay("登入成功!",50);
freopen("C:\\guess.txt","r",stdin);
cin>>name>>password;
cin>>win>>lose>>times;
times++;
freopen("CON","r",stdin);
cin.clear();
cout.clear();
return true;
}
cout<<endl;
slowsay("用户名或密码错误!",50);
cout<<endl;
system("pause");
return false;
}
void sign_out()
{
freopen("C:\\guess.txt","w",stdout);
cout<<name<<" "<<password<<endl;
cout<<win<<" "<<lose<<" "<<times;
freopen("CON","w",stdout);
cin.clear();
cout.clear();
return;
}
bool register_new()
{
cout<<endl;
slowsay("请输入您想创建的用户名:",50);
cin>>name;
slowsay("请输入您的密码:",50);
cin>>password;
freopen("C:\\guess.txt","w",stdout);
cout<<name<<" "<<password<<endl;
cout<<0<<" "<<0<<" "<<0;
freopen("CON","w",stdout);
cin.clear();
cout.clear();
MessageBox(NULL,("检测到您是第一次体验此游戏,在这里给您两条游戏基本规则。"),("游戏提示"),MB_OK);
MessageBox(NULL,("第一,请按照游戏提示输入,不要输入无关内容,否则会导致程序崩溃,后果自负!"),("游戏提示"),MB_OK);
MessageBox(NULL,("第二,请不要擅自修改guess.txt里面的内容,否则会导致程序无法正常运行,后果自负!"),("游戏提示"),MB_OK);
MessageBox(NULL,("当前游戏版本不支持创建多个用户,如果强行创建了一个新的用户,旧的用户里的数据将会丢失!"),("游戏提示"),MB_OK);
MessageBox(NULL,("创建用户成功了!请重启游戏来登录吧!"),("游戏提示"),MB_OK);
exit(0);
return true;
}
bool exitt()
{
if(MessageBox(NULL,("你确定?"),("退出游戏"),MB_ICONINFORMATION|MB_YESNO)==IDYES)
exit(0);
return false;
}
bool sign(int k)
{
if(k==1)return sign_in();
if(k==2)return register_new();
return exitt();
}
void game()
{
int ans,cai=0;
system("cls");
cout<<endl<<"---------------开始游戏---------------"<<endl;
cout<<"1.一位数 2.两位数"<<endl;
cout<<"3.三位数 4.四位数"<<endl;
cout<<"0.退出"<<endl;
choose_play:
int chpl;
cin>>chpl;
switch(chpl)
{
case 0:return;break;
case 1:ans=rand()%9+1;break;
case 2:ans=rand()%99+10;break;
case 3:ans=rand()%999+100;break;
case 4:ans=rand()%9999+1000;break;
default:
cout<<endl;
slowsay("输入错误!请重新输入:",50);
goto choose_play;
break;
}
cout<<endl;
slowsay("请选择游戏难度:",50);
slowsay("1.简单 2.中等 3.困难",50);
choose_difficulty:
int chdi;
cin>>chdi;
if(!(chdi>=1&&chdi<=3))
{
cout<<endl;
slowsay("输入错误!请重新输入:",50);
goto choose_difficulty;
}
int cnt;
switch(chdi)
{
case 1:cnt=50;break;
case 2:cnt=15;break;
case 3:cnt=5;break;
}
while(cnt)
{
cout<<endl;
string node;
int t=cnt;
while(t)
{
char tt=t%10+'0';
t/=10;
node=tt+node;
}
string tip="你还剩"+node+"次机会,请输入你猜的数字:";
slowsay(tip,50);
cin>>cai;
if(cai==ans)
{
cout<<endl;
slowsay("猜中啦!恭喜恭喜!",50);
cout<<endl;
win++;
system("pause");
return;
}
if(cai<ans)
{
slowsay("你猜的数字小了。",50);
}
if(cai>ans)
{
slowsay("你猜的数字大了。",50);
}
cnt--;
}
cout<<endl;
slowsay("次数用完了,你输了!",50);
cout<<endl;
lose++;
system("pause");
return;
}
void mine()
{
system("cls");
cout<<endl<<"---------------我的主页---------------"<<endl<<endl;
cout<<"获胜局数:"<<win<<"局"<<endl;
cout<<"失败局数:"<<lose<<"局"<<endl;
cout<<"总局数:"<<win+lose<<"局"<<endl;
cout<<"这是你第"<<times<<"次登录游戏"<<endl<<endl;
system("pause");
return;
}
void level()
{
system("cls");
cout<<endl<<"---------------我的等级---------------"<<endl<<endl;
int sum=win+lose;
if(sum<10)
{
string oop="还需统计";
for(auto&i:oop)
{
cout<<i;
Sleep(50);
}
cout<<sum;
oop="局才可评价等级。";
for(auto&i:oop)
{
cout<<i;
Sleep(50);
}
cout<<endl;
system("pause");
return;
}
cout<<"你的等级是:";
double ratio=1.0*win/sum*100;
if(ratio<30)
{
slowsay("你的获胜率太低,需要提高获胜率才可评价等级!",50);
cout<<endl;
system("pause");
return;
}
if(ratio>=30&&sum>=10&&sum<=15)
cout<<"LV1.猜数新秀";
else if(ratio>=50&&sum>=16&&sum<=25)
cout<<"LV2.进阶狂者";
else if(ratio>=70&&sum>=26&&sum<=40)
cout<<"LV3.初露锋芒";
else if(ratio>=80&&sum>=41&&sum<=65)
cout<<"LV4.不败强者";
else if(ratio>=90&&sum>=66&&sum<100)
cout<<"LV5.猜数王者";
else if(ratio>=97&&sum>=100)
cout<<"LV99999.无限脑王";
cout<<endl<<endl;
system("pause");
return;
}
int main()
{
srand(time(0));
slowsay("欢迎来到猜数字游戏!",100);
begin:
cout<<endl;
slowsay("请选择您的操作:",50);
slowsay("1.登录 2.注册 3.退出",50);
choose_sign:
int k;
cin>>k;
if(!(k>=1&&k<=3))
{
cout<<endl;
slowsay("输入错误!请重新输入:",50);
goto choose_sign;
}
if(!sign(k))goto begin;
main:
system("cls");
cout<<endl<<"---------------猜数字---------------"<<endl;
cout<<endl<<"0.存档并退出 1.开始游戏"<<endl;
cout<<endl<<"2.我的主页 3.我的等级"<<endl;
choose_main:
int chma;
cin>>chma;
if(chma==0)
{
system("cls");
for(int i=1;i<=5;i++)
{
slowsay("正在存档,请稍后..........",50);
system("cls");
}
MessageBox(NULL,("已经安全存档并退出。"),("退出游戏"),MB_OK);
sign_out();
return 0;
}
if(!(chma>=1&&chma<=3))
{
cout<<endl;
slowsay("输入错误!请重新输入:",50);
goto choose_main;
}
if(chma==1){game();goto main;}
else if(chma==2){mine();goto main;}
else {level();goto main;}
return 0;
}
后记
点个赞吧!