繁体   English   中英

C ++ - Chess Bishop MoveCode错误?

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

所以我正在制作国际象棋游戏,但是我不能让主教乐队正确地移动。

这是我的棋盘:

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] = "\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.

您需要检查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;
        }
    }
}

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

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

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