私は2次元配列を持っています。中にはキャラクターがい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