2017-01-13 11 views
6

私はチェスの試合をしていますが、ビショップを正しく動かすことはできません。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を表示します。

+0

デバッガでコードを実行しましたか? –

+2

ボード[x + counter] [y + counter]のインデックスにオーバーフローとアンダーフローがあります。 – PanicSheep

+0

@PanicSheepのコメント司教から離れて、ビショップも後方に移動します。 – bansi

答えて

7

x + countery + counterがボード内にあるかどうかを確認する必要があります。

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; (x + counter) <= 7 && (y + 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; 
     } 
    } 
} 

もちろん、ビショップは実際には4方向に移動することができますが、これは一方向にしか表示されません。

また、パスの間にピースがあるかどうかはチェックされません。

4つの方向すべてを考慮するには、それぞれの方向のxとyの変化を保存する方向行列を作成できます。

// 4 directions in which bishop can move 
int dx[4] = {-1, -1, 1, 1}; 
int dy[4] = {-1, 1, -1, 1}; 


if (board[x][y] == "B") 
{ 
    // for each direction 
    for(int dir = 0; dir < 4; dir++) { 

     // the bishop can move to a maximum of 7 squares 
     for(int counter = 1; counter < 8; counter++) { 

      // calculate where the bishop will be 
      // after moving "counter" number of squares 
      int new_x, new_y; 
      new_x = x + dx[dir] * counter; 
      new_y = y + dy[dir] * counter; 

      // check if the square lies within the board 
      if(new_x >= 0 && new_x < 8 && new_y >= 0 && new_y < 8) { 

       // if there is an empty space, then place X to show peice can move there 
       if(board[cur_x][cur_y] == "_") { 
        board[cur_x][cur_y] = "X"; 
       } 

       // if there is any other piece in between, the bishop can't move further 
       else { 
        break; 
       } 
      } 

      // break if the square is outside the board 
      else { 
       break; 
      } 
     } 
    } 
} 
+0

ありがとうございます。私はあなたの応答から多くを学んだ。 –

+1

すばらしい答え! ;) '++' – Inian

+0

ありがとう@Inian :) –

関連する問題