推箱子小游戏

内容描述

描述:用户进入页面后可以进行选关,每关的关卡难度依次递增,当你选择一关并通过后,会自动进入下一关,直到游戏结束。游戏过程中会有系统提示。
实现方式:二维数组

功能

在这里插入图片描述
1.菜单功能。玩家通过看目录按对应的键可以进入对应的管卡。
2.难度。关卡难度依次递增。
3.顺序功能。当玩家完成了一关后可顺利进入下一关,继续挑战难度高的关卡。

设计目的

训练学生掌握c的数据结构和综合变成能力和游戏开发能力等应用能力。

初步设想

推箱子是比较经典的益智小游戏,目的是在训练人的逻辑思考能力。 在一个狭小的空间中,要求把木箱从起始位置处推放到指定位置。在空间中地方有限,稍不小心就会出现无法移动或道路被堵住的情况,而且箱子不能推不能拉。所以需要玩家利用正确的思维通关。

总体设计

(1)初始化模块。该模块包括屏幕初始化和游戏第一关到最后一关的初始化。游戏每一关的初始化时显实现每一关的关键。
(2)画图模块。该模块主要是被其他模块调用,用于画墙、在空地画箱子、在目的地画箱子、画小人和画目的地。
(3)移动箱子模块。该模块用于移动箱子,包括目的地之间、空地之间和目的地与空地之间箱子的移动。
(4)移动小人模块。该模块是几个功能函数的集合。
在这里插入图片描述

缺点

代码的冗余度很高,需要优化

实例一

#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<string.h>
#include<windows.h>
using namespace std;

int total=0;
char name[10],bel[20],names[10],bels[20];

void SetColor(unsigned short ForeColor,unsigned short BackGroundColor)
{
	HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hCon,(ForeColor%16)|(BackGroundColor%16*16));
}
/*
	1.先想先了解
	2. 
		2.1 地图 --贯穿整个数组  --全局的地图数组
		2.2 游戏,项目的划分:就是模块的划分
			原则:一个功能使用一个模块
			      功能与功能尽量不要有交叉的现象
				  功能划分,互不影响,避免
	    2.3 函数的设计
				2.3.1 函数的返回值(函数唯一的遗留物)问题
					举例:判断正负的函数,想清楚
					最终结果:是赢还是输(游戏结束) 
					通过返回值表示下一步操作
					一般情况下只需要知道结果的函数才需要返回值 
				2.3.2 函数的参数
				形参就是实参的拷贝版,复制品 
					1.要操作数据,但是不想改变数据 
				 	2.说明函数要具体的操作对象 
	3.经验和代码量的堆积,以及独立思考
		必须有自己的编程思想↑	
*/
//二维数组即多个一维数组,三维数组即多个二维数组 

int map[5][12][13]=
{
	//0 空的
	//1▋ 墙
	//3☆ 4□ 目的地和箱子
	//5※人
	//7⊙ 目的地(3)和箱子(4)在一起
	//8※ 人(5)和目的地(3)在一起 
	//数学艺术  上面数字,数字的加减与代表物都是精心策划的 
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
	1, 3, 1, 0, 1, 1, 3, 1, 1, 1, 1, 1, 1,
	1, 4, 0, 0, 4, 0, 3, 1, 1, 1, 1, 1, 1,
	1, 0, 1, 0, 1, 1, 4, 1, 1, 1, 1, 1, 1,
	1, 0, 0, 5, 0, 0, 0, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
	1, 3, 1, 0, 1, 1, 3, 1, 1, 1, 1, 1, 1,
	1, 3, 4, 5, 4, 0, 3, 1, 1, 1, 1, 1, 1,
	1, 4, 1, 0, 1, 1, 4, 1, 1, 1, 1, 1, 1,
	1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 3, 0, 0, 0, 3, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 4, 1, 0, 4, 1, 1, 1, 1, 1, 1, 1,
	1, 0, 0, 0, 5, 0, 3, 1, 1, 1, 1, 1, 1,
	1, 0, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1,
	1, 0, 4, 0, 0, 0, 3, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 0, 0, 0, 0, 1, 0, 1, 0, 4, 0, 3, 1,
	1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1,	
	1, 0, 0, 0, 4, 0, 0, 1, 0, 1, 1, 0, 1,
	1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1,
	1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, 0, 1,
	1, 1, 0, 1, 1, 1, 1, 4, 1, 0, 1, 3, 1,
	1, 1, 0, 1, 1, 1, 0, 0, 0, 5, 1, 0, 1,
	1, 0, 0, 0, 1, 1, 0, 1, 0, 4, 3, 4, 1,
	1, 3, 0, 0, 0, 1, 3, 4, 0, 0, 0, 0, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 0, 0, 0, 0, 1, 3, 1, 1, 1, 1, 1, 1,
	1, 0, 4, 0, 0, 4, 0, 1, 1, 1, 1, 1, 1,
	1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1,
	1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1,
	1, 3, 1, 1, 5, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} 
};

int cas=0;   //为0表示第一关 

//你还要有一个量,来记录每一关的箱子数,或者说箱子和目的地在一起的总数
int boxSum[5]={3,4,4,6,2}; 
//地图绘制
void drawMap()
{	
	for(int i=0;i<12;i++)
	{
		for(int j=0;j<13;j++)
		{
			switch(map[cas][i][j])
			{
				case 0:
					printf("  ");
					break;
				case 1:
					printf("▋");
					break;
				case 3:
					printf("☆");
					break;
				case 4:
					printf("□");
					break;
				case 5:
				case 8:
					printf("※");
					break;
				case 7:
					printf("⊙");
					break;	
			}
		}
		printf("\n");
	}
}

//按键处理
void keyDown()
{
	//分析按键过程
	//定位人的坐标
	//人有两种情况:1.人,2.人和目的地在一起 
	int i,j;
	for(i=0;i<12;i++)
	{
		for(j=0;j<13;j++)
		{
			if(map[cas][i][j]==5||map[cas][i][j]==8)
			{
				break;
			}
		}
		if(map[cas][i][j]==5||map[cas][i][j]==8)
			{
				break;
			}
	 } 
	 char ch=_getch();   //看不见的字符输入 
	 switch(ch)
	 {
	 	//小键盘也要注意  键码值:上 72 下 80 左 75 右 77 
	 	case 'w':
	 	case 'W':
	 	case 72:
	 		//3+5=8,8表示目的地和人在一起 
	 		//什么时候能走? 0或者目的地
	 		//注意要判断的是什么,此位置还是要走的位置 
			 if(map[cas][i-1][j]==0||map[cas][i-1][j]==3)
			 {
			 	//新地方(map[i-1][j])人来了
				map[cas][i-1][j]+=5;
				 //老地方(map[i][j])人走了
				map[cas][i][j]-=5;
			 } 
			 //下一个地方是箱子的话(递进关系),判断箱子的下一个地方是不是目的地或空地 
			 //箱子有两种情况  1.箱子 2.箱子和目的地在一起 
			//如果下一个地方是箱子,要进一步判断怎么走 
			else if(map[cas][i-1][j]==4||map[cas][i-1][j]==7)
			{
				//做箱子的下一个地方判断能不能走
				 if(map[cas][i-2][j]==0||map[cas][i-2][j]==3)
				 {
				 	//新的地方箱子来了
					 map[cas][i-2][j]+=4;
					 //箱子的位置:箱子(-4)走了,人(+5)来了
					 map[cas][i-1][j]+=1;  //只显示最终结果; 
					 //原来的地方人走了
					 map[cas][i][j]-=5; 
				 }
			}
		 break;
		
		case 's':
		case 'S':
		case 80:
			//什么时候能走? 0或者目的地
	 		//注意要判断的是什么,是要走的位置 
			if(map[cas][i+1][j]==0||map[cas][i+1][j]==3)
			 {
			 	//新地方(map[i-1][j])人来了
				map[cas][i+1][j]+=5;
				 //老地方(map[i][j])人走了
				map[cas][i][j]-=5;
			 } 
			 //下一个地方是箱子,判断箱子的下一个地方是不是目的地或空地 
			 //箱子有两种情况  1.箱子 2.箱子和目的地在一起 
			//如果下一个地方是箱子,要进一步判断怎么走 
			else if(map[cas][i+1][j]==4||map[cas][i+1][j]==7)
			{
				//做箱子的下一个地方判断能不能走
				 if(map[cas][i+2][j]==0||map[cas][i+2][j]==3)
				 {
				 	//新的地方箱子来了
					 map[cas][i+2][j]+=4;
					 //箱子的位置:箱子(-4)走了,人(+5)来了
					 map[cas][i+1][j]+=1;  //只显示最终结果; 
					 //原来的地方人走了
					 map[cas][i][j]-=5; 
				 }
			}
			break;
			
		case 'a':
		case 'A':
		case 75:
			//什么时候能走? 0或者目的地
	 		//注意要判断的是什么,是要走的位置 
			if(map[cas][i][j-1]==0||map[cas][i][j-1]==3)
			 {
			 	//新地方(map[i-1][j])人来了
				map[cas][i][j-1]+=5;
				 //老地方(map[i][j])人走了
				map[cas][i][j]-=5;
			 } 
			 //下一个地方是箱子,判断箱子的下一个地方是不是目的地或空地 
			 //箱子有两种情况  1.箱子 2.箱子和目的地在一起 
			//如果下一个地方是箱子,要进一步判断怎么走 
			else if(map[cas][i][j-1]==4||map[cas][i][j-1]==7)
			{
				//做箱子的下一个地方判断能不能走
				 if(map[cas][i][j-2]==0||map[cas][i][j-2]==3)
				 {
				 	//新的地方箱子来了
					 map[cas][i][j-2]+=4;
					 //箱子的位置:箱子(-4)走了,人(+5)来了
					 map[cas][i][j-1]+=1;  //只显示最终结果; 
					 //原来的地方人走了
					 map[cas][i][j]-=5; 
				 }
			}
			break;
		
		case 'd':
		case 'D':
		case 77:
			//人什么时候能走? 0或者目的地
	 		//注意要判断的是什么,是要走的位置 
			if(map[cas][i][j+1]==0||map[cas][i][j+1]==3)
			 {
			 	//新地方(map[i-1][j])人来了
				map[cas][i][j+1]+=5;
				 //老地方(map[i][j])人走了
				map[cas][i][j]-=5;
			 } 
			 //下一个地方是箱子,判断箱子的下一个地方是不是目的地或空地 
			 //箱子有两种情况  1.箱子 2.箱子和目的地在一起 
			//如果下一个地方是箱子,要进一步判断怎么走 
			else if(map[cas][i][j+1]==4||map[cas][i][j+1]==7)
			{
				//做箱子的下一个地方判断,能不能走
				if(map[cas][i][j+2]==0||map[cas][i][j+2]==3)
				 {
				 	//新的地方箱子来了
					 map[cas][i][j+2]+=4;
					 //箱子的位置:箱子(-4)走了,人(+5)来了
					 map[cas][i][j+1]+=1; //只显示最终结果; 
					 //原来的地方人走了
					 map[cas][i][j]-=5;
				 }
			}
			break;
	 }
}

