第一个C语言小游戏——猜数字

#include <stdio.h>//标准库
#include <stdlib.h>//内含随机函数
#include <time.h>//将时间设置成随机函数的种子
#include <windows.h>//内含Sleep函数
#include <math.h>//内含sqrt函数

void test() 
{ 
  int i = 0; 
  int j = 0; 
  double a = 0; 
  
  while (i++ < 1000000) 
      while (j++ < 1000000) 
      { 
        a = sqrt(2.0); 
      } 
} 

int main(void){
	int a=0;

	printf("----------------数字游戏---------------\n\n");
	Sleep(1000);
	printf("加载中.");Sleep(500);
	printf(".");Sleep(500);
	printf(".");Sleep(500);
	printf(".");Sleep(500);
	printf(".");Sleep(500);
	printf(".");Sleep(500);
	
d:
	printf("-----------开始游戏[请按1]-----------\n");
	printf("-----------查看帮助[请按2]-----------\n");
	printf("-----------退出游戏[请按0]-----------\n");//显示初始界面以及交互信息

	scanf("%d",&a);//接收指令
		switch(a){
			case 1:goto a;break;
			case 2:goto b;break;
			case 0:return 0;break;
			default:printf("\a请重新输入!\n");goto d;break;
			break;
		}//做出判断及执行操作 
c:
	return 0;//退出
b:
	printf("[操作方法]\n");
	printf("键盘操作,根据提示输入数字后回车即可继续\n");
	printf("[模式简介]\n");
	printf("单人模式:进行单人计分游戏\n");
	printf("人机模式:采取人机对战模式,可选择难度,根据用时长短判断获胜与否\n");
	printf("挑战模式:根据用时长短计分并计入排行榜\n");//显示帮助信息
e:	
	printf("返回[请按2]\n");

	scanf("%d",&a);
		switch(a){
			case 2:goto d;break;
		    default:printf("\a请重新输入!\n");goto e;break;
		}//返回
a:
	printf("单人模式[请按1]人机模式[请按2]挑战模式[请按3]\n");
	scanf("%d",&a);
		switch(a){
			case 1:goto single;break;
			case 2:goto computer;break;
			case 3:goto challenge;break;
			default:printf("\a请重新输入!\n");goto a;break;
		}//选择模式
single://单人模式入口
	
  	
y:
	srand( (unsigned)time( NULL ) );//将当前时间设置成随机函数的种子,所以每次产生的数都不一样
	int num;//随机数
	num=rand();

	clock_t start, finish; //时间变量的声明与之前将时间设为随机种子冲突,所以必须放在其后
    double  duration = 0.0; 
  
    start = clock(); 
	printf("猜猜我是几^-^(0~32767)\n");
	int x;
x:
	scanf("%d",&x);

	if(x>num){
		printf("猜大了哦^-^\n");
		goto x;
	}else if(x<num){
		printf("猜小了哦^-^\n");
		goto x;
	}else if(x==num){
		printf("恭喜你,猜对咯!^-^\n");
	}
	test(); 
  	finish = clock(); 
  	duration = (double)(finish - start);//输出单位ms
 	duration = (double)(finish - start) / CLOCKS_PER_SEC; //输出单位为秒,精确到毫秒级
  	printf("用时%f 秒\n得分:%d", duration,2500/(int)duration);
z:
	printf("再来一局[请按1]返回菜单[请按2]退出游戏[请按3]\n");
	scanf("%d",&a);
	switch(a){
		case 1:goto y;break;
		case 2:goto d;break;
		case 3:return 0;
		default:printf("\a请重新输入!\n");goto z;break;
	}
computer://人机模式入口
    
	printf("请选择难度\n");
j:
	printf("简单[请按1]普通[请按2]困难[请按3]\n");
	int n=0,i=1;
	scanf("%d",&a);
	switch(a){
		case 1:n=1.1;break;
		case 2:n=1.5;break;
		case 3:n=1.9;break;
		default:printf("\a请重新输入!\n");goto j;
	}
	srand( (unsigned)time( NULL ) );//将当前时间设置成随机函数的种子,所以每次产生的数都不一样
	int nu;//随机数
	nu=rand();
	

	clock_t star, finis; //时间变量的声明与之前将时间设为随机种子冲突,所以必须放在其后
    double  duratio = 0.0; 
  
    star = clock(); 
	printf("猜猜我是几^-^(0~32767)\n");
q:
	scanf("%d",&x);

	if(x>nu){
		printf("猜大了哦^-^\n");
		goto q;
	}else if(x<nu){
		printf("猜小了哦^-^\n");
		goto q;
	}else if(x==nu){
		printf("恭喜你,猜对咯!^-^\n");
	}
	test(); 
  	finis = clock(); 
  	duratio = (double)(finis - star);//输出单位ms
 	duratio = (double)(finis - star) / CLOCKS_PER_SEC; //输出单位为秒,精确到毫秒级
 	printf("您用时%f 秒\n得分:%d\n", duratio,2500/(int)duratio);
 //电脑 
 
	int peo=duratio;
	double cum_=nu*n/1000;
	int cum=2500/(int)cum_;
	printf("电脑用时%f秒,得分:%d\n",cum_,cum);
	if(peo>cum){
		printf("您赢了!\n");
	}else if(peo<cum){
		printf("电脑获胜!\n");
	}else{
		printf("平局!\n");
	}
	
w:
	printf("再来一局[请按1]返回菜单[请按2]退出游戏[请按3]\n");
	scanf("%d",&a);
	switch(a){
		case 1:goto j;break;
		case 2:goto d;break;
		case 3:return 0;
		default:printf("\a请重新输入!\n");goto w;break;
	}
challenge://挑战模式入口 
k:
srand( (unsigned)time( NULL ) );//将当前时间设置成随机函数的种子,所以每次产生的数都不一样
	int nume;//随机数
	nume=rand();

	clock_t starte, finishe; //时间变量的声明与之前将时间设为随机种子冲突,所以必须放在其后
    double  duratione = 0.0; 
  
    starte = clock(); 
	printf("猜猜我是几^-^(0~32767)\n");
t:
	scanf("%d",&x);

	if(x>nume){
		printf("猜大了哦^-^\n");
		goto t;
	}else if(x<nume){
		printf("猜小了哦^-^\n");
		goto t;
	}else if(x==nume){
		printf("恭喜你,猜对咯!^-^\n");
	}
	test(); 
  	finishe = clock(); 
  	duratione = (double)(finishe - starte);//输出单位ms
 	duratione = (double)(finishe - starte) / CLOCKS_PER_SEC; //输出单位为秒,精确到毫秒级
  	printf("用时%f 秒\n得分:%d\n", duratione,2500/(int)duratione);
  	int ww=0;
	FILE *fp;
	fp = fopen("socre.txt","a");
	fscanf(fp,"%d",&ww);
	if(2500/(int)duratione>ww){
		printf("新纪录!\n");
		fprintf(fp,"%d",2500/(int)duratione);
        
	}else{
		printf("历史最高:%d",ww);
	}
	fclose(fp);
	
v:
	printf("再来一局[请按1]返回菜单[请按2]退出游戏[请按3]\n");
	scanf("%d",&a);
	switch(a){
		case 1:goto k;break;
		case 2:goto d;break;
		case 3:return 0;
		default:printf("\a请重新输入!\n");goto v;break;
	}
	
return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37450955/article/details/86556754