C语言的推箱子在推箱子移动的时候存在一个错误,求大牛解答,并且修改程序绝对不麻烦,谢谢了。

#include
#include
#include

//定义墙壁的高和宽
#define H 11
#define W 26

int scr[H][W] = {{1,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,0,0,0,0,0,0,0,0,0,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,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,1},
{1,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,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},
{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},
{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},
{1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0},
{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},};//2为人,3为箱子,4为位置

typedef struct {
int x;
int y;
char flag;
}person_t;

typedef struct {
int x;
int y;
char flag;
}box_t;

person_t per;
box_t box;

void print(int a[H][W]);
void menu();
void MoveUpOfBox(int a[][W]);
void MoveDownOfBox(int a[][W]);
void MoveLeftOfBox(int a[][W]);
void MoveRightOfBox(int a[][W]);

int main(int argc, char *argv[]){
menu();

per.x = 5;
per.y = 5;
box.x = 4;
box.y = 6;

scr[per.x][per.y] = 2;
scr[box.x][box.y] = 3;  

while(1){
    if(kbhit()){
        switch(getch()){
        case 'a':
        case 'A':
            if(per.y >= 1){
                scr[per.x][per.y] = 0;
                if(box.x == per.x && per.y == box.y + 1){
                    MoveLeftOfBox(scr);
                }
                scr[per.x][--per.y] = 2;
            }
            break;
        case 'd':
        case 'D':
            if(per.y <= W - 1){
                scr[per.x][per.y] = 0;
                if(per.x == box.x && per.y == box.y - 1){
                    MoveRightOfBox(scr);
                }
                scr[per.x][++per.y] = 2;
            }   
            break;
        case 'w':
        case 'W':
            if(per.x >= 1){
                scr[per.x][per.y] = 0;
                if(per.y == box.y && per.x == box.x + 1){
                    MoveUpOfBox(scr);
                }
                scr[--per.x][per.y] = 2;
            }
            break;
        case 'S':
        case 's':
            if(per.x <= H - 1){
                scr[per.x][per.y] = 0;
                if(per.y == box.y && per.x == box.x - 1){
                    MoveDownOfBox(scr);
                }
                scr[++per.x][per.y] = 2;
            }
            break;
        default:
            printf("enter error. please again.\n");             
        }
    }

    print(scr);
}

return 0;

}

void print(int a[H][W]){
int i, j;

system("cls");
for(i = 0; i <= H - 1; i++){
    for(j = 0; j <= W - 1; j++){            
            a[i][W - 1] = 4;
        if(a[i][j] == 0){
            printf(" ");
        }
        if(a[i][j] == 1){
            printf("█");
        }
        if(a[i][j] == 2){
            printf("⊙");
        }
        if(a[i][j] == 3){
            printf("▓");
        }
        if(a[i][j] == 4){
            printf("|\n");
        }
    }
}

}

void menu(){
printf("欢迎,请点击任意键继续.\n");
getch();
}

void MoveLeftOfBox(int a[][W]){
a[box.x][box.y] = 0;
a[box.x][--box.y] = 3;
}

void MoveRightOfBox(int a[][W]){
a[box.x][box.y] = 0;
a[box.x][++box.y] = 3;
}

void MoveUpOfBox(int a[][W]){
a[box.x][box.y] = 0;
a[--box.x][box.y] = 3;
}

void MoveDownOfBox(int a[][W]){
a[box.x][box.y] = 0;
a[++box.x][box.y] = 3;
}

c

2个回答

程序报啥错误啊,亲!!!!

Oraice
Oraice 不好意思,我之前忘记回复了。我说的错误并不是代码中的错误,而是在程序运行过程中存在的错误,比如说:1.往一个方向移动时会出现内存错误, 方块移动的那行会比定义的时候多出一格。2.连续按住一个方向键的时候方块会错误移动等等,反正错误很多,不知道注意了没。
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!