我是一個C++新手,在參考網路上的資料製作一個迷宮小遊戲。
卻一直出現可讀取大小只有多少個位元組,但可能會讀取多達"負數"個位元組,這一類的警告訊息。
實在搞不太清楚,希望有高人指點。
IDE: vs code
以下是我的程式碼:
#include
#include<conio.h>//非標準函式
#include<Windows.h>//不太了解只懂部分功能
using namespace std;
int a[20][20] = { {1,1,1,1,1,1,1},
{1,8,0,0,0,0,1},
{1,0,0,1,1,1,1},
{1,0,0,0,0,0,1},
{1,1,0,1,0,1,1},
{1,0,0,1,0,0,0},
{1,1,1,1,1,1,1} };
int main() {
int one = 0, two = 0,x=1, y=1,n=0;
char in;
for (one=0; one < 7; one++) {
for (two = 0; two < 7; two++) {
if (a[one][two] != 0) {
cout << a[one][two];
}
else {
cout << " ";
}
}
cout << endl;
}
while (1) {
in = _getch();
if (in=='w') {
if (a[x - 1][y] != 1) {
n = a[x][y];
a[x][y] = a[x - 1][y];
a[x - 1][y] = n;
x--;
}
}
else if (in=='s') {
if (a[x + 1][y] != 1) {
n = a[x][y];
a[x][y] = a[x + 1][y];
a[x + 1][y] = n;
x++;
}
}
else if (in == 'd') {
if (a[x][y+1] != 1) {
n = a[x][y];
a[x][y] = a[x][y+1];
a[x][y+1] = n;
y++;
}
}
else if (in == 'a') {
if (a[x][y-1] != 1) {
n = a[x][y];
a[x][y] = a[x][y-1];
a[x][y-1] = n;
y--;
}
}
system("cls");
for (one = 0; one < 7; one++) {
for (two = 0; two < 7; two++) {
if (a[one][two] != 0) {
cout << a[one][two];
}
else {
cout << " ";
}
}
cout << endl;
}
if (x == 5 && y == 6) {
break;
}
}
cout << "You Win!!" << endl;
}
if (in=='w') {
if (a[x - 1][y] != 1) {
n = a[x][y];
a[x][y] = a[x - 1][y];
a[x - 1][y] = n;
x--;
}
}
else if (in=='s') {
if (a[x + 1][y] != 1) {
n = a[x][y];
a[x][y] = a[x + 1][y];
a[x + 1][y] = n;
x++;
}
}
else if (in == 'd') {
if (a[x][y+1] != 1) {
n = a[x][y];
a[x][y] = a[x][y+1];
a[x][y+1] = n;
y++;
}
}
else if (in == 'a') {
if (a[x][y-1] != 1) {
n = a[x][y];
a[x][y] = a[x][y-1];
a[x][y-1] = n;
y--;
}
}
改成
if (in=='w') {
if ((x-1)>=0) {
if (a[x - 1][y] != 1) {
n = a[x][y];
a[x][y] = a[x - 1][y];
a[x - 1][y] = n;
x--;
}
} else {
//已碰到最「上面」的邊界
}
}
else if (in=='s') {
if ((x+1)<=6) {
if (a[x + 1][y] != 1) {
n = a[x][y];
a[x][y] = a[x + 1][y];
a[x + 1][y] = n;
x++;
}
} else {
//已碰到最「下面」的邊界
}
}
else if (in == 'd') {
if ((y+1)<=6) {
if (a[x][y+1] != 1) {
n = a[x][y];
a[x][y] = a[x][y+1];
a[x][y+1] = n;
y++;
}
} else {
//已碰到最「右邊」的邊界
}
}
else if (in == 'a') {
if ((y-1)>=0) {
if (a[x][y-1] != 1) {
n = a[x][y];
a[x][y] = a[x][y-1];
a[x][y-1] = n;
y--;
}
} else {
//已碰到最「左邊」的邊界
}
}
試試看
另外
下次要貼程式碼時
請使用三個單引號把程式碼包起來
'''
asdfasdf
'''
以上用單引號做為示範,正確應使用鍵盤最左上方的那個back quote(`)