私はチェスの試合をしていますが、ビショップを正しく動かすことはできません。C++ - Chess Bishop MoveCodeエラー?
これは私のチェス盤である:ここでは
string board[8][8] = {
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "B" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"} };
は、ボードを描画するDraw関数です。
void Draw()
{
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 8; j++)
std::cout << board[ i ][ j ] << ' ';
std::cout << '\n';
}
cout<<"\n";
}
これまでのビショップムーブメントコード。
if (board[x][y] == "B")
{ //Highlight the users chosen piece
board[x][y] = "\033[0;31mB\033[0m";
//Now showing available moves the chosen bishop can move to
for(int counter=1; counter <=7; counter++) {
if(board[x+counter][y+counter] == "_") { //if there is an empty space, then place X to show peice can move there
board[x+counter][y+counter] = "X";
}
else { //if cannot move their ,then break
break;
}
}
}
これは私の問題です。これは、ボード上のいくつかの場所でユーザーに移動できるXスペースを示しています。しかし、作品が場所のような配列の特定の場所にあるときは、ボードコードにあります。それは重複し、ボードの異なる側にXを表示します。
デバッガでコードを実行しましたか? –
ボード[x + counter] [y + counter]のインデックスにオーバーフローとアンダーフローがあります。 – PanicSheep
@PanicSheepのコメント司教から離れて、ビショップも後方に移動します。 – bansi