#include <stdio.h>
#include <graphics.h>
#include <Windows.h>
#include <stdlib.h>
#include <conio.h>
#define OEN 1
#define TWO 2
//地图
//0:空地 1:墙壁 3:箱子的目的地 4:箱子
//6:人 7:箱子和目的的重合 9:人站在目的地
int g_map[2][10][12] =
{
{
{1,1,1,1,1,0,0,0,0,0,0,0},
{1,0,0,0,1,0,1,1,1,0,0,0},
{1,0,4,0,1,0,1,1,1,1,1,1},
{1,0,4,6,1,0,1,0,0,0,3,1},
{1,1,1,4,1,1,1,0,0,0,3,1},
{1,1,1,0,0,0,0,0,0,0,3,1},
{0,1,0,0,0,1,0,0,0,0,0,1},
{0,1,0,0,0,1,0,0,0,0,0,1},
{0,1,0,0,0,1,1,1,1,1,1,1},
{0,1,1,1,1,1,0,0,0,0,0,0},
},
{
{0,1,1,1,1,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,0,0,0},
{0,1,0,0,0,0,1,1,1,1,1,1},
{1,1,4,1,1,0,1,1,0,0,0,1},
{1,0,6,0,1,0,0,1,0,0,0,1},
{1,0,0,1,1,1,0,0,0,0,0,1},
{1,0,0,0,4,0,0,4,0,0,0,1},
{1,0,3,3,1,0,4,0,0,0,1,1},
{1,1,3,3,1,0,0,0,1,1,1,0},
{0,1,1,1,1,1,1,1,1,1,0,0},
},
};
int g_nCurrentLevel = 0;
void DrapMap(); //打印地图
POINT GetGamerPosition(); //获取玩家坐标
void Up(); //往上
void Down(); //往下
void Lefr(); //往左
void Right(); //往右
int GetSpareBox(); //查看是否有箱子
void SetColor(int nColor); //颜色
int main(void)
{
//设置标题
SetConsoleTitle(("推箱子"));
//设置窗口大小
//system("mode con cols=26 lines=12");
while(1)
{
if(GetSpareBox() == 0)
{
g_nCurrentLevel++;
}
//清屏
system("cls");
//打印地图
DrapMap();
char ch = getch();
switch (ch)
{
case 'w':case 72: //往上
Up();
break;
case 's':case 80: //往下
Down();
break;
case 'a':case 75: //往左
Lefr();
break;
case 'd':case 77: //往右
Right();
break;
}
}
system("pause");
return 0;
}
void DrapMap()
{
for (int i = 0; i < 10; i++)
{
for(int j = 0; j < 12; j++)
{
switch (g_map[g_nCurrentLevel][i][j])
{
case 0:
printf(" ");
break;
case 1:
SetColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
printf("■");
break;
case 3:
SetColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf("☆");
break;
case 4:
SetColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
printf("□");
break;
case 6:
SetColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf("♀");
break;
case 7:
printf("★");
break;
case 9:
SetColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf("♀");
break;
}
}
printf("\n");
}
}
//获取玩家位置
POINT GetGamerPosition()
{
POINT pos = {-OEN, -OEN};
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 12; j++)
{
if(g_map[g_nCurrentLevel][i][j] == 6 || g_map[g_nCurrentLevel][i][j] == 9)
{
pos.x = i;
pos.y = j;
return pos;
}
}
}
return pos;
}
//往上
void Up()
{
POINT pos = GetGamerPosition();
//1.人的前面是空地
if(g_map[g_nCurrentLevel][pos.x - 1][pos.y] == 0)
{
g_map[g_nCurrentLevel][pos.x -1][pos.y] = 6; //空地改为人
//还原人的位置还原
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
//2.人的前面是目的地
if(g_map[g_nCurrentLevel][pos.x - 1][pos.y] == 3)
{
g_map[g_nCurrentLevel][pos.x - 1][pos.y] = 9; //目的地改为人
//还原人的位置还原
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
//3.人的前面是箱子
if(g_map[g_nCurrentLevel][pos.x - 1][pos.y] == 4)
{
//a。箱子的前面是空地
if(g_map[g_nCurrentLevel][pos.x - 2][pos.y] == 0)
{
g_map[g_nCurrentLevel][pos.x - 2][pos.y] = 4;
g_map[g_nCurrentLevel][pos.x -1][pos.y] = 6;
//还原人的位置还原
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
//b。箱子的前面是目的地
if(g_map[g_nCurrentLevel][pos.x - 1][pos.y] == 3)
{
g_map[g_nCurrentLevel][pos.x - 2][pos.y] = 7;
g_map[g_nCurrentLevel][pos.x - 1][pos.y] = 9;
//还原人的位置还原
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
}
//4.人的前面是箱子和目的地的重合
if(g_map[g_nCurrentLevel][pos.x - 1][pos.y] == 7)
{
//a。箱子的前面是空地
if(g_map[g_nCurrentLevel][pos.x - 2][pos.y] == 0)
{
g_map[g_nCurrentLevel][pos.x - 2][pos.y] = 4;
g_map[g_nCurrentLevel][pos.x - 1][pos.y] = 9;
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
//b箱子的前面是目的地
if(g_map[g_nCurrentLevel][pos.x - 2][pos.y] == 3)
{
g_map[g_nCurrentLevel][pos.x - 2][pos.y] = 7;
g_map[g_nCurrentLevel][pos.x - 1][pos.y] = 9;
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
}
}
//往下
void Down()
{
POINT pos = GetGamerPosition();
//1.人的前面是空地
if(g_map[g_nCurrentLevel][pos.x + 1][pos.y] == 0)
{
g_map[g_nCurrentLevel][pos.x + 1][pos.y] = 6; //空地改为人
//还原人的位置还原
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
//2.人的前面是目的地
if(g_map[g_nCurrentLevel][pos.x +1][pos.y] == 3)
{
g_map[g_nCurrentLevel][pos.x + 1][pos.y] = 9; //目的地改为人
//还原人的位置还原
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
//3.人的前面是箱子
if(g_map[g_nCurrentLevel][pos.x + 1][pos.y] == 4)
{
//a。箱子的前面是空地
if(g_map[g_nCurrentLevel][pos.x + 2][pos.y] == 0)
{
g_map[g_nCurrentLevel][pos.x + 2][pos.y] = 4;
g_map[g_nCurrentLevel][pos.x + 1][pos.y] = 6;
//还原人的位置还原
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
//b。箱子的前面是目的地
if(g_map[g_nCurrentLevel][pos.x + 2][pos.y] == 3)
{
g_map[g_nCurrentLevel][pos.x + 2][pos.y] = 7;
g_map[g_nCurrentLevel][pos.x + 1][pos.y] = 6;
//还原人的位置还原
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
}
//4.人的前面是箱子和目的地的重合
if(g_map[g_nCurrentLevel][pos.x + 1][pos.y] == 7)
{
//a。箱子和目的地的重合前面是空地
if(g_map[g_nCurrentLevel][pos.x + 2][pos.y] == 0)
{
g_map[g_nCurrentLevel][pos.x + 2][pos.y] = 4;
g_map[g_nCurrentLevel][pos.x + 1][pos.y] = 9;
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
//b箱子和目的地的重合的前面是目的地
if(g_map[g_nCurrentLevel][pos.x + 2][pos.y] == 3)
{
g_map[g_nCurrentLevel][pos.x + 2][pos.y] = 7;
g_map[g_nCurrentLevel][pos.x + 1][pos.y] = 9;
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
}
}
//往左
void Lefr()
{
POINT pos = GetGamerPosition();
//1.人的前面是空地
if(g_map[g_nCurrentLevel][pos.x][pos.y - 1] == 0)
{
g_map[g_nCurrentLevel][pos.x][pos.y - 1] = 6; //空地改为人
//还原人的位置还原
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
//2.人的前面是目的地
if(g_map[g_nCurrentLevel][pos.x][pos.y - 1] == 3)
{
g_map[g_nCurrentLevel][pos.x][pos.y - 1] = 9; //目的地改为人
//还原人的位置还原
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
//3.人的前面是箱子
if(g_map[g_nCurrentLevel][pos.x][pos.y - 1] == 4)
{
//a。箱子的前面是空地
if(g_map[g_nCurrentLevel][pos.x][pos.y - 2] == 0)
{
g_map[g_nCurrentLevel][pos.x][pos.y - 2] = 4;
g_map[g_nCurrentLevel][pos.x][pos.y - 1] = 6;
//还原人的位置还原
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
//b。箱子的前面是目的地
if(g_map[g_nCurrentLevel][pos.x][pos.y - 2] == 3)
{
g_map[g_nCurrentLevel][pos.x][pos.y - 2] = 7;
g_map[g_nCurrentLevel][pos.x][pos.y - 1] = 6;
//还原人的位置还原
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
}
//4.人的前面是箱子和目的地的重合
if(g_map[g_nCurrentLevel][pos.x][pos.y - 1] == 7)
{
//a。箱子的前面是空地
if(g_map[g_nCurrentLevel][pos.x][pos.y - 2] == 0)
{
g_map[g_nCurrentLevel][pos.x][pos.y - 2] = 4;
g_map[g_nCurrentLevel][pos.x][pos.y - 1] = 9;
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
//b箱子的前面是目的地
if(g_map[g_nCurrentLevel][pos.x][pos.y - 2] == 3)
{
g_map[g_nCurrentLevel][pos.x][pos.y - 2] = 7;
g_map[g_nCurrentLevel][pos.x][pos.y - 1] = 9;
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
}
}
//往右
void Right()
{
POINT pos = GetGamerPosition();
//1.人的前面是空地
if(g_map[g_nCurrentLevel][pos.x][pos.y + 1] == 0)
{
g_map[g_nCurrentLevel][pos.x][pos.y + 1] = 6; //空地改为人
//还原人的位置还原
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
//2.人的前面是目的地
if(g_map[g_nCurrentLevel][pos.x][pos.y + 1] == 3)
{
g_map[g_nCurrentLevel][pos.x][pos.y + 1] = 9; //目的地改为人
//还原人的位置还原
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
//3.人的前面是箱子
if(g_map[g_nCurrentLevel][pos.x][pos.y + 1] == 4)
{
//a。箱子的前面是空地
if(g_map[g_nCurrentLevel][pos.x][pos.y + 2] == 0)
{
g_map[g_nCurrentLevel][pos.x][pos.y + 2] = 4;
g_map[g_nCurrentLevel][pos.x][pos.y + 1] = 6;
//还原人的位置还原
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
//b。箱子的前面是目的地
if(g_map[g_nCurrentLevel][pos.x][pos.y + 2] == 3)
{
g_map[g_nCurrentLevel][pos.x][pos.y + 2] = 7;
g_map[g_nCurrentLevel][pos.x][pos.y + 1] = 6;
//还原人的位置还原
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
}
//4.人的前面是箱子和目的地的重合
if(g_map[g_nCurrentLevel][pos.x][pos.y + 1] == 7)
{
//a。箱子的前面是空地
if(g_map[g_nCurrentLevel][pos.x][pos.y + 2] == 0)
{
g_map[g_nCurrentLevel][pos.x][pos.y + 2] = 4;
g_map[g_nCurrentLevel][pos.x][pos.y + 1] = 9;
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
//b箱子的前面是目的地
if(g_map[g_nCurrentLevel][pos.x][pos.y + 2] == 3)
{
g_map[g_nCurrentLevel][pos.x][pos.y + 2] = 7;
g_map[g_nCurrentLevel][pos.x][pos.y + 1] = 9;
if(g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
}else
{
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
}
}
//查看箱子的个数
int GetSpareBox()
{
int num = 0;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 12; j++)
{
if(g_map[g_nCurrentLevel][i][j] == 4)
{
num++;
}
}
}
return num;
}
//设置颜色
void SetColor(int nColor)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole,nColor);
}