おそらく、迷路を 2 次元の char 配列に格納したいと思うでしょう。C++ では、配列を初期化するかどうかに関係なく宣言できます。
char a[30][10]; // declares a char array of 30 rows and 10 columns.
// declare an array with 3 rows and 3 columns, and provide initial values
char ticTacToeBoard[3][3] = {{'x', 'x', 'o'},
{'o', 'o', 'x'},
{'x', 'o', ' '}
};
迷路の壁と壁'|'
の初期値を変更し、通路にスペース文字 を使用できます。どちらの初期化方法でも機能しますが、常に同じ方法で要素を使用します。上記の初期化された配列でボードをクリアする方法は次のとおりです。'-'
' '
// clear the board
for (int row=0; row<3; row++) {
for (int col=0; col<3; col++) {
ticTacToeBoard[row][col] = ' ';
}
}
要素の値を読み取りたい場合 (迷路をナビゲートしようとしている場合に役立ちます)、その値を設定するときと同じ添字表記を使用します。
char y = a[2][2]; // reads the character in row 2, column 2