//胜负判断
//用什么判断胜负:想清楚:箱子到达目的地的个数
int gameover()
{
	int count=0;
	//所有的地方都找一遍
	for(int i=0;i<12;i++)
	{
		for(int j=0;j<13;j++)
		{
			if(map[cas][i][j]==7)
			count++;
		}
	}
	return count;
}
//箱子数是0的时候,也是表示胜利 
int gameover2()
{
	int count=boxSum[cas];
	//所有的地方都找一遍
	for(int i=0;i<12;i++)
	{
		for(int j=0;j<13;j++)
		{
			if(map[cas][i][j]==boxSum[cas])
			count--;
		}
	}
	return count;
}

int main()
{
		printf("游戏开始");
		Sleep(1100);
		system("cls");
		printf("第%d关",cas+1);
		getch();
		system("cls");
		while(1)
		{
			drawMap();
			if(gameover()==boxSum[cas])
			{
				cas++;
				if(cas==5)
				break;
				system("cls");
				printf("第%d关",cas+1);
				Sleep(900);
				
			}
			keyDown();
			system("cls");
		 
		}
		printf("Game over\n");
		system("pause");
		return 0;
	
}

实例二

#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<Windows.h>
using namespace std;
int mapsecond[11][13]= {
	{0,0,0,1,1,1,1,1,1,1,0,0,0},
	{1,1,1,1,0,0,0,0,0,1,0,0,0},
	{1,0,0,0,3,1,1,1,0,1,0,0,0},
	{1,0,1,0,1,0,0,0,0,1,1,0,0},
	{1,0,1,0,4,0,4,1,3,0,1,0,0},
	{1,0,1,0,0,1,0,0,1,0,1,0,0},
	{1,0,3,1,4,0,4,0,1,0,1,0,0},
	{1,1,0,0,0,0,1,0,1,0,1,1,1},
	{0,1,0,1,1,1,3,0,0,0,5,0,1},
	{0,1,0,0,0,0,0,1,1,0,0,0,1},
	{0,1,1,1,1,1,1,1,1,1,1,1,1},
};
int mapfourth[9][11] = {
	{0,1,1,1,1,1,1,1,1,1,0},  
	{0,1,0,0,0,1,0,0,0,1,0},  
	{0,1,0,4,4,4,4,4,0,1,0},  
	{0,1,0,4,0,4,0,4,0,1,1},  
	{0,1,0,0,0,0,0,0,4,0,1},  
	{1,1,0,1,1,1,1,0,4,0,1},
	{1,0,8,3,3,3,3,1,0,0,1},
	{1,0,3,3,3,3,3,0,0,1,1},
	{1,1,1,1,1,1,1,1,1,1,0},
};
int mapthird[7][8]= {
	{0,1,1,1,1,1,1,0},
	{0,1,0,0,0,0,1,1},
	{1,1,3,1,1,4,0,1},
	{1,0,3,3,4,0,0,1},
	{1,0,0,1,4,0,0,1},
	{1,0,0,5,0,1,1,1},
	{1,1,1,1,1,1,0,0},	
};
int mapfirst[7][8]= {
	{1,1,1,1,1,1,1,1},
	{1,0,0,0,0,0,0,1},
	{1,0,1,1,0,1,0,1},
	{1,0,3,4,0,4,3,1},
	{1,0,1,1,0,1,0,1},
	{1,0,0,0,5,0,0,1},
	{1,1,1,1,1,1,1,1},
};
void drawGamethird() {
	for(int a=0; a<7;a++)         //0:空地 1: ■表示墙 3:☆ 目的地 4:★箱子  5.※人 7.⊙表示箱子推到了目的的显示
		                          //8:※人到达目的地,显示人
	{
		for(int k=0; k<8; k++) {
			switch(mapthird[a][k]) {
				case 0:cout<<"  ";break;
				case 1:cout<<"■";break;
				case 3:cout<<"☆";break;
				case 4:cout<<"★";break;
				case 5:cout<<"※";break;
				case 7:cout<<"⊙";break;
				case 8:cout<<"※";break;
			}
		}
		cout<<endl;
	}
}
void drawGamesecond() {
	for(int b=0; b<11;b++)
	{
		for(int z=0; z<13; z++) {
			switch(mapsecond[b][z]) {
				case 0:cout<<"  ";break;
				case 1:cout<<"■";break;
				case 3:cout<<"☆";break;
				case 4:cout<<"★";break;
				case 5:cout<<"※";break;
				case 7:cout<<"⊙";break;
				case 8:cout<<"※";break;
			}
		}
		cout<<endl;
	}
}
void drawGamefourth() { 
	for(int c=0; c<9;c++)
	{
		for(int d=0; d<11; d++) {
			switch(mapfourth[c][d]) {
				case 0:cout<<"  ";break;
				case 1:cout<<"■";break;
				case 3:cout<<"☆";break;
				case 4:cout<<"★";break;
				case 5:cout<<"※";break;
				case 7:cout<<"⊙";break;
				case 8:cout<<"※";break;
			}
		}
		cout<<endl;
	}
}
void drawGamefirst() {
	for(int e=0; e<7; e++)
	{
		for(int f=0; f<8; f++) {
			switch(mapfirst[e][f]) {case 0:cout<<"  ";break;
				case 1:cout<<"■";break;
				case 3:cout<<"☆";break;
				case 4:cout<<"★";break;
				case 5:cout<<"※";break;
				case 7:cout<<"⊙";break;
				case 8:cout<<"※";break;
			}
		}
		cout<<endl;
	}
}
void keydownfourth() { 
      int i,j;
	for(int a=0; a<9; a++) {
		for(int k=0; k<11; k++) {
			if(mapfourth[a][k]==5||mapfourth[a][k]==8) {
				i=a;
				j=k;
			}
		}
	}
	char ch=getch();
	switch(ch) {
		case 'W':
		case 'w':
		case  72:
			if(mapfourth[i-1][j]==0||mapfourth[i-1][j]==3) { 
				mapfourth[i][j]=mapfourth[i][j]-5;
				mapfourth[i-1][j]=mapfourth[i-1][j]+5; 
			} else if(mapfourth[i-1][j]==4
			          ||mapfourth[i-1][j]==7) { 
				if(mapfourth[i-2][j]==0||mapfourth[i-2][j]==3) { 
					mapfourth[i-2][j]=mapfourth[i-2][j]+4;
					mapfourth[i-1][j]=mapfourth[i-1][j]+1;
					mapfourth[i][j]=mapfourth[i][j]-5;
				}
			}
			break;
		case 's':
		case 'S':
		case  80:
			if(mapfourth[i+1][j]==0||mapfourth[i+1][j]==3) { 
				mapfourth[i][j]=mapfourth[i][j]-5;
				mapfourth[i+1][j]=mapfourth[i+1][j]+5; 
			} else if(mapfourth[i+1][j]==4
			          ||mapfourth[i+1][j]==7) { 
				if(mapfourth[i+2][j]==0||mapfourth[i+2][j]==3) { 
					mapfourth[i+2][j]=mapfourth[i+2][j]+4;
					mapfourth[i+1][j]=mapfourth[i+1][j]+1;
					mapfourth[i][j]=mapfourth[i][j]-5;
				}
			}
			break;
		case 'a':
		case 'A':
		case 75:
			if(mapfourth[i][j-1]==0||mapfourth[i][j-1]==3) { 
				mapfourth[i][j]=mapfourth[i][j]-5;
				mapfourth[i][j-1]=mapfourth[i][j-1]+5; 
			} else if(mapfourth[i][j-1]==4
			          ||mapfourth[i][j-1]==7) { 
				if(mapfourth[i][j-2]==0||mapfourth[i][j-2]==3) { 
					mapfourth[i][j-2]=mapfourth[i][j-2]+4;
					mapfourth[i][j-1]=mapfourth[i][j-1]+1;
					mapfourth[i][j]=mapfourth[i][j]-5;
				}
			}
			break;
		case 'D':
		case 'd':
		case 77:
			if(mapfourth[i][j+1]==0||mapfourth[i][j+1]==3) { 
				mapfourth[i][j]=mapfourth[i][j]-5;
				mapfourth[i][j+1]=mapfourth[i][j+1]+5; 
			} else if(mapfourth[i][j+1]==4
			          ||mapfourth[i][j+1]==7) { 
				if(mapfourth[i][j+2]==0||mapfourth[i][j+2]==3) { 
					mapfourth[i][j+2]=mapfourth[i][j+2]+4;
					mapfourth[i][j+1]=mapfourth[i][j+1]+1;
					mapfourth[i][j]=mapfourth[i][j]-5;
				}
			}
			break;
	}
}
void keydownsecond() { 
	int i,j;
	for(int b=0; b<11; b++) {
		for(int z=0; z<13; z++) {
			if(mapsecond[b][z]==5||mapsecond[b][z]==8) {
				i=b;
				j=z;
			}
		}
	}
	char ch=getch();
	switch(ch) {
		case 'W':
		case 'w':
		case  72:
			if(mapsecond[i-1][j]==0||mapsecond[i-1][j]==3) { 
				mapsecond[i][j]=mapsecond[i][j]-5;
				mapsecond[i-1][j]=mapsecond[i-1][j]+5; 
			} else if(mapsecond[i-1][j]==4||mapsecond[i-1][j]==7) { 
				if(mapsecond[i-2][j]==0||mapsecond[i-2][j]==3) { 
					mapsecond[i-2][j]=mapsecond[i-2][j]+4;
					mapsecond[i-1][j]=mapsecond[i-1][j]+1;
					mapsecond[i][j]=mapsecond[i][j]-5;
				}
			}
			break;
		case 's':
		case 'S':
		case  80:
			if(mapsecond[i+1][j]==0||mapsecond[i+1][j]==3) { 
				mapsecond[i][j]=mapsecond[i][j]-5;
				mapsecond[i+1][j]=mapsecond[i+1][j]+5; 
			} else if(mapsecond[i+1][j]==4
			          ||mapsecond[i+1][j]==7) { 
				if(mapsecond[i+2][j]==0||mapsecond[i+2][j]==3) { 
					mapsecond[i+2][j]=mapsecond[i+2][j]+4;
					mapsecond[i+1][j]=mapsecond[i+1][j]+1;
					mapsecond[i][j]=mapsecond[i][j]-5;
				}
			}
			break;
		case 'a':
		case 'A':
		case 75:
			if(mapsecond[i][j-1]==0||mapsecond[i][j-1]==3) { 
				mapsecond[i][j]=mapsecond[i][j]-5;
				mapsecond[i][j-1]=mapsecond[i][j-1]+5; 
			} else if(mapsecond[i][j-1]==4
			          ||mapsecond[i][j-1]==7) { 
				if(mapsecond[i][j-2]==0||mapsecond[i][j-2]==3) { 
					mapsecond[i][j-2]=mapsecond[i][j-2]+4;
					mapsecond[i][j-1]=mapsecond[i][j-1]+1;
					mapsecond[i][j]=mapsecond[i][j]-5;
				}
			}
			break;
		case 'D':
		case 'd':
		case 77:
			if(mapsecond[i][j+1]==0||mapsecond[i][j+1]==3) { 
				mapsecond[i][j]=mapsecond[i][j]-5;
				mapsecond[i][j+1]=mapsecond[i][j+1]+5; 
			} else if(mapsecond[i][j+1]==4
			          ||mapsecond[i][j+1]==7) { 
				if(mapsecond[i][j+2]==0||mapsecond[i][j+2]==3) { 
					mapsecond[i][j+2]=mapsecond[i][j+2]+4;
					mapsecond[i][j+1]=mapsecond[i][j+1]+1;
					mapsecond[i][j]=mapsecond[i][j]-5;
				}
			}
			break;
	}
}
void keydownthird() { 
	int i,j;
	for(int c=0; c<7; c++) {
		for(int d=0; d<8; d++) {
			if(mapthird[c][d]==5||mapthird[c][d]==8) {
				i=c;
				j=d;
			}
		}
	}
	char ch=getch();
	switch(ch) {
		case 'W':
		case 'w':
		case  72:
			if(mapthird[i-1][j]==0||mapthird[i-1][j]==3) { 
				mapthird[i][j]=mapthird[i][j]-5;
				mapthird[i-1][j]=mapthird[i-1][j]+5; 
			} else if(mapthird[i-1][j]==4
			          ||mapthird[i-1][j]==7) { 
				if(mapthird[i-2][j]==0||mapthird[i-2][j]==3) { 
					mapthird[i-2][j]=mapthird[i-2][j]+4;
					mapthird[i-1][j]=mapthird[i-1][j]+1;
					mapthird[i][j]=mapthird[i][j]-5;
				}
			}
			break;
		case 's':
		case 'S':
		case  80:
			if(mapthird[i+1][j]==0||mapthird[i+1][j]==3) { 
				mapthird[i][j]=mapthird[i][j]-5;
				mapthird[i+1][j]=mapthird[i+1][j]+5; 
			} else if(mapthird[i+1][j]==4
			          ||mapthird[i+1][j]==7) { 
				if(mapthird[i+2][j]==0||mapthird[i+2][j]==3) { 
					mapthird[i+2][j]=mapthird[i+2][j]+4;
					mapthird[i+1][j]=mapthird[i+1][j]+1;
					mapthird[i][j]=mapthird[i][j]-5;
				}
			}
			break;
		case 'a':
		case 'A':
		case 75:
			if(mapthird[i][j-1]==0||mapthird[i][j-1]==3) { 
				mapthird[i][j]=mapthird[i][j]-5;
				mapthird[i][j-1]=mapthird[i][j-1]+5; 
			} else if(mapthird[i][j-1]==4
			          ||mapthird[i][j-1]==7) { 
				if(mapthird[i][j-2]==0||mapthird[i][j-2]==3) { 
					mapthird[i][j-2]=mapthird[i][j-2]+4;
					mapthird[i][j-1]=mapthird[i][j-1]+1;
					mapthird[i][j]=mapthird[i][j]-5;
				}
			}
			break;
		case 'D':
		case 'd':
		case 77:
			if(mapthird[i][j+1]==0||mapthird[i][j+1]==3) {
				mapthird[i][j]=mapthird[i][j]-5;
				mapthird[i][j+1]=mapthird[i][j+1]+5;
			} else if(mapthird[i][j+1]==4
			          ||mapthird[i][j+1]==7) {
				if(mapthird[i][j+2]==0||mapthird[i][j+2]==3) { 
					mapthird[i][j+2]=mapthird[i][j+2]+4;
					mapthird[i][j+1]=mapthird[i][j+1]+1;
					mapthird[i][j]=mapthird[i][j]-5;
				}
			}
			break;
	}
}
void keydownfirst() { 
	int i,j;
	for(int e=0; e<7; e++) {
		for(int f=0; f<8; f++) {
			if(mapfirst[e][f]==5||mapfirst[e][f]==8) {
				i=e;
				j=f;
			}
		}
	}
	
	char ch=getch();
	switch(ch) {
		case 'W':
		case 'w':
		case  72:
			if(mapfirst[i-1][j]==0||mapfirst[i-1][j]==3) { 
				mapfirst[i][j]=mapfirst[i][j]-5;
				mapfirst[i-1][j]=mapfirst[i-1][j]+5; 
			} else if(mapfirst[i-1][j]==4
			          ||mapfirst[i-1][j]==7) { 
				if(mapfirst[i-2][j]==0||mapfirst[i-2][j]==3) { 
					mapfirst[i-2][j]=mapfirst[i-2][j]+4;
					mapfirst[i-1][j]=mapfirst[i-1][j]+1;
					mapfirst[i][j]=mapfirst[i][j]-5;
				}
			}
			break;
		case 's':
		case 'S':
		case  80:
			if(mapfirst[i+1][j]==0||mapfirst[i+1][j]==3) { 
				mapfirst[i][j]=mapfirst[i][j]-5;
				mapfirst[i+1][j]=mapfirst[i+1][j]+5; 
			} else if(mapfirst[i+1][j]==4
			          ||mapfirst[i+1][j]==7) { 
				if(mapfirst[i+2][j]==0||mapfirst[i+2][j]==3) { 
					mapfirst[i+2][j]=mapfirst[i+2][j]+4;
					mapfirst[i+1][j]=mapfirst[i+1][j]+1;
					mapfirst[i][j]=mapfirst[i][j]-5;
				}
			}
			break;
		case 'a':
		case 'A':
		case 75:
			if(mapfirst[i][j-1]==0||mapfirst[i][j-1]==3) { 
				mapfirst[i][j]=mapfirst[i][j]-5;
				mapfirst[i][j-1]=mapfirst[i][j-1]+5; 
			} else if(mapfirst[i][j-1]==4
			          ||mapfirst[i][j-1]==7) { 
				if(mapfirst[i][j-2]==0||mapfirst[i][j-2]==3) { 
					mapfirst[i][j-2]=mapfirst[i][j-2]+4;
					mapfirst[i][j-1]=mapfirst[i][j-1]+1;
					mapfirst[i][j]=mapfirst[i][j]-5;
				}
			}
			break;
		case 'D':
		case 'd':
		case 77:
			if(mapfirst[i][j+1]==0||mapfirst[i][j+1]==3) { 
				mapfirst[i][j]=mapfirst[i][j]-5;
				mapfirst[i][j+1]=mapfirst[i][j+1]+5; 
			} else if(mapfirst[i][j+1]==4
			          ||mapfirst[i][j+1]==7) { 
				if(mapfirst[i][j+2]==0||mapfirst[i][j+2]==3) { 
					mapfirst[i][j+2]=mapfirst[i][j+2]+4;
					mapfirst[i][j+1]=mapfirst[i][j+1]+1;
					mapfirst[i][j]=mapfirst[i][j]-5;
				}
			}
			break;
	}
}
void gameoverfourth ()
{
	int k=0;
	for(int i=0;i<9;i++)
	{
	for(int j=0;j<11;j++)
	{
	  if(mapfourth[i][j]==4)
	   k++;
	}
    }
	if(k==0)
	{
	 cout<<"恭喜你,你赢了!"<<endl;
	 exit(0);
    }
}
void gameoverthird()
{
	int k=0;
	for(int i=0;i<7;i++)
	{
	for(int j=0;j<8;j++){
	 	if(mapthird[i][j]==4)
	 	 k++;}
     }    
	 if(k==0) {	
	 cout<<"恭喜你,顺利进入下一关"<<endl;
	 system("cls");
	 while(1){
	  system("cls");
	  drawGamefourth();
	  cout<<"当前关卡第2关"<<endl;
	cout<<"本关不给提示,祝你好运!"<<endl;
	  keydownfourth();
	  gameoverfourth();
	  }
    }
}
void gameoversecond()
{
	int k=0;
	for(int i=0;i<11;i++)
	{
	for(int j=0;j<13;j++)
	{
	  if(mapsecond[i][j]==4)
	   k++;
	}
    }
	if(k==0)
	{
	 cout<<"恭喜你,你通过了此关!"<<endl;
	 Sleep(1000);
	 while(1)
	 {
	system("cls");
	drawGamethird();
	cout<<"当前关卡第3关"<<endl;
	cout<<"  这一关比较难"<<endl;
	keydownthird();
	gameoverthird();
    }
    }
}
void gameoverfirst()
{
	int k=0;
	for(int i=0; i<7; i++) {
		for (int j=0; j<8; j++) {
			if(mapfirst[i][j]==4)
				k++;
		}
	}
	while(k==0){
		printf("恭喜你,你通过过了此关!\n");
		Sleep(1000);
	     while(1) 
			   { 
			    system("cls");
				drawGamesecond();
				cout<<"当前关卡第2关"<<endl;
				cout<<"D右1,B上1右2下1,A左1上2,D下2右1,A右1,C下1左2上1,D右2左4."<<endl;
				keydownsecond();
				gameoversecond();}
		}	        
}
int main() {
	while(1){ 
	system("color 3F ");
	cout<<"———————请选关————————"<<endl;
	cout<<"          1. 第一关                " <<endl;
	cout<<"          2. 第二关                " <<endl;
	cout<<"          3. 第三关                " <<endl;
	cout<<"          4. 最后一关              "<<endl;
	cout<<"          5.退出游戏               " <<endl;
	cout<<"提示:每通关一次你都会进入下一关"<<endl;
	cout<<"注意:0:空地 1: ■表示墙 3:☆ 目的地 4:★箱子  5.※人 7.⊙表示箱子推到了目的的显示 8:※人到达目的地"<<endl;
       int n;
	   cin>>n; 
	      if(n==1){
	      	  while(1){
                system("cls");
				drawGamefirst();
				cout<<"  当前关卡第[1]关"<<endl; 
				keydownfirst();
				gameoverfirst();}
		  }
	     if(n==2){ 
		    while(1) { 
			    system("cls");
				drawGamesecond(); 
				cout<<"  当前关卡第[2]关"<<endl;
				cout<<"  提示:D右1,B上1右2下1,A左1上2,D下2右1,A右1,C下1左2上1,D右2左4."<<endl; 
				keydownsecond();
				gameoversecond();}
			 }
	   if(n==3){
	     while(1) 
		 {      system("cls");
				drawGamethird();
				cout<<"  当前关卡第[3]关"<<endl; 
				cout<<"  这一关比较难"<<endl;
			    keydownthird();
			    gameoverthird(); }
		}
	 if(n==4)
	 {
	   while(1) 
	   {        system("cls");
				drawGamefourth();
				cout<<"  当前关卡为最后一关"<<endl; 
				cout<<"本关不给提示,祝你好运!"<<endl; 
				keydownfourth();
				gameoverfourth();

			 }
		}
	if(n==5)
	system("cls");
    cout<<"已经退出游戏"<<endl;exit(-1);
}
	return 0;
}

