我想让数字2上下左右移动,但运行时会让周围的墙改变(1代表墙2表示空位)
代码在下面求大佬解答#include<stdio.h>
#include<Windows.h>
#include<conio.h>
int main()
{
int arr[5][5]{
1,1,1,1,1,
1,0,0,0,1,
1,0,2,0,1,
1,0,0,0,1,
1,1,1,1,1,
};
int x = 0;
int y = 0;
while (1) {
system("cls");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++)
{
printf("%d\t", arr[i][j]);
if (arr[i][j] == 2) {
x = i;
y = j;
}
}
printf("\n");
}
switch (_getch()) {
case 'w':
if (arr[x-1][y] == 0) {
arr[x - 1][y] += 2;
arr[x][y] -= 2;
}
case 's':
if (arr[x - 1][y] == 0) {
arr[x + 1][y] += 2;
arr[x][y] -= 2;
}
case 'a':
if (arr[x][y - 1] == 0) {
arr[x][y - 1] += 2;
arr[x][y] -= 2;
}
case 'd':
if (arr[x][y + 1] == 0) {
arr[x][y + 1] += 2;
arr[x][y] -= 2;
}
}
}
return 0;
}