C语言 推箱子小游戏 可重玩 可选关 保存记录

转载请保留此行,来自Vic___


这是DLNU大一的题目,估计我发上来后,这个课题的难度大大降低。


可以随意增加地图

根据20*20的画就行

0:可移动 1:墙 2:目的地 3:箱子 5:玩家

记得也要增加“存在关数”

C语言 推箱子小游戏 可重玩 可选关 保存记录_第1张图片

//-----------------------------------
//
//		version 1.0
//			From Vic___
//
//-----------------------------------
#include 
#include 
#include 
#include 
void DisplayMap();
const int completelevel=5;//存在关数
int dataMap[30][20][20]=
{
	
	{
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,1,1,2,1,0,0,0,0,0,0,0,0,0,            //0:可移动 1:墙 2:目的地 3:箱子 5:玩家 
		0,0,0,0,0,0,0,1,1,3,1,1,1,0,0,0,0,0,0,0,
		0,0,0,0,0,1,1,2,3,5,3,2,1,0,0,0,0,0,0,0,
		0,0,0,0,0,1,2,3,0,0,1,1,1,0,0,0,0,0,0,0,
		0,0,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,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0,0,
		0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	}
};
int Map[20][20];
int level=0;
int x,y;
int bestdata[30]={0};
int ReadKey()
{
	int a;
	a=getch();
	if(a==114)
		return 0;
	if(a==122)
		return 10;
	if(a==120)
		return 11;
    if(a==224)
    {
		a=getch();
		switch(a)
		{
		case 72:return 1;
		case 75:return 2;
		case 80:return 3;
		case 77:return 4;
		}
	}
    else
		return 0;
}
void FindXY()
{
	int i,j;
	for(i=0;i<20;i++)
		for(j=0;j<20;j++)
			if(Map[i][j]==5 ||Map[i][j]==7)
			{
				y=i;x=j;break;
			}
}
void GameStart()
{
    int i,j;
	for(i=0;i<20;i++)
		for(j=0;j<20;j++)
			Map[i][j]=dataMap[level][i][j];
		FindXY();
		DisplayMap();
		printf("第 %d 关\t\t最佳纪录 %d 步\n\nR:重玩\tZ:上关\tX:下关\n",level+1,bestdata[level]);
}
void HandleKey(int key)
{
	if(key==1)//上
	{
		int y1,y2;
		y1=y-1;
		y2=y-2;
		if(y1<0)
			return ;
		else if(Map[y1][x]==3||Map[y1][x]==4)
		{
			if(Map[y2][x]==3 ||Map[y2][x]==4||Map[y2][x]==1)
				return ;
			else  if(Map[y2][x]==0 ||Map[y2][x]==2)
			{
				// tuixiangzi
				Map[y][x]-=5;
				if(Map[y1][x]==3)
					Map[y1][x]=5;
				else if(Map[y1][x]==4)
					Map[y1][x]=7;
				if(Map[y2][x]==0)
					Map[y2][x]=3;
				else if(Map[y2][x]==2)
					Map[y2][x]=4;
				FindXY();
				
			}
			
		}
		else if(Map[y1][x]==0 ||Map[y1][x]==2)
		{
            
			Map[y][x]-=5;Map[y1][x]+=5;
			FindXY();
			
		}
		
	}
	else if(key==2) //左
	{
		int x1,x2;
		x1=x-1;
		x2=x-2;
		if(x1<0)
			return ;
		else if(Map[y][x1]==1)
			return ;
		else if(Map[y][x1]==3||Map[y][x1]==4)
		{
			if(Map[y][x2]==3 ||Map[y][x2]==4||Map[y][x2]==1)
				return ;
			else  if(Map[y][x2]==0 ||Map[y][x2]==2)
			{
				// tuixiangzi
				Map[y][x]-=5;
				if(Map[y][x1]==3)
					Map[y][x1]=5;
				else if(Map[y][x1]==4)
					Map[y][x1]=7;
				if(Map[y][x2]==0)
					Map[y][x2]=3;
				else if(Map[y][x2]==2)
					Map[y][x2]=4;
				FindXY();
				
			}
			
		}
		else if(Map[y][x1]==0 ||Map[y][x1]==2)
		{
            
			Map[y][x]-=5;Map[y][x1]+=5;
			FindXY();
			
		}
		
	}
	else if(key==3)//下
	{
		int y1,y2;
		y1=y+1;
		y2=y+2;
		if(y1>=20)
			return ;
		else if(Map[y1][x]==3||Map[y1][x]==4)
		{
			if(Map[y2][x]==3 ||Map[y2][x]==4||Map[y2][x]==1)
				return ;
			else  if(Map[y2][x]==0 ||Map[y2][x]==2)
			{
				// tuixiangzi
				Map[y][x]-=5;
				if(Map[y1][x]==3)
					Map[y1][x]=5;
				else if(Map[y1][x]==4)
					Map[y1][x]=7;
				if(Map[y2][x]==0)
					Map[y2][x]=3;
				else if(Map[y2][x]==2)
					Map[y2][x]=4;
				FindXY();
				
			}
			
		}
		else if(Map[y1][x]==0 ||Map[y1][x]==2)
		{
            
			Map[y][x]-=5;Map[y1][x]+=5;
			FindXY();
			
		}
		
	}
	else if(key==4)//右
	{
		int x1,x2;
		x1=x+1;
		x2=x+2;
		if(x1>=20)
			return ;
		else if(Map[y][x1]==1)
			return ;
		else if(Map[y][x1]==3||Map[y][x1]==4)
		{
			if(Map[y][x2]==3 ||Map[y][x2]==4||Map[y][x2]==1)
				return ;
			else  if(Map[y][x2]==0 ||Map[y][x2]==2)
			{
				Map[y][x]-=5;
				if(Map[y][x1]==3)
					Map[y][x1]=5;
				else if(Map[y][x1]==4)
					Map[y][x1]=7;
				if(Map[y][x2]==0)
					Map[y][x2]=3;
				else if(Map[y][x2]==2)
					Map[y][x2]=4;
				FindXY();// tuixiangzi
			}
		}
		else if(Map[y][x1]==0 ||Map[y][x1]==2)
		{
            
			Map[y][x]-=5;Map[y][x1]+=5;
			FindXY();
			
		}
	}
}
void DisplayMap()
{
	int i,j;
	for(i=0;i<20;i++)
	{
		for(j=0;j<20;j++)
		{
			switch(Map[i][j])
			{
			case 0:printf("  ");break;
			case 1:printf("▓");break;
			case 2:printf("※");break;
            case 3:printf("□");break;
            case 4:printf("■");break;
            case 5:printf("♀");break;
            case 7:printf("♂");break;
			}
		}
		printf("\n");
	}
	
}
int JudgeComplete()
{
	int i,j;
	for(i=0;i<20;i++)
		for(j=0;j<20;j++)
			if(2==Map[i][j]||3==Map[i][j]) return 0;
			return 1;
}

void OpenData()
{
	int i=0;
	ifstream ifile("save.dat",ios::binary);
	if(!ifile)
	{
		printf("不能打开文件\n");
		return;
	}
	while(!ifile.eof())
	{
		ifile.read((char *)&bestdata[i],sizeof(bestdata[i]));
		i++;
	}
	ifile.close();
}

void SaveData()
{
	ofstream ofile("save.dat",ios::binary);
	if(!ofile)
	{
		printf("不能存入文件\n");
		return;
	}
	for(int i=0;i

你可能感兴趣的:(C语言 推箱子小游戏 可重玩 可选关 保存记录)