推箱子

c++
// linux命令行推箱子游戏
#include <iostream>
#include <unistd.h>
// termios.h中有getch()不需要等待回车
#include <termios.h>
using namespace std;
// wall墙
#define wall 1
//people人
#define people 2
//box箱子
#define box 3
//destination目标点
#define destination 4
//destination_people人和目标点
#define destination_people 5
//destination_box箱子和目标点
#define destination_box 6
//地图宽度
#define width 8
// 地图高度
#define height 8
int map[width][height] = {
1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 4, 4, 4, 3, 0, 1,
1, 0, 4, 4, 4, 3, 0, 1,
1, 0, 4, 4, 3, 3, 0, 1,
1, 0, 0, 0, 0, 3, 0, 1,
1, 0, 3, 3, 3, 0, 0, 1,
1, 0, 0, 2, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1
};
// 双层循环画图
void draw()
{
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
switch (map[i][j])
{
case 0:
cout << " ";
break;
case wall:
cout << "\033[36m # \033[0m";
break;
case people:
cout << "\033[31m P \033[0m";
break;
case box:
cout << "\033[32m @ \033[0m";
break;
case destination:
cout << "\033[37m + \033[0m";
break;
case destination_people:
cout << "\033[31m X \033[0m";
break;
case destination_box:
cout << "\033[33m ! \033[0m";
break;
}
}
cout << "\n";
}
}
int getch()
{
struct termios oldt, newt;
int ch;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return ch;
}
void find_xy(int*x,int*y)
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (map[i][j] == people ||
map[i][j] == destination_people)
{
*x = i;
*y = j;
return;
}
}
}
}
void move_(int x, int y,int pos)
{
int x1,x2,y1,y2;
x1 = x;
x2 = x;
y1 = y;
y2 = y;
switch (pos)
{
case 0://向上移动
x1 = x-1;//上面的位置
x2 = x-2;
break;
case wall://向下移动
x1 = x+1;
x2 = x+2;
break;
case people://向左移动
y1 = y-1;
y2 = y-2;
break;
case box://向右移动
y1 = y+1;
y2 = y+2;
break;
}
// 人要移动的面 存在的东西
switch (map[x1][y1])
{
case 0: // 空
if (map[x][y] == destination_people)
{
map[x1][y1] = people;
map[x][y] = destination;
}
else
{
map[x1][y1] = people;
map[x][y] = 0;
}
break;
case box: // 箱子
if (map[x2][y2] == destination)
{
if (map[x][y] == destination_people)
{
map[x2][y2] = destination_box;
map[x1][y1] = people;
map[x][y] = destination;
}
else
{
map[x2][y2] = destination_box;
map[x1][y1] = people;
map[x][y] = 0;
}
}
else if (map[x2][y2] == 0)
{
if (map[x][y] == destination_people)
{
map[x2][y2] = box;
map[x1][y1] = people;
map[x][y] = destination;
}
else
{
map[x2][y2] = box;
map[x1][y1] = people;
map[x][y] = 0;
}
}
break;
case destination: // 目标点
if(map[x][y] == destination_people){
map[x1][y1] = destination_people;
map[x][y] = destination;
}else{
map[x1][y1] = destination_people;
map[x][y] = 0;
}
break;
case destination_box: // 目标点+箱子
if (map[x2][y2] == destination)
{
if (map[x][y] == destination_people)
{
map[x2][y2] = destination_box;
map[x1][y1] = destination_people;
map[x][y] = destination;
}
else
{
map[x2][y2] = destination_box;
map[x1][y1] = destination_people;
map[x][y] = 0;
}
}
else if (map[x2][y2] == 0)
{
if (map[x][y] == destination_people)
{
map[x2][y2] = box;
map[x1][y1] = destination_people;
map[x][y] = destination;
}
else
{
map[x2][y2] = box;
map[x1][y1] = destination_people;
map[x][y] = 0;
}
}
break;
}
}
// wall墙 people人 box箱子 destination目标点
// destination_people目标点+人 0空
// destination_box!箱子+目标点
void move()
{
char i = getch();
int x = 0;
int y = 0;
find_xy(&x,&y);
switch (i)
{
case 'w':
//0向上移动
move_(x, y,0);
break;
case 's':
//1向下移动
move_(x, y,1);
break;
case 'a':
//2向左移动
move_(x, y,2);
break;
case 'd':
//3向右移动
move_(x, y,3);
break;
}
}
bool isWin(){
int box_num = 0;
int destination_box_num = 0;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
if(map[i][j] == destination_box){
destination_box_num++;
box_num++;
}
if(map[i][j] == box){
box_num++;
}
}
}
if(box_num == destination_box_num){
return true;
}
return false;
}c++
int main()
{
system("clear");
while (true)
{
//判断是否已经推完箱子
if(isWin()){
break;
}
draw();//画图
move();//移动
system("clear");
}
draw();
cout << "you win\n";
return 0;
}