所以,我在完成C 的国际象棋游戏的任务中已经走了很远。但是,我想在一个小问题上遇到了一些小问题,我想获得一些输入。
情况:
我的棋子,国王,骑士举动验证效果非常完美。但;当移动一块(例如白色的菜单)时,它会遵循大多数规则。例如,它只会移动垂直或水平,它不会通过另一件白色,它将不会替换白色碎片,最后它将代替黑色(相反)的碎片。
问题是在将其移动到另一件黑色的一块时,它可以通过以替换过去的一块。因此,可以说,我们在x = 2,y = 6和x = 2,y = 4的黑色零件时有一个白色片,而黑色则是x = 2,y = 3的另一个黑色。将允许白色件移动到x = 2,y = 3,不允许。很想就如何解决此问题获得一些意见。下面的当前代码。
bool Rook:: canMove(int startx, int starty, int endx, int endy)
{
int i;
if(board[endx][endy] !=NULL && board[endx][endy]->color==color)
return false;
if (startx == ends) //Collision Detection...
{
// Horizontal move
if (starty < endy)
{
// Move down
for (i = starty + 1; i <= endy; ++i)
if (board[startx][i] != NULL && board[startx][i]->color==color)
return false;
}
else
{
// Move up
for (i = starty - 1; i >= endy; --i)
if (board[startx][i] != NULL && board[startx][i]->color==color) //cant allow passing of non color piece
return false;
}
}
else if (starty == endy)
{
// Vertical move
if (startx < endx)
{
// Move right
for (i = startx + 1; i <= endx; ++i)
if (board[i][starty] != NULL && board[i][starty]->color==color)
return false;
}
else
{
// Move left
for (i = startx - 1; i >= endx; --i)
if (board[i][starty] != NULL && board[i][starty]->color==color)
return false;
}
}
else
{
// Not a valid rook move (neither horizontal nor vertical)
return false;
}
return true;
}
您的功能是指类中的许多成员变量,例如末端,颜色,板不好,并且使功能难以在单位级别进行测试您可以独立测试该功能吗?不,你不能。
,但看起来您的循环在应该时没有破裂(当他们找到有效的举动时?)如果该功能允许移动到(2,3)以及(2,4),则它将其循环(2,4)到(2,3)
另外,仅使用阵列和ints索引板并不是很好。我本来可以期待高级董事会课程,也许是坐标类,因此您可以轻松迭代并为董事会索引。