实例三

#include<iostream>
#include<windows.h>
#include<conio.h> 
//#include<stdlib.h>
using namespace std;

//0.空地 1.墙 2.人 3.箱子 4.目的地 5.箱子与目的地 6.人与目的地 
int map[5][10][10]=
{
	{
		{0,0,0,0,1,1,1,0,0,0},
		{0,0,0,0,1,4,1,0,0,0},
		{0,0,0,0,1,0,1,0,0,0},
		{0,0,0,0,1,3,1,0,0,0},
		{1,1,1,1,1,0,1,1,1,1},
		{1,4,0,0,3,2,3,0,4,1},
		{1,1,1,1,1,3,1,1,1,1},
		{0,0,0,0,1,0,1,0,0,0},
		{0,0,0,0,1,4,1,0,0,0},
		{0,0,0,0,1,1,1,0,0,0},
	},
	{
		{1,1,1,1,1,0,0,0,0,0},
		{1,0,0,0,1,0,0,0,0,0},
		{1,0,3,0,1,0,1,1,1,0},
		{1,0,3,0,1,0,1,4,1,0},
		{1,1,1,2,1,1,1,4,1,0},
		{0,1,1,3,0,0,0,4,1,0},
		{0,1,0,0,0,1,0,0,1,0},
		{0,1,0,0,0,1,1,1,1,0},
		{0,1,1,1,1,1,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0},
	},
	{
		{0,1,1,1,1,1,1,1,0,0},
		{0,1,0,0,0,0,0,1,1,1},
		{1,1,3,1,1,1,0,0,0,1},
		{1,0,2,0,3,0,0,3,0,1},
		{1,0,4,4,1,0,3,0,1,1},
		{1,1,4,4,1,0,0,0,1,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,1,1,1,1,0,0,0,0,0},
		{1,1,0,0,1,0,0,0,0,0},
		{1,2,3,0,1,0,0,0,0,0},
		{1,1,3,0,1,1,0,0,0,0},
		{1,1,0,3,0,1,0,0,0,0},
		{1,4,3,0,0,1,0,0,0,0},
		{1,4,4,5,4,1,0,0,0,0},
		{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,1,1,1,1,1,0,0,0,0},
		{0,1,2,0,1,1,1,0,0,0},
		{0,1,0,3,0,0,1,0,0,0},
		{1,1,1,0,1,0,1,1,0,0},
		{1,4,1,0,1,0,0,1,0,0},
		{1,4,3,0,0,1,0,1,0,0},
		{1,4,0,0,0,3,0,1,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},
	}
};

