所以我正在制作国际象棋游戏,但是我不能让主教乐队正确地移动 .
这是我的棋盘:
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空间 . 但是当这个部件位于阵列的某些位置时,就像它在板代码中的位置一样 . 它重叠并在板的不同侧显示Xs,而不是在它们没有“_”可用时停止绘制X.
1 回答
您需要检查
x + counter
和y + counter
是否属于电路板 .当然,这只标志着一个方向,而主教实际上可以向四个方向移动 .
此外,这不会检查路径之间是否有任何碎片 .
要考虑所有四个方向,您可以创建方向矩阵,该矩阵存储每个方向的x和y变化 .