本篇内容主要讲解“怎么用c/c++实现猜数字游戏”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用c/c++实现猜数字游戏”吧!
猜数字游戏
以下代码在Dev,codeblocks,VC上都能运行
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h> //下面Sleep()函数的头文件
#include<mmsystem.h>
void menu()
{
printf(" *****************************\n");
printf(" *****************************\n");
printf(" ************1.play***********\n");
printf(" ************0.exit***********\n");
printf(" **********2.chakan***********\n");
printf(" *********3.qingkong**********\n");
printf(" *****************************\n");
printf("请选择: \n");
}
void game()
{
system("cls");
system ( "color 33" ); //设置颜色
FILE *fp;
if((fp=fopen("D:\\caishuziyou.txt","a+"))==NULL)
{
printf("open file error\n");
return ;
}
int rand_num=rand()%100;
int tmp=0,t=0;
while(1)
{
int flag=0;
printf("请输入你要猜的数字:");
scanf("%d",&tmp);
if(tmp>rand_num)
{
printf("猜大了!\n");
flag=0;
}
if(tmp<rand_num)
{
printf("猜小了!\n");
flag=0;
}
if(tmp==rand_num)
{
printf("正确!恭喜!\n");
printf("共用了%d次\n",t+1);
FILE *fp;
fp=fopen("D:\\caishuziyou.txt","w");
if(fp==NULL)
{
printf("create file failed\n");
return ;
}
fprintf(fp,"%d",t+1);
fclose(fp);
break;
}
if(flag==0) t++;
printf(" 你再猜:\n");
}
printf("\n按任意键返回目录");
getch();
}
void menu2()
{
system("cls");
system ( "color B6" ); //设置颜色
printf("即将登陆系统!!!\n");
getch();
for(int t=3;t>=1;t--)
{
system("cls");
printf("%d",t);
Sleep(700);
system("cls");
}
}
void chakan()
{
system("cls");
system ( "color E0" ); //设置颜色
int t=0;
FILE*fp;
if((fp=fopen("D:\\caishuziyou.txt","r"))==NULL)
{
printf("can not to open the file!\n");
exit(0);
}
while(fscanf(fp,"%d",&t)!=EOF)//若不到文件结尾则继续
{
printf("此玩家共进行了%d次\n",t);
}
printf("\n按任意键返回目录");
getch();
}
void qk()//清空
{
FILE *fp;
system("cls"); //清屏函数,不管下面是否保存数据,每次输入新数据是屏幕上只能有此次的数据
system ( "color C0" ); //设置颜色
if((fp=fopen("D:\\caishuziyou.txt","w"))==NULL)
{
printf("open file error\n");
return;
}
fclose(fp);
printf("\n按任意键返回!");
getch();
}
int main()
{
int input=0;
srand((unsigned)time(NULL));
do{
//while(1)
//{
system("cls");
menu();
// printf("请选择:");
scanf("%d",&input);
switch(input)
{
case 1:
menu2();
game();
break;
case 2:
chakan();
break;
case 3:
qk();
break;
case 0:
exit(0);
break;
default:printf("输入错误!请重新输入!");
}
//}
}while(input);
return 0;
}
到此,相信大家对“怎么用c/c++实现猜数字游戏”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。