我有一个二维数组。里面有一个字符U
。我的代码搜索字符,然后输出数组,但只显示U
它周围的每个字符。我的代码在某个地方出错了,并且U
输出的似乎与它应该在的位置相反。
原始数组是gameboard[]
,可以很好地读取。我们也不能使用向量、指针,就像我已经使用过的一样
int boardSizeRow;
int boardSizeCol;
inputFile.open("C:\\Users\\Michael\\Desktop\\fileboard1.txt");
inputFile >> boardSizeRow;
inputFile >> boardSizeCol;
inputFile.get();
char gameBoard[21][21];
for (int row = 0; row <= boardSizeRow; row++)
{
for (int col = 0; col <= boardSizeCol; col++)
{
gameBoard[row][col] = inputFile.get();
}
//inputFile.get();
}
for (int row = 0; row <= boardSizeRow; row++)
{
for (int col = 0; col <= boardSizeCol; col++)
{
cout << gameBoard[row][col];
}
//cout << endl;
}
bool toPrint[21][21] = {false};
for (int i = 0; i < boardSizeRow; i++ )
{
for (int j = 0; j < boardSizeCol; j++)
{
if (gameBoard[i][j] == 'U')
{
toPrint[i][j] = true;
toPrint[i][j-1] = true; //West
toPrint[i][j+1] = true; //East
toPrint[i-1][j] = true; //North
toPrint[i+1][j] = true; //South
}
}
}
for (int i = 0; i < boardSizeRow; i++ )
{
for (int j = 0; j < boardSizeCol; j++)
{
if (toPrint[i][j] == true)
{
cout << gameBoard[i][j];
}
else
{
cout << "0";
}
}
cout<<endl;
}
原始数组:
WWWWWWWWWW
W WW GO W
W WW WWW W
W W GW
WPWG WW W
WWWDWK WW
W GW W W
W WW KWAW
W SW UW
WWWWWWWWWW
** 想要的输出:
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
00000000A0
00000000UW
00000000W0
这是我得到的随机输出示例:http: //tinypic.com/r/2ujo8/6