刚才尝试写一个让‘@’字符上下左右移动的代码,但是出错了!!大佬们扶一把吧,下面是代码
#include <bits/stdc++.h>
using namespace std;
int x = 0, y = 0;
char maze[100][100];
void print() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
cout << maze[i][j];
}
cout << endl;
}
}
int main() {
system("cls");
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (i == 0 && j == 0) maze[i][j] = '@';
else maze[i][j] = '#';
}
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
cout << maze[i][j];
}
cout << endl;
}
while (1) {
char ch;
cin >> ch;
if (ch == 'w') {
maze[x][y + 1] = '@';
maze[x][y] = '#';
y += 1;
system("cls");
print();
}
if (ch == 's') {
maze[x][y - 1] = '@';
maze[x][y] = '#';
y -= 1;
system("cls");
print();
}
if (ch == 'a') {
maze[x - 1][y] = '@';
maze[x][y] = '#';
x -= 1;
system("cls");
print();
}
if (ch == 'd') {
maze[x + 1][y] = '@';
maze[x][y] = '#';
x += 1;
system("cls");
print();
}
}
return 0;
}