栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

2048小游戏求解(C语言期末大作业)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2048小游戏求解(C语言期末大作业)

这个是上学期期末的时候的大作业,纯C语言,中间有很多的函数就是为了好看,瞎放上去的,实际上用不了这么多的代码段。水平有限,多多包容。

#include
#include
#include
#include
#include     //getch  
#include
#define SIZE 4
typedef struct intormation{
	char username[100];//用户名 
	char cryptography[100];//密码 
	char name[100];//用户的真实姓名 
	char sex;//用户的性别 
	long int phone;//用户的号码 
}users;

int high=0;
int score=0; 
void help(int a[][SIZE]);
void begin(int a[][SIZE]);
void savescore(int score){
	FILE*fp;
	fp=fopen("tscore.txt","w");
	if(!fp){
		printf("分数存入失败n");
		return ;
	}
	fprintf(fp,"%d",score);
	fclose(fp);
	
}

int loadscore(){
	FILE*fp;
	int a;
	fp=fopen("tscore.txt","r");//以只读的形式打开文件, 
	if(fp==NULL){
		printf("ERROR,分数读取失败");
		return 0; 
	}
	fscanf(fp,"%d",&a);//从历史记录中读出来最高分
	fclose(fp);
	return a; 
}

void loadgame(int (*a)[SIZE]){
	FILE*fp;
	int i,j;
	fp=fopen("tgame.txt","r");
	if(fp==NULL){
		printf("ERROR!内存读取失败");
		return;
	}
	for(i=0;i按下1返回主列表n");
	do{
	scanf("%d",&n);
	}while(n!=1);
	begin(a);
}

int check(int (*a)[SIZE]){
	int i,j,m=0,t=0;
	//检查是否所有的格子都被填满
	for(i=0;ihighest)
			highest=*(*(a+i)+j);
		}
	}
	return highest;
}

void product(int (*a)[SIZE]){
	int i,j,t,w;
	//判断是否还有为0的点,防止函数持续产生,无法终止 ***
	for(i=0;i0;i--){
			if(*(*(a+i)+j)!=0&&*(*(a+i-1)+j)==*(*(a+i)+j)){
				*(*(a+i)+j)*=2;
				*(*(a+i-1)+j)=0;
			}
		}
	} 
	//再次向下移
	  for(j=0;j0;j--){
	 		if(*(*(a+i)+j)!=0&&*(*(a+i)+j)==*(*(a+i)+j-1)){
	 			*(*(a+i)+j)*=2;
	 			*(*(a+i)+j-1)=0;
			 }
		 }
	 }
	 //再次右移
	 for(i=0;ihigh)
			savescore(score);
			system("CLS");
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),4);
			printf("nnnnnnTry again! I`m sure you will get better scores!n");
			Sleep(500);
			system("CLS");
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),1);
			printf("nnnnnttt本局得分为%d,历史最高分为%dn",score,high);
			Sleep(500);
			system("CLS");
			printf("nnnnnntttt是否要再来一次:n");
			printf("ttttttt按下1表示再来一局,其余键表示结束游戏n");
			score=0;
			fflush(stdin); 
			if(getchar()=='1'){
				goto start;
			}
			else
			exit(0);
		}
		if(o!=0){
		num=SUMscore(a);
		score+=num;
		}
		o=1;//防止第一次的分数被算入 
		product(a);//在表内产生一个随机数2或者4 
		if(score>high)
		savescore(score);
		high=loadscore();
		print(a);//显示当前界面
		//选择键盘输入方式 
		
		if(h=='2'){
		getch();
		switch(getch()){
		case 72:
		case 'w':up(a);break;
		case 80:
		case 's':down(a);break;
		case 75:
		case 'a':left(a);break;
		case 77:
		case 'd':right(a);break;
		case '2':saveGame(a);
				Sleep(500);
				exit(0);
		}
	}
	
	else if(h=='1'){
		switch(getch()){
		case 72:
		case 'w':up(a);break;
		case 80:
		case 's':down(a);break;
		case 75:
		case 'a':left(a);break;
		case 77:
		case 'd':right(a);break;
		case '2':saveGame(a);
				 Sleep(500);
				 exit(0);
	}
	}
		win(a);
	} while(1);
	system("PAUSE");
	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/893199.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号