简体   繁体   English

C ++ - Chess Bishop MoveCode错误?

[英]C++ - Chess Bishop MoveCode Error?

So i'm making a chess game, however i can't get the bishop piece moving correctly. 所以我正在制作国际象棋游戏,但是我不能让主教乐队正确地移动。

This is my chessboard: 这是我的棋盘:

string board[8][8] = {

{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "B" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"} };

Here is the Draw function to draw board. 这是绘制板的绘图功能。

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";
}

Bishop Movement Code so far. 主教运动法至今。

 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;
         }
     }
}

Here is my problem. 这是我的问题。 It shows the X spaces the piece is able to move to to the user in some places on the board. 它显示了工件在板上某些位置可以移动到用户的X空间。 however when the piece is in certain places of the array like the the place it is in the board code. 但是当这个部件位于阵列的某些位置时,就像它在板代码中的位置一样。 It overlaps and shows Xs on different side of the board, instead of stopping drawing the Xs when their is no "_" available. 它重叠并在板的不同侧显示X,而不是在它们没有“_”可用时停止绘制X.

You need to check if x + counter and y + counter fall within the board. 您需要检查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;
        }
    }
}

Of course, this marks only one direction, while the bishop can actually move in four directions. 当然,这只标志着一个方向,而主教实际上可以向四个方向移动。

Also, this does not check if there are any pieces in between the path. 此外,这不会检查路径之间是否有任何碎片。

To consider all four directions, you can create a direction matrix, which stores the change in x and y for each of the directions. 要考虑所有四个方向,您可以创建方向矩阵,该矩阵存储每个方向的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;
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM