C++ - 国际象棋主教移动代码错误

C++ - Chess Bishop MoveCode Error?

本文关键字:移动 代码 错误 国际象棋 C++      更新时间:2023-10-16

所以我正在做一个国际象棋游戏,但是我无法让主教棋子正确移动。

这是我的棋盘:

string board[8][8] = {
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "B" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"} };

这是绘制板的绘图功能。

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] = "33[0;31mB33[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。

您需要

检查x + countery + counter是否属于董事会。

if (board[x][y] == "B")
{      //Highlight the users chosen piece
    board[x][y] = "33[0;31mB33[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;
        }
    }
}

当然,这只标志着一个方向,而主教实际上可以向四个方向移动。

此外,这不会检查路径之间是否有任何部分。

要考虑所有四个方向,您可以创建一个方向矩阵,该矩阵存储每个方向的 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;
            }
        }
    }
}