int zongfen(int map[5][10][10],int level)
{
	int a=0,i,j;
	for(i=0;i<10;i++)
	{
		for(j=0;j<10;j++)
		{
			if(map[level][i][j]==4)
			{
				a++;
			}
		}
	}
	return a;
}

//0.空地 1.墙 2.人 3.箱子 4.目的地 5.箱子与目的地 6.人与目的地 
void getmap(int map[5][10][10],int level)
{
	for(int i=0;i<10;i++)
	{
		for(int j=0;j<10;j++)
		{
			switch(map[level][i][j])
			{
				case 0://空地
					cout<<"  "; 
					break;
				case 1://墙 
					cout<<"■"; 
					break;
				case 2://人 
					cout<<"♀";
					break;
				case 3://箱子 
					cout<<"□";
					break;
				case 4://目的地
					cout<<"☆"; 
					break;
				case 5://箱子与目的地
					cout<<"★"; 
					break;
				case 6://人与目的地
					cout<<"♀"; 
					break;
			}
		}
		cout<<endl; 
	}
}

int main()
{
	system("mode con cols=40 lines=20");
	int score=0;//分数
	int level=0;
	char input;//输入字符
	system("color ed");
	
	while(1)
	{
		
		system("cls");
		cout<<"\t\t推箱子"<<endl;
		cout<<"\t选择关卡:(共五关)"<<endl;
		int l;
		cin>>l;
		if(l>=1&&l<=5)
		{
			level=l-1;
			break;
		}
		else
		{
			cout<<"暂无该关卡"<<endl;
			return 0; 
			system("pause");
		}
	}
	int b[5];
	for(int q=0;q<5;q++)
	{
		b[q]=zongfen(map,q);
	}
	while(1)
	{
		system("cls");
		cout<<"\t推箱子第"<<level+1<<"关\t\t"<<endl;
		cout<<"基本操作:w-上 s-下 a-左 d-右 *-退出"<<endl<<endl;
		getmap(map,level);
		
		cout<<endl<<"分数:"<<score<<endl;//输出分数 
		
		int i,j;
		for(i=0;i<10;i++)
		{
			for(j=0;j<10;j++)
			{
				if(map[level][i][j]==2 || map[level][i][j]==6)
					break;
			}
			if(map[level][i][j]==2 || map[level][i][j]==6)
					break;
		}
		cout<<"坐标:("<<i+1<<","<<j+1<<")"<<endl;
		
		if(score==b[level])
		{
			int o=1;
			cout<<"1.下一关"<<endl<<"2.退出"<<endl<<"(请输入两次回车)"<<endl;
			while(o)
			{
				int p;
				cin>>p;
				switch(p)
				{
					case 1:
						level++;
						score=0;
						o=0;
						break;
					case 2:
						o=0;
						exit(0);
						break;
					default:
						cout<<"无效操作"<<endl;
						break;
				}	
			}
		}
		
		input = getch();//接受字符asdw 

		//1.人前是空地
		//2.人前是目的地 
		//3.人前是箱子 
		//4.人前是箱子与目的地 
		switch(input)
		{
			case 'w':
				//1.人前是空地 
				if(map[level][i-1][j]==0)
				{
					map[level][i-1][j]=2;
					if(map[level][i][j]==6){//人在目的地
						map[level][i][j]=4; 
					}else{
						map[level][i][j]=0;
					}
				}
				//2.人前是目的地 
				if(map[level][i-1][j]==4)
				{
					map[level][i-1][j]=6;
					if(map[level][i][j]==6){//人在目的地
						map[level][i][j]=4; 
					}else{
						map[level][i][j]=0;
					}
				}
				//3.人前是箱子 
				if(map[level][i-1][j]==3)
				{
					//箱子前是空地
					if(map[level][i-2][j]==0)
					{
						map[level][i-2][j]=3;
						map[level][i-1][j]=2;
						if(map[level][i][j]==6){//人在目的地
							map[level][i][j]=4;
						}else{
							map[level][i][j]=0;
						}
					}
					//箱子前是目的地
					if(map[level][i-2][j]==4)
					{
						score++;
						map[level][i-2][j]=5;
						map[level][i-1][j]=2;
						if(map[level][i][j]==6){//人在目的地
							map[level][i][j]=4; 
						}else{
							map[level][i][j]=0;
						}
					}
				}
				//4.人前是箱子与目的地
				if(map[level][i-1][j]==5)
				{
					//箱子与目的地前是空地
					if(map[level][i-2][j]==0)
					{
						score--;
						map[level][i-2][j]=3;
						map[level][i-1][j]=6;
						if(map[level][i][j]==6){//人在目的地
							map[level][i][j]=4;
						}else{
							map[level][i][j]=0;
						}
					}
					//箱子与目的地前是目的地 
					if(map[level][i-2][j]==4)
					{
						map[level][i-2][j]=5;
						map[level][i-1][j]=6;
						if(map[level][i][j]==6){//人在目的地
							map[level][i][j]=4; 
						}else{
							map[level][i][j]=0;
						}	
					}
				}
				break;
			case 'a':
				//1.人前是空地 
				if(map[level][i][j-1]==0)
				{
					map[level][i][j-1]=2;
					if(map[level][i][j]==6){//人在目的地
						map[level][i][j]=4; 
					}else{
						map[level][i][j]=0;
					}
				}
				//2.人前是目的地 
				if(map[level][i][j-1]==4)
				{
					map[level][i][j-1]=6;
					if(map[level][i][j]==6){//人在目的地
						map[level][i][j]=4; 
					}else{
						map[level][i][j]=0;
					}
				}
				//3.人前是箱子 
				if(map[level][i][j-1]==3)
				{
					//箱子前是空地
					if(map[level][i][j-2]==0)
					{
						map[level][i][j-2]=3;
						map[level][i][j-1]=2;
						if(map[level][i][j]==6){//人在目的地
							map[level][i][j]=4;
						}else{
							map[level][i][j]=0;
						}
					}
					//箱子前是目的地
					if(map[level][i][j-2]==4)
					{
						score++;
						map[level][i][j-2]=5;
						map[level][i][j-1]=2;
						if(map[level][i][j]==6){//人在目的地
							map[level][i][j]=4; 
						}else{
							map[level][i][j]=0;
						}
					}
				}
				//4.人前是箱子与目的地
				if(map[level][i][j-1]==5)
				{
					//箱子与目的地前是空地
					if(map[level][i][j-2]==0)
					{
						score--;
						map[level][i][j-2]=3;
						map[level][i][j-1]=6;
						if(map[level][i][j]==6){//人在目的地
							map[level][i][j]=4;
						}else{
							map[level][i][j]=0;
						}
					}
					//箱子与目的地前是目的地 
					if(map[level][i][j-2]==4)
					{
						map[level][i][j-2]=5;
						map[level][i][j-1]=6;
						if(map[level][i][j]==6){//人在目的地
							map[level][i][j]=4; 
						}else{
							map[level][i][j]=0;
						}	
					}
				}
				break;
			case 's':
				//1.人前是空地 
				if(map[level][i+1][j]==0)
				{
					map[level][i+1][j]=2;
					if(map[level][i][j]==6){//人在目的地
						map[level][i][j]=4; 
					}else{
						map[level][i][j]=0;
					}
				}
				//2.人前是目的地 
				if(map[level][i+1][j]==4)
				{
					map[level][i+1][j]=6;
					if(map[level][i][j]==6){//人在目的地
						map[level][i][j]=4; 
					}else{
						map[level][i][j]=0;
					}
				}
				//3.人前是箱子 
				if(map[level][i+1][j]==3)
				{
					//箱子前是空地
					if(map[level][i+2][j]==0)
					{
						map[level][i+2][j]=3;
						map[level][i+1][j]=2;
						if(map[level][i][j]==6){//人在目的地
							map[level][i][j]=4;
						}else{
							map[level][i][j]=0;
						}
					}
					//箱子前是目的地
					if(map[level][i+2][j]==4)
					{
						score++;
						map[level][i+2][j]=5;
						map[level][i+1][j]=2;
						if(map[level][i][j]==6){//人在目的地
							map[level][i][j]=4; 
						}else{
							map[level][i][j]=0;
						}
					}
				}
				//4.人前是箱子与目的地
				if(map[level][i+1][j]==5)
				{
					//箱子与目的地前是空地
					if(map[level][i+2][j]==0)
					{
						score--;
						map[level][i+2][j]=3;
						map[level][i+1][j]=6;
						if(map[level][i][j]==6){//人在目的地
							map[level][i][j]=4;
						}else{
							map[level][i][j]=0;
						}
					}
					//箱子与目的地前是目的地 
					if(map[level][i+2][j]==4)
					{
						map[level][i+2][j]=5;
						map[level][i+1][j]=6;
						if(map[level][i][j]==6){//人在目的地
							map[level][i][j]=4; 
						}else{
							map[level][i][j]=0;
						}	
					}
				}
				break;
			case 'd':
				//1.人前是空地 
				if(map[level][i][j+1]==0)
				{
					map[level][i][j+1]=2;
					if(map[level][i][j]==6){//人在目的地
						map[level][i][j]=4; 
					}else{
						map[level][i][j]=0;
					}
				}
				//2.人前是目的地 
				if(map[level][i][j+1]==4)
				{
					map[level][i][j+1]=6;
					if(map[level][i][j]==6){//人在目的地
						map[level][i][j]=4; 
					}else{
						map[level][i][j]=0;
					}
				}
				//3.人前是箱子 
				if(map[level][i][j+1]==3)
				{
					//箱子前是空地
					if(map[level][i][j+2]==0)
					{
						map[level][i][j+2]=3;
						map[level][i][j+1]=2;
						if(map[level][i][j]==6){//人在目的地
							map[level][i][j]=4;
						}else{
							map[level][i][j]=0;
						}
					}
					//箱子前是目的地
					if(map[level][i][j+2]==4)
					{
						score++;
						map[level][i][j+2]=5;
						map[level][i][j+1]=2;
						if(map[level][i][j]==6){//人在目的地
							map[level][i][j]=4; 
						}else{
							map[level][i][j]=0;
						}
					}
				}
				//4.人前是箱子与目的地
				if(map[level][i][j+1]==5)
				{
					//箱子与目的地前是空地
					if(map[level][i][j+2]==0)
					{
						score--;
						map[level][i][j+2]=3;
						map[level][i][j+1]=6;
						if(map[level][i][j]==6){//人在目的地
							map[level][i][j]=4;
						}else{
							map[level][i][j]=0;
						}
					}
					//箱子与目的地前是目的地 
					if(map[level][i][j+2]==4)
					{
						map[level][i][j+2]=5;
						map[level][i][j+1]=6;
						if(map[level][i][j]==6){//人在目的地
							map[level][i][j]=4; 
						}else{
							map[level][i][j]=0;
						}
					}
				}
				break;
			case '*':
				exit(0);
				break; 
			default:
				break;
		}
	}
	return 0;
}

