在处理移动的时候有太多种情况了:

1、有空位

2、在推箱子,推到了空地

3、推箱子推到了目标,

4、推目标位的箱子推到另一个目标

5、推目标位的箱子推到空地

首先记录目标位置,在每次推动之后会再绘画中自动修正目标位置的情况

修改地图之后发现不能将上述情况的5实现,还要再改。

#include<iostream>
#include<graphics.h>
#include<conio.h>
#include<vector>
using namespace std;
//0空白,1墙,2人,3箱子,4目标,5箱子与目标,6人与目标
int x, y,temp;
vector<int>goalx, goaly;
int mapp[30][40] = {0,0,0,0,0,0 ,0,0,0,0,0,0 ,0,0,0,0 ,0,0,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,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,1,2,0,0,0,0,0,0,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,1,0,3,0 ,4,0,0,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,1,1,1,0,3 ,0,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,4,1,0,0 ,0,0,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,4,3,0,0 ,1,0,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,4,0,0,0 ,3,0,1,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,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,
                    0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,
                    0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,
                    0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,
                    0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,
                    0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,
                    0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,
                    0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,
                    0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,
                    0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,
                    0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,
                    0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0
};
void scann() {
    int i, j;
    for (i = 0; i < 30; i++) {
        for (j = 0; j < 40; j++) {
            if (mapp[i][j] >= 4) {
                goalx.push_back(i);
                goaly.push_back(j);
            }
        }
    }
}
void drawmapp() {
    int i, j,k;
    for (i = 0; i < 30; i++) {
        for (j = 0; j < 40; j++) {
            for (k = 0; k < goalx.size(); k++) {
                if (i == goalx[k] && j == goaly[k]) {
                    if (mapp[i][j] == 0) {
                        mapp[i][j] = 4;
                    }
                    if (mapp[i][j] == 2) {
                        mapp[i][j] = 6;
                    }
                    if (mapp[i][j] == 3) {
                        mapp[i][j] = 5;
                    }

                }
            }
            switch (mapp[i][j]) {
            case 1:
                setlinecolor(RGB(0,0,0));
                setfillcolor(RGB(255, 255, 0));
                fillrectangle(j * 10, i * 10, j * 10 + 10, i * 10 + 10);
                break;
            case 2:
                x = i;
                y = j;
                setlinecolor(RGB(0, 0, 0));
                setfillcolor(RGB(255, 0, 0));
                fillrectangle(j * 10, i * 10, j * 10 + 10, i * 10 + 10);
                break;
            case 3:
                setlinecolor(RGB(0, 0, 0));
                setfillcolor(RGB(0, 0, 0));
                fillrectangle(j * 10, i * 10, j * 10 + 10, i * 10 + 10);
                break;
            case 4:
                setlinecolor(RGB(0, 0, 0));
                setfillcolor(RGB(0, 0, 255));
                fillrectangle(j * 10, i * 10, j * 10 + 10, i * 10 + 10);
                break;
            case 5:
                setlinecolor(RGB(0, 0, 0));
                setfillcolor(RGB(0, 255, 0));
                fillrectangle(j * 10, i * 10, j * 10 + 10, i * 10 + 10);
                break;
            case 6:
                x = i;
                y = j;
                setlinecolor(RGB(0, 0, 0));
                setfillcolor(RGB(0, 255, 255));
                fillrectangle(j * 10, i * 10, j * 10 + 10, i * 10 + 10);
                break;
             }
        }
    }
}
void keyDown() {
    char ch = '\0';
    ch = _getch();
    switch (ch)
    {
    case 'w':
    case 'W':
        if (mapp[x - 1][y] == 0) {
            temp = mapp[x][y];
            mapp[x][y] = mapp[x - 1][y];
            mapp[x - 1][y] =2;
        }
        if (mapp[x - 1][y] == 4) {
            mapp[x][y] = 0;
            mapp[x - 1][y] = 6;
        }
        if (mapp[x - 1][y] == 3 && mapp[x-2][y]==0) {
            mapp[x-2][y] = 3;
            mapp[x - 1][y] = 2;
            mapp[x][y] = 0;
        }
        if (mapp[x - 1][y] == 3 && mapp[x - 2][y] == 4) {
            mapp[x - 2][y] = 5;
            mapp[x - 1][y] = 2;
            mapp[x][y] = 0;
        }
        if (mapp[x - 1][y] == 5 && mapp[x - 2][y] == 4) {
            mapp[x - 2][y] = 5;
            mapp[x - 1][y] = 6;
            mapp[x][y] = 0;
        }
        if (mapp[x - 1][y] == 5 && mapp[x - 2][y] == 0) {
            mapp[x - 2][y] = 3;
            mapp[x - 1][y] = 6;
            mapp[x][y] = 0;
        }

        break;
    case 'a':
    case 'A':
        if (mapp[x][y-1] == 0) {
            temp = mapp[x][y];
            mapp[x][y] = mapp[x][y-1];
            mapp[x][y-1] =2;
        }
        if (mapp[x][y-1] == 4) {
            mapp[x][y] = 0;
            mapp[x][y-1] = 6;
        }
        if (mapp[x][y-1] == 3 && mapp[x][y-2] == 0) {
            mapp[x][y-2] = 3;
            mapp[x][y-1] = 2;
            mapp[x][y] = 0;
        }
        if (mapp[x][y - 1] == 3 && mapp[x][y - 2] == 4) {
            mapp[x][y - 2] = 5;
            mapp[x][y - 1] = 2;
            mapp[x][y] = 0;
        }
        if (mapp[x][y - 1] == 5 && mapp[x][y - 2] == 4) {
            mapp[x][y - 2] = 5;
            mapp[x][y - 1] = 6;
            mapp[x][y] = 0;
        }
        if (mapp[x][y - 1] == 5 && mapp[x][y - 2] ==0) {
            mapp[x][y - 2] = 3;
            mapp[x][y - 1] = 6;
            mapp[x][y] = 0;
        }
        break;
    case 's':
    case 'S':
            if (mapp[x + 1][y] == 0) {
                temp = mapp[x][y];
                mapp[x][y] = mapp[x + 1][y];
                mapp[x + 1][y] =2;
            }
            if (mapp[x + 1][y] == 4) {
                mapp[x][y] = 0;
                mapp[x + 1][y] = 6;
            }
            if (mapp[x + 1][y] == 3 && mapp[x + 2][y] == 0) {
                mapp[x + 2][y] = 3;
                mapp[x + 1][y] = 2;
                mapp[x][y] = 0;
            }
            if (mapp[x + 1][y] == 3 && mapp[x + 2][y] == 4) {
                mapp[x + 2][y] = 5;
                mapp[x + 1][y] = 2;
                mapp[x][y] = 0;
            }
            if (mapp[x + 1][y] == 5 && mapp[x + 2][y] == 4) {
                mapp[x + 2][y] = 5;
                mapp[x + 1][y] = 6;
                mapp[x][y] = 0;
            }
            if (mapp[x + 1][y] == 5 && mapp[x + 2][y] == 0) {
                mapp[x + 2][y] = 3;
                mapp[x + 1][y] = 6;
                mapp[x][y] = 0;
            }
            break;
    case 'd':
    case 'D':
            if (mapp[x][y+1] == 0) {
                mapp[x][y] = mapp[x][y+1];
                mapp[x][y+1] =2;
            }
            if (mapp[x][y+1] == 4) {
                mapp[x][y] = 0;
                mapp[x][y+1] = 6;
            }
            if (mapp[x][y+1] == 3 && mapp[x][y+2] == 0) {
                mapp[x][y+2] = 3;
                mapp[x][y+1] = 2;
                mapp[x][y] = 0;
            }
            if (mapp[x][y + 1] == 3 && mapp[x][y + 2] == 4) {
                mapp[x][y + 2] = 5;
                mapp[x][y + 1] = 2;
                mapp[x][y] = 0;
            }
            if (mapp[x][y + 1] == 5 && mapp[x][y + 2] == 4) {
                mapp[x][y + 2] = 5;
                mapp[x][y + 1] = 6;
                mapp[x][y] = 0;
            }
            if (mapp[x][y + 1] == 5 && mapp[x][y + 2] ==0) {
                mapp[x][y + 2] = 3;
                mapp[x][y + 1] = 6;
                mapp[x][y] = 0;
            }
            break;
    default:
        break;
    }
}
int main() {
    int kl;
    bool checkk;
    HWND hwnd=initgraph(400, 400);
    setbkcolor(RGB(255,255,255));
    cleardevice();
    drawmapp();
    scann();
    while (true) {
        checkk = true;
        settextcolor(BLACK);
        outtextxy(220, 20, "WASD控制");
        keyDown();
        cleardevice();
        drawmapp();
            for (kl = 0; kl < goalx.size(); kl++) {
                if (mapp[goalx[kl]][goaly[kl]] != 5) checkk = false;
    }
            if (checkk) {
                MessageBox(hwnd, "Finish", "Finish!", 0);
            break;
            }
    }
    system("pause");
    return 0;

}
12-22 03:58