c++推箱子
#include<iostream>
#include<conio.h>
#include<string.h>
#include<Windows.h>
using namespace std;
const int mapsize = 20;
int xx = 0, yy = 0;
int win = 0;
int curIndex = 0;
bool isMove = false;
int backId = 0;
int backCh;
int thereBack[3] = { 0 };
char thereBackCh[3];
int Steps = 0;
enum {
ROAD = 0,
WALL = 1,
HUMAN = 2,
BOX = 3,
TARGET = 4,
VRI = 7
};
int map[5][mapsize][mapsize] = {
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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 3, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0,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, 0, 0, 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 copyMap[mapsize][mapsize];
void printMap(){
win = 0;
for (int i = 0; i < mapsize; i++){
for (int j = 0; j < mapsize; j++){
switch (map[curIndex][i][j]){
case ROAD:
cout << " ";
break;
case WALL:
cout << "■";
break;
case HUMAN:
case 6:
cout << "♂";
xx = i;
yy = j;
break;
case BOX:
cout << "○";
win++;
break;
case TARGET:
cout << "☆";
break;
case VRI:
cout << "★";
break;
}
}
cout << endl;
}
}
void Hmove(int x, int y){
int nextX = xx + x;
int nextY = yy + y;
int nextnextX = xx + x * 2;
int nextnextY = yy + y * 2;
if (ROAD == map[curIndex][nextX][nextY] || TARGET == map[curIndex][nextX][nextY]){
map[curIndex][xx][yy] -= HUMAN;
map[curIndex][nextX][nextY] += HUMAN;
backId = 1;
}
else if (BOX == map[curIndex][nextX][nextY] || VRI == map[curIndex][nextX][nextY]){
if (ROAD == map[curIndex][nextnextX][nextnextY] || TARGET == map[curIndex][nextnextX][nextnextY]){
map[curIndex][xx][yy] -= HUMAN;
map[curIndex][nextX][nextY] -= WALL;
map[curIndex][nextnextX][nextnextY] += BOX;
backId = 2;
}
}
thereBack[Steps++ % (sizeof(thereBack) / sizeof(int))] = backId;
isMove = true;
}
void rBack(char ch,int bcak_id){
int x = 0;
int y = 0;
switch (ch){
case 'w':
x = 1;
break;
case 'a':
y = 1;
break;
case 's':
x = -1;
break;
case 'd':
y = -1;
break;
}
int nextX = xx + x;
int nextY = yy + y;
int beforeX = xx - x;
int beforeY = yy - y;
if (1 == bcak_id){
map[curIndex][xx][yy] -= HUMAN;
map[curIndex][nextX][nextY] += HUMAN;
}
else if (2 == bcak_id){
map[curIndex][xx][yy] += WALL;
map[curIndex][nextX][nextY] += HUMAN;
map[curIndex][beforeX][beforeY] -= BOX;
}
}
void rBackThereSteps(int index){
rBack(thereBackCh[index], thereBack[index]);
system("cls");
printMap();
}
void keyboard(){
char ch = getch();
if (ch >= 'A' && ch <= 'Z'){
ch += 32;
}
switch (ch){
case 'w':
Hmove(-1, 0);
backCh = ch;
break;
case 'a':
Hmove(0, -1);
backCh = ch;
break;
case 's':
Hmove(1, 0);
backCh = ch;
break;
case 'd':
Hmove(0, 1);
backCh = ch;
break;
case 'r':
isMove = false;
system("cls");
memcpy(map[curIndex],copyMap, sizeof(copyMap));
printMap();
break;
case 'b':
if (0 != memcmp(map[curIndex], copyMap, sizeof(copyMap))&&isMove){
rBack(backCh,backId);
}
isMove = false;
break;
case'p':
if (0 != memcmp(map[curIndex], copyMap, sizeof(copyMap)) && 0 != thereBack[2]&&isMove){
if (0 == (Steps - 1) % (sizeof(thereBack) / sizeof(int))){
rBackThereSteps(0);
rBackThereSteps(2);
rBackThereSteps(1);
}
else if (1 == (Steps - 1) % (sizeof(thereBack) / sizeof(int))){
rBackThereSteps(1);
rBackThereSteps(0);
rBackThereSteps(2);
}
else if (2 == (Steps - 1) % (sizeof(thereBack) / sizeof(int))){
rBackThereSteps(2);
rBackThereSteps(1);
rBackThereSteps(0);
}
}
isMove = false;
break;
}
thereBackCh[(Steps - 1) % (sizeof(thereBack) / sizeof(int))] = backCh;
}
int main(void) {
memcpy(copyMap, map[curIndex], sizeof(map[curIndex]));
while (true){
system("cls");
cout << " 按R键重置本关 "<< endl;
printMap();
cout << "关卡" << curIndex + 1 << " 共" << sizeof(map) / sizeof(map[0] )<<"关"<< " 剩余箱子数:" << win << endl;
cout << "移动: wasd键 " << endl;
cout << "回退一步: b键 " << endl;
cout << "回退三步: p键 " << endl;
cout << "人物: ♂" << endl;
cout << "箱子: ○" << endl;
cout << "目标点: ☆" << endl;
if (0 == win){
cout << endl;
cout << " victory!!!!! \n" << endl;
curIndex++;
memcpy(copyMap, map[curIndex], sizeof(map[curIndex]));
if (curIndex >= sizeof(map)/sizeof(map[0])){
system("cls");
cout << "恭喜你通过所有关卡!!" << endl;
break;
}
cout << "按任意键进入下一关" << endl;
getch();
continue;
}
keyboard();
}
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/18e2146ff0448e1ec80e2308ceb5e1cd.png)