实例四

#include<stdio.h>
#include<conio.h> 
#include<windows.h>
int map0();
int map1();
int map2();
int map3();
int map4();
int shuying(int map[9][11]);
int zhuyao(int map[9][11]); 
int map0()
{
	int k=1;
	static int map[9][11]={
	{0,0,0,0,1,1,1,0,0,0,0},
	{0,0,0,0,1,4,1,0,0,0,0},
	{0,0,0,0,1,0,1,1,1,1,1},
	{1,1,1,1,1,3,3,0,0,4,1},
	{1,4,0,0,3,2,0,1,1,1,1}, 
	{1,1,1,1,1,1,3,1,0,0,0},
	{0,0,0,0,0,1,0,1,0,0,0},
	{0,0,0,0,0,1,4,1,0,0,0},
	{0,0,0,0,0,1,1,1,0,0,0}};
	
	k=shuying(map);
	if(k==0)
	return k;
	k=zhuyao(map);
	printf(" ");
	if(k==2)
	return k; 
//	return 2;
 }
int map1()
{
	static int map[9][11]={
	{0,0,0,0,1,1,1,1,1,1,1},
	{1,1,1,1,1,0,0,0,0,0,1},
	{1,4,1,0,0,0,0,0,0,0,1},
	{1,0,3,0,3,0,0,1,2,1,1},
	{1,0,0,0,0,0,0,0,3,0,1},
	{1,1,0,0,0,0,0,0,0,0,1},
	{0,1,4,0,1,0,0,0,1,0,1},
	{0,1,1,0,0,0,0,0,4,1,1},
	{0,0,1,1,1,1,1,1,1,1,0}};
	int k=1;
	k=shuying(map);
	if(k==0)
	return k;
	k=zhuyao(map);
	if(k==2)
	return k; 
}
int map2()
{
	static int map[9][11]={
	{0,0,1,1,1,1,1,1,1,1,1},
	{0,1,1,4,0,0,0,0,0,2,1},
	{1,1,0,0,0,0,0,1,0,1,1},
	{1,0,0,1,0,0,0,0,3,0,1},
	{1,0,3,3,4,0,0,0,0,0,1},
	{1,1,0,1,0,0,0,0,1,1,1},
	{0,1,4,0,0,0,0,0,1,0,0},
	{0,1,1,0,0,0,0,0,1,0,0},
	{0,0,1,1,1,1,1,1,1,0,0}};
	int k=1;
	k=shuying(map);
	if(k==0)
	return k;
	k=zhuyao(map); 
	if(k==2)
	return k;
}
int map3()
{
	static int map[9][11]={
	{0,0,1,1,1,1,0,0,0,0,0},
	{1,1,1,4,0,1,1,1,1,1,0},
	{1,4,1,0,0,0,0,0,0,1,1},
	{1,0,3,0,1,1,0,0,0,0,1},
	{1,2,0,3,0,0,0,0,4,0,1},
	{1,0,0,0,0,0,0,0,0,1,1},
	{1,1,1,0,0,0,0,3,1,1,0},
	{0,0,1,1,0,0,0,0,1,0,0},
	{0,0,0,1,1,1,1,1,1,0,0}};
	int k=1;
	k=shuying(map);
	if(k==0)
	return k;
	k=zhuyao(map); 
	if(k==2)
	return k;
}
int map4()
{
	static int map[9][11]={
	{0,1,1,1,1,1,1,1,1,1,0},
	{0,1,0,0,0,1,0,0,0,1,0},
	{0,1,0,0,3,0,0,0,0,1,0},
	{0,1,0,3,0,3,3,3,0,1,1},
	{0,1,0,0,0,2,0,0,0,0,1},
	{1,1,0,0,1,1,1,0,3,0,1},
	{1,0,4,4,0,4,0,0,0,0,1},
	{1,0,4,4,0,4,4,3,0,1,1},
	{1,1,1,1,1,1,1,1,1,1,0}};
	int k=1;
	k=shuying(map);
	if(k==0)
	return k;
	k=zhuyao(map);
	if(k==2)
	return k; 
}
int shuying(int map[9][11])
{
	int i,j,a=0;
	for(i=0;i<9;i++)
	{
		for(j=0;j<11;j++)
		{
			if(map[i][j]==3)
			a++;
		}
	}
	if(a==0)
	{
		printf("恭喜你过关^_^");
		system("pause");
		return a;
	}
	else
	return 3;
}
int zhuyao(int map[9][11])
{
	int i,j,a,b,c;
	for(i=0;i<9;i++)
	{
		for(j=0;j<11;j++)
		{
			if(map[i][j]==0)
			printf("  ");
			if(map[i][j]==1)
			printf("■");
			if(map[i][j]==2) 
			printf("♀"); 
			if(map[i][j]==3)
			printf("◇"); 
			if(map[i][j]==4)
			printf("◎"); 
			if(map[i][j]==6)
			printf("♂"); 
			if(map[i][j]==7)
			printf("◆");
		}
		printf("\n");
	}

	for(i=0;i<9;i++)
	{
		for(j=0;j<11;j++)
		{
			if(map[i][j]==2||map[i][j]==6)
			{
				a=i;
				b=j;
			}
		}
	 } 
	 int k=1;
	 c=getch();
	 if(c=='p')
	 	return 2;
	 switch(c)
	 {
	 	case 'w':
	 	case 'W':
	 	case 72:
			if(map[a-1][b]==0||map[a-1][b]==4)
		 	{
		 		map[a-1][b]+=2;
		 		map[a][b]-=2;
			 }
			 if(map[a-1][b]==3||map[a-1][b]==7)
			 {
			 	if(map[a-2][b]==0||map[a-2][b]==4)
			 	{
			 		map[a][b]-=2;
			 		map[a-1][b]-=1;
			 		map[a-2][b]+=3;
				 }
			 }
			break;
		case 's' :
		case 'S':
		case 80:
			if(map[a+1][b]==0||map[a+1][b]==4)
			{
				map[a+1][b]+=2;
				map[a][b]-=2;
			}
			if(map[a+1][b]==3||map[a+1][b]==7)
			{
				if(map[a+2][b]==0||map[a+2][b]==4)
				{
					map[a+1][b]-=1;
					map[a][b]-=2;
					map[a+2][b]+=3;
				}
			}
			break;
		case 'a':
		case 'A':
		case 75:
			if(map[a][b-1]==0||map[a][b-1]==4)
			{
				map[a][b]-=2;
				map[a][b-1]+=2;
			}
			if(map[a][b-1]==3||map[a][b-1]==7)
			{
				if(map[a][b-2]==0||map[a][b-2]==4)
				{
					map[a][b]-=2;
					map[a][b-1]-=1;
					map[a][b-2]+=3;
				}
			}
			break;
		case 'd':
		case 'D':
		case 77 :
			if(map[a][b+1]==0||map[a][b+1]==4)
			{
				map[a][b]-=2;
				map[a][b+1]+=2;
			}
			if(map[a][b+1]==3||map[a][b+1]==7)
			{
				if(map[a][b+2]==0||map[a][b+2]==4)
				{
					map[a][b]-=2;
					map[a][b+1]-=1;
					map[a][b+2]+=3;
				}
			}
			break;
		//case'p': k=2;  break; 
	 }
	printf(" ");
	 //system("pause");
	 //if(k==2)
	 return k;
}
int main()
{
	printf("***********推箱子游戏欢迎你***********\n");
	printf("*                                    *\n");
	printf("*       ♀:自己的位置                *\n");
	printf("*       ◇:箱子的位置                *\n");
	printf("*       ◎:终点的位置                *\n");
	printf("*          游戏规则                  *\n");
	printf("*     控制♀将◇推到◎的位置         *\n");
	printf("*     按“w,s,a,d”或方向键控制移动  *\n");
	printf("*     按“p”退出游戏                *\n");
	printf("**************************************\n");
	system("pause");
	int k=1;
	while(1) 
	{
		
		system("cls");
		k=map0();
		if(k==0)
		{
			break;
		}
		if(k==2)
		return 0;
	}
	while(1)
	{
		system("cls");
		k=map1();
		if(k==0)
		break;
		if(k==2)
		return 0;
	}
	while(1)
	{
		system("cls");
		k=map2();
		if(k==0)
		break;
		if(k==2)
		return 0;
	}
	while(1)
	{
		system("cls");
		k=map3();
		if(k==0)
		break;
		if(k==2)
		return 0;
	}
	while(1)
	{
		system("cls");
		k=map4();
		if(k==0)
		break;
		if(k==2)
		return 0;
	}
}

实例五

#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<Windows.h>
using namespace std;
int mapsecond[11][13]= {
	{0,0,0,1,1,1,1,1,1,1,0,0,0},
	{1,1,1,1,0,0,0,0,0,1,0,0,0},
	{1,0,0,0,3,1,1,1,0,1,0,0,0},
	{1,0,1,0,1,0,0,0,0,1,1,0,0},
	{1,0,1,0,4,0,4,1,3,0,1,0,0},
	{1,0,1,0,0,1,0,0,1,0,1,0,0},
	{1,0,3,1,4,0,4,0,1,0,1,0,0},
	{1,1,0,0,0,0,1,0,1,0,1,1,1},
	{0,1,0,1,1,1,3,0,0,0,5,0,1},
	{0,1,0,0,0,0,0,1,1,0,0,0,1},
	{0,1,1,1,1,1,1,1,1,1,1,1,1},
};
int mapfourth[9][11] = {
	{0,1,1,1,1,1,1,1,1,1,0},  
	{0,1,0,0,0,1,0,0,0,1,0},  
	{0,1,0,4,4,4,4,4,0,1,0},  
	{0,1,0,4,0,4,0,4,0,1,1},  
	{0,1,0,0,0,0,0,0,4,0,1},  
	{1,1,0,1,1,1,1,0,4,0,1},
	{1,0,8,3,3,3,3,1,0,0,1},
	{1,0,3,3,3,3,3,0,0,1,1},
	{1,1,1,1,1,1,1,1,1,1,0},
};
int mapthird[7][8]= {
	{0,1,1,1,1,1,1,0},
	{0,1,0,0,0,0,1,1},
	{1,1,3,1,1,4,0,1},
	{1,0,3,3,4,0,0,1},
	{1,0,0,1,4,0,0,1},
	{1,0,0,5,0,1,1,1},
	{1,1,1,1,1,1,0,0},	
};
int mapfirst[7][8]= {
	{1,1,1,1,1,1,1,1},
	{1,0,0,0,0,0,0,1},
	{1,0,1,1,0,1,0,1},
	{1,0,3,4,0,4,3,1},
	{1,0,1,1,0,1,0,1},
	{1,0,0,0,5,0,0,1},
	{1,1,1,1,1,1,1,1},
};
void drawGamethird() {
	for(int a=0; a<7;a++)         //0:空地 1: ■表示墙 3:☆ 目的地 4:★箱子  5.※人 7.⊙表示箱子推到了目的的显示
		                          //8:※人到达目的地,显示人
	{
		for(int k=0; k<8; k++) {
			switch(mapthird[a][k]) {
				case 0:cout<<"  ";break;
				case 1:cout<<"■";break;
				case 3:cout<<"☆";break;
				case 4:cout<<"★";break;
				case 5:cout<<"※";break;
				case 7:cout<<"⊙";break;
				case 8:cout<<"※";break;
			}
		}
		cout<<endl;
	}
}
void drawGamesecond() {
	for(int b=0; b<11;b++)
	{
		for(int z=0; z<13; z++) {
			switch(mapsecond[b][z]) {
				case 0:cout<<"  ";break;
				case 1:cout<<"■";break;
				case 3:cout<<"☆";break;
				case 4:cout<<"★";break;
				case 5:cout<<"※";break;
				case 7:cout<<"⊙";break;
				case 8:cout<<"※";break;
			}
		}
		cout<<endl;
	}
}
void drawGamefourth() { 
	for(int c=0; c<9;c++)
	{
		for(int d=0; d<11; d++) {
			switch(mapfourth[c][d]) {
				case 0:cout<<"  ";break;
				case 1:cout<<"■";break;
				case 3:cout<<"☆";break;
				case 4:cout<<"★";break;
				case 5:cout<<"※";break;
				case 7:cout<<"⊙";break;
				case 8:cout<<"※";break;
			}
		}
		cout<<endl;
	}
}
void drawGamefirst() {
	for(int e=0; e<7; e++)
	{
		for(int f=0; f<8; f++) {
			switch(mapfirst[e][f]) {case 0:cout<<"  ";break;
				case 1:cout<<"■";break;
				case 3:cout<<"☆";break;
				case 4:cout<<"★";break;
				case 5:cout<<"※";break;
				case 7:cout<<"⊙";break;
				case 8:cout<<"※";break;
			}
		}
		cout<<endl;
	}
}
void keydownfourth() { 
      int i,j;
	for(int a=0; a<9; a++) {
		for(int k=0; k<11; k++) {
			if(mapfourth[a][k]==5||mapfourth[a][k]==8) {
				i=a;
				j=k;
			}
		}
	}
	char ch=getch();//2.找到开始移动
	//getch()  直接接收 不显示回文   getchar() 接受键盘字符,显示回文可以修改 enter键结束
	switch(ch) {
			//1.小键盘
			//上:72 下:80  左:75 右:77
			// 2.移动的原理
			//0:空地 1: ■表示墙 3:☆ 目的地 4:★:箱子  5.※人 7.⊙表示箱子推到了目的的显示
			//8:※人到达目的地,显示人
		case 'W':
		case 'w'://二维数组具有行和列的 ,首先要判断(选择结构)能走,然后在走, 上走 列不变行变
		case  72:
			if(mapfourth[i-1][j]==0||mapfourth[i-1][j]==3) { //空格或者目的地
				mapfourth[i][j]=mapfourth[i][j]-5;//人走了
				mapfourth[i-1][j]=mapfourth[i-1][j]+5; //人来了
			} else if(mapfourth[i-1][j]==4
			          ||mapfourth[i-1][j]==7) { //人上面是不是箱子 或者箱子到达了目的地
				if(mapfourth[i-2][j]==0||mapfourth[i-2][j]==3) { //箱子上面是不是目的地或空地
					mapfourth[i-2][j]=mapfourth[i-2][j]+4;
					mapfourth[i-1][j]=mapfourth[i-1][j]+1;
					mapfourth[i][j]=mapfourth[i][j]-5;
				}
			}
			break;
		case 's':
		case 'S':
		case  80:
			if(mapfourth[i+1][j]==0||mapfourth[i+1][j]==3) { //空格或者目的地
				mapfourth[i][j]=mapfourth[i][j]-5;//人走了
				mapfourth[i+1][j]=mapfourth[i+1][j]+5; //人来了
			} else if(mapfourth[i+1][j]==4
			          ||mapfourth[i+1][j]==7) { //人上面是不是箱子 或者箱子到达了目的地
				if(mapfourth[i+2][j]==0||mapfourth[i+2][j]==3) { //箱子上面是不是目的地或空地
					mapfourth[i+2][j]=mapfourth[i+2][j]+4;
					mapfourth[i+1][j]=mapfourth[i+1][j]+1;
					mapfourth[i][j]=mapfourth[i][j]-5;
				}
			}
			break;
		case 'a':
		case 'A':
		case 75:
			if(mapfourth[i][j-1]==0||mapfourth[i][j-1]==3) { //空格或者目的地
				mapfourth[i][j]=mapfourth[i][j]-5;//人走了
				mapfourth[i][j-1]=mapfourth[i][j-1]+5; //人来了
			} else if(mapfourth[i][j-1]==4
			          ||mapfourth[i][j-1]==7) { //人上面是不是箱子 或者箱子到达了目的地
				if(mapfourth[i][j-2]==0||mapfourth[i][j-2]==3) { //箱子上面是不是目的地或空地
					mapfourth[i][j-2]=mapfourth[i][j-2]+4;
					mapfourth[i][j-1]=mapfourth[i][j-1]+1;
					mapfourth[i][j]=mapfourth[i][j]-5;
				}
			}
			break;
		case 'D':
		case 'd':
		case 77:
			if(mapfourth[i][j+1]==0||mapfourth[i][j+1]==3) { //空格或者目的地
				mapfourth[i][j]=mapfourth[i][j]-5;//人走了
				mapfourth[i][j+1]=mapfourth[i][j+1]+5; //人来了
			} else if(mapfourth[i][j+1]==4
			          ||mapfourth[i][j+1]==7) { //人上面是不是箱子 或者箱子到达了目的地
				if(mapfourth[i][j+2]==0||mapfourth[i][j+2]==3) { //箱子上面是不是目的地或空地
					mapfourth[i][j+2]=mapfourth[i][j+2]+4;
					mapfourth[i][j+1]=mapfourth[i][j+1]+1;
					mapfourth[i][j]=mapfourth[i][j]-5;
				}
			}
			break;
	}
}
void keydownsecond() { //按键处理
	int i,j;
	for(int b=0; b<11; b++) {
		for(int z=0; z<13; z++) {
			if(mapsecond[b][z]==5||mapsecond[b][z]==8) {
				i=b;
				j=z;
			}
		}
	}
	//2.找到开始移动
	char ch=getch();
	//getch()  直接接收 不显示回文   getchar() 接受键盘字符,显示回文可以修改 enter键结束
	switch(ch) {
			//1.小键盘
			//上:72 下:80  左:75 右:77
			// 2.移动的原理
			//0:空地 1: ■表示墙 3:☆ 目的地 4:★:箱子  5.※人 7.⊙表示箱子推到了目的的显示
			//8:※人到达目的地,显示人
		case 'W':
		case 'w'://二维数组具有行和列的 ,首先要判断(选择结构)能走,然后在走, 上走 列不变行变
		case  72:
			if(mapsecond[i-1][j]==0||mapsecond[i-1][j]==3) { //空格或者目的地
				mapsecond[i][j]=mapsecond[i][j]-5;//人走了
				mapsecond[i-1][j]=mapsecond[i-1][j]+5; //人来了
			} else if(mapsecond[i-1][j]==4||mapsecond[i-1][j]==7) { //人上面是不是箱子 或者箱子到达了目的地
				if(mapsecond[i-2][j]==0||mapsecond[i-2][j]==3) { //箱子上面是不是目的地或空地
					mapsecond[i-2][j]=mapsecond[i-2][j]+4;
					mapsecond[i-1][j]=mapsecond[i-1][j]+1;
					mapsecond[i][j]=mapsecond[i][j]-5;
				}
			}
			break;
		case 's':
		case 'S':
		case  80:
			if(mapsecond[i+1][j]==0||mapsecond[i+1][j]==3) { //空格或者目的地
				mapsecond[i][j]=mapsecond[i][j]-5;//人走了
				mapsecond[i+1][j]=mapsecond[i+1][j]+5; //人来了
			} else if(mapsecond[i+1][j]==4
			          ||mapsecond[i+1][j]==7) { //人上面是不是箱子 或者箱子到达了目的地
				if(mapsecond[i+2][j]==0||mapsecond[i+2][j]==3) { //箱子上面是不是目的地或空地
					mapsecond[i+2][j]=mapsecond[i+2][j]+4;
					mapsecond[i+1][j]=mapsecond[i+1][j]+1;
					mapsecond[i][j]=mapsecond[i][j]-5;
				}
			}
			break;
		case 'a':
		case 'A':
		case 75:
			if(mapsecond[i][j-1]==0||mapsecond[i][j-1]==3) { //空格或者目的地
				mapsecond[i][j]=mapsecond[i][j]-5;//人走了
				mapsecond[i][j-1]=mapsecond[i][j-1]+5; //人来了
			} else if(mapsecond[i][j-1]==4
			          ||mapsecond[i][j-1]==7) { //人上面是不是箱子 或者箱子到达了目的地
				if(mapsecond[i][j-2]==0||mapsecond[i][j-2]==3) { //箱子上面是不是目的地或空地
					mapsecond[i][j-2]=mapsecond[i][j-2]+4;
					mapsecond[i][j-1]=mapsecond[i][j-1]+1;
					mapsecond[i][j]=mapsecond[i][j]-5;
				}
			}
			break;
		case 'D':
		case 'd':
		case 77:
			if(mapsecond[i][j+1]==0||mapsecond[i][j+1]==3) { //空格或者目的地
				mapsecond[i][j]=mapsecond[i][j]-5;//人走了
				mapsecond[i][j+1]=mapsecond[i][j+1]+5; //人来了
			} else if(mapsecond[i][j+1]==4
			          ||mapsecond[i][j+1]==7) { //人上面是不是箱子 或者箱子到达了目的地
				if(mapsecond[i][j+2]==0||mapsecond[i][j+2]==3) { //箱子上面是不是目的地或空地
					mapsecond[i][j+2]=mapsecond[i][j+2]+4;
					mapsecond[i][j+1]=mapsecond[i][j+1]+1;
					mapsecond[i][j]=mapsecond[i][j]-5;
				}
			}
			break;
	}
}
void keydownthird() { //按键处理
	int i,j;
	for(int c=0; c<7; c++) {
		for(int d=0; d<8; d++) {
			if(mapthird[c][d]==5||mapthird[c][d]==8) {
				i=c;
				j=d;
			}
		}
	}
	char ch=getch();
	switch(ch) {
		case 'W':
		case 'w':
		case  72:
			if(mapthird[i-1][j]==0||mapthird[i-1][j]==3) { 
				mapthird[i][j]=mapthird[i][j]-5;
				mapthird[i-1][j]=mapthird[i-1][j]+5; 
			} else if(mapthird[i-1][j]==4
			          ||mapthird[i-1][j]==7) { 
				if(mapthird[i-2][j]==0||mapthird[i-2][j]==3) { 
					mapthird[i-2][j]=mapthird[i-2][j]+4;
					mapthird[i-1][j]=mapthird[i-1][j]+1;
					mapthird[i][j]=mapthird[i][j]-5;
				}
			}
			break;
		case 's':
		case 'S':
		case  80:
			if(mapthird[i+1][j]==0||mapthird[i+1][j]==3) { 
				mapthird[i][j]=mapthird[i][j]-5;
				mapthird[i+1][j]=mapthird[i+1][j]+5; 
			} else if(mapthird[i+1][j]==4
			          ||mapthird[i+1][j]==7) { 
				if(mapthird[i+2][j]==0||mapthird[i+2][j]==3) { 
					mapthird[i+2][j]=mapthird[i+2][j]+4;
					mapthird[i+1][j]=mapthird[i+1][j]+1;
					mapthird[i][j]=mapthird[i][j]-5;
				}
			}
			break;
		case 'a':
		case 'A':
		case 75:
			if(mapthird[i][j-1]==0||mapthird[i][j-1]==3) { 
				mapthird[i][j]=mapthird[i][j]-5;
				mapthird[i][j-1]=mapthird[i][j-1]+5; 
			} else if(mapthird[i][j-1]==4
			          ||mapthird[i][j-1]==7) { 
				if(mapthird[i][j-2]==0||mapthird[i][j-2]==3) { 
					mapthird[i][j-2]=mapthird[i][j-2]+4;
					mapthird[i][j-1]=mapthird[i][j-1]+1;
					mapthird[i][j]=mapthird[i][j]-5;
				}
			}
			break;
		case 'D':
		case 'd':
		case 77:
			if(mapthird[i][j+1]==0||mapthird[i][j+1]==3) {
				mapthird[i][j]=mapthird[i][j]-5;
				mapthird[i][j+1]=mapthird[i][j+1]+5;
			} else if(mapthird[i][j+1]==4
			          ||mapthird[i][j+1]==7) {
				if(mapthird[i][j+2]==0||mapthird[i][j+2]==3) { 
					mapthird[i][j+2]=mapthird[i][j+2]+4;
					mapthird[i][j+1]=mapthird[i][j+1]+1;
					mapthird[i][j]=mapthird[i][j]-5;
				}
			}
			break;
	}
}
void keydownfirst() { 
	int i,j;
	for(int e=0; e<7; e++) {
		for(int f=0; f<8; f++) {
			if(mapfirst[e][f]==5||mapfirst[e][f]==8) {
				i=e;
				j=f;
			}
		}
	}
	
	char ch=getch();
	switch(ch) {
		case 'W':
		case 'w':
		case  72:
			if(mapfirst[i-1][j]==0||mapfirst[i-1][j]==3) { 
				mapfirst[i][j]=mapfirst[i][j]-5;
				mapfirst[i-1][j]=mapfirst[i-1][j]+5; 
			} else if(mapfirst[i-1][j]==4
			          ||mapfirst[i-1][j]==7) { 
				if(mapfirst[i-2][j]==0||mapfirst[i-2][j]==3) { 
					mapfirst[i-2][j]=mapfirst[i-2][j]+4;
					mapfirst[i-1][j]=mapfirst[i-1][j]+1;
					mapfirst[i][j]=mapfirst[i][j]-5;
				}
			}
			break;
		case 's':
		case 'S':
		case  80:
			if(mapfirst[i+1][j]==0||mapfirst[i+1][j]==3) { 
				mapfirst[i][j]=mapfirst[i][j]-5;
				mapfirst[i+1][j]=mapfirst[i+1][j]+5; 
			} else if(mapfirst[i+1][j]==4
			          ||mapfirst[i+1][j]==7) { 
				if(mapfirst[i+2][j]==0||mapfirst[i+2][j]==3) { 
					mapfirst[i+2][j]=mapfirst[i+2][j]+4;
					mapfirst[i+1][j]=mapfirst[i+1][j]+1;
					mapfirst[i][j]=mapfirst[i][j]-5;
				}
			}
			break;
		case 'a':
		case 'A':
		case 75:
			if(mapfirst[i][j-1]==0||mapfirst[i][j-1]==3) { 
				mapfirst[i][j]=mapfirst[i][j]-5;
				mapfirst[i][j-1]=mapfirst[i][j-1]+5; 
			} else if(mapfirst[i][j-1]==4
			          ||mapfirst[i][j-1]==7) { 
				if(mapfirst[i][j-2]==0||mapfirst[i][j-2]==3) { 
					mapfirst[i][j-2]=mapfirst[i][j-2]+4;
					mapfirst[i][j-1]=mapfirst[i][j-1]+1;
					mapfirst[i][j]=mapfirst[i][j]-5;
				}
			}
			break;
		case 'D':
		case 'd':
		case 77:
			if(mapfirst[i][j+1]==0||mapfirst[i][j+1]==3) { 
				mapfirst[i][j]=mapfirst[i][j]-5;
				mapfirst[i][j+1]=mapfirst[i][j+1]+5; 
			} else if(mapfirst[i][j+1]==4
			          ||mapfirst[i][j+1]==7) { 
				if(mapfirst[i][j+2]==0||mapfirst[i][j+2]==3) { 
					mapfirst[i][j+2]=mapfirst[i][j+2]+4;
					mapfirst[i][j+1]=mapfirst[i][j+1]+1;
					mapfirst[i][j]=mapfirst[i][j]-5;
				}
			}
			break;
	}
}
void gameoverfourth ()
{
	int k=0;
	for(int i=0;i<9;i++)
	{
	for(int j=0;j<11;j++)
	{
	  if(mapfourth[i][j]==4)
	   k++;
	}
    }
	if(k==0)
	{
	 cout<<"恭喜你,你赢了!"<<endl;
	 exit(0);
    }
}
void gameoverthird()
{
	int k=0;
	for(int i=0;i<7;i++)
	{
	for(int j=0;j<8;j++){
	 	if(mapthird[i][j]==4)
	 	 k++;}
     }    
	 if(k==0) {	
	 cout<<"恭喜你,顺利进入下一关"<<endl;
	 system("cls");
	 while(1){
	  system("cls");
	  drawGamefourth();
	  cout<<"当前关卡第2关"<<endl;
	  keydownfourth();
	  gameoverfourth();
	  }
    }
}
void gameoversecond()
{
	int k=0;
	for(int i=0;i<11;i++)
	{
	for(int j=0;j<13;j++)
	{
	  if(mapsecond[i][j]==4)
	   k++;
	}
    }
	if(k==0)
	{
	 cout<<"恭喜你,你通过了此关!"<<endl;
	 Sleep(1000);
	 while(1)
	 {
	system("cls");
	drawGamethird();
	cout<<"当前关卡第3关"<<endl;
	keydownthird();
	gameoverthird();
    }
    }
}
void gameoverfirst()
{
	int k=0;
	for(int i=0; i<7; i++) {
		for (int j=0; j<8; j++) {
			if(mapfirst[i][j]==4)
				k++;
		}
	}
	while(k==0){
		printf("恭喜你,你通过过了此关!\n");
		Sleep(1000);
	     while(1) 
			   { 
			    system("cls");
				drawGamesecond();
				cout<<"当前关卡第2关"<<endl;
				keydownsecond();
				gameoversecond();}
		}	        
}
int main() {
	while(1){ 
	system("color 3F ");
	cout<<"———————请选关————————"<<endl;
	cout<<"          1. 第一关                " <<endl;
	cout<<"          2. 第二关                " <<endl;
	cout<<"          3. 第三关                " <<endl;
	cout<<"          4. 最后一关              "<<endl;
	cout<<"          5.退出游戏               " <<endl;
	cout<<"提示:每通关一次你都会进入下一关"<<endl;
	cout<<"注意:0:空地 1: ■表示墙 3:☆ 目的地 4:★箱子  5.※人 7.⊙表示箱子推到了目的的显示 8:※人到达目的地"<<endl;
       int n;
	   cin>>n; 
	      if(n==1){
	      	  while(1){
                system("cls");
				drawGamefirst();
				cout<<"  当前关卡第[1]关"<<endl; 
				keydownfirst();
				gameoverfirst();}
		  }
	     if(n==2){ 
		    while(1) { 
			    system("cls");
				drawGamesecond(); 
				cout<<"  当前关卡第[2]关"<<endl;
				keydownsecond();
				gameoversecond();}
			 }
	   if(n==3){
	     while(1) 
		 {      system("cls");
				drawGamethird();
				cout<<"  当前关卡第[3]关"<<endl; 
			    keydownthird();
			    gameoverthird(); }
		}
	 if(n==4)
	 {
	   while(1) 
	   {        system("cls");
				drawGamefourth();
				cout<<"  当前关卡为最后一关"<<endl; 
				keydownfourth();
				gameoverfourth();
			 }
		}
	if(n==5)
	system("cls");
    cout<<"已经退出游戏"<<endl;exit(-1);
}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值