我正在尝试检查国际象棋比赛中是否有玩家。 为此,我有以下变量 this-> getLocX(), this-> getLocY()(播放器所在的位置), x , y (玩家想去的地方)
并且我有一个函数 boardP-> hasPiece(x,y),如果给定的点数中有玩家,则返回我 true (1) ,y)
我目前正在为Bishop进行此操作,因此它可以而且需要在诊断方面检查球员(我已经检查了此举是否有效
这是我尝试过的方法,它不起作用,即使有播放器,播放器仍然可以移动,我可以知道,因为该程序是代表播放器的c#程序的localHost。
C ++:
if (abs(x - this->getLocX()) == abs(y - this->getLocY())) // THIS WORKS GOOD
{
cout << "\n" << abs(x - this->getLocX()) << abs(y - this->getLocY()) << x << y;
for (int i = 0; i < abs(x - this->getLocX()); i++)
{
cout << "\n" << boardP->hasPiece(this->getLocX() - 1, i-1) << "\n" << boardP->hasPiece(i, y) << "\n" << x << "\n" << y << "\n" << i << "\n";
if (boardP->hasPiece(this->getLocX() - 1, i-1)) // THIS DOESNT WORK
return 0; // THERE ARE PLAYERS IN THE WAY
}
return 2; // THERE ARE NO PLAYERS IN THE WAY
}
return 0;
答案 0 :(得分:0)
您真的需要一个解决方案,假设位置是int:
if (abs(x - this->getLocX()) == abs(y - this->getLocY()))
{
int cx = this->getLocX();
int cy = this->getLocY();
int dx = (x > cx) ? 1 : -1;
int dy = (y > cy) ? 1 : -1;
while (cx != x)
{
cx += dx;
cy += dy;
if (boardP->hasPiece(cx, cy))
return true; // THERE ARE PLAYERS IN THE WAY
}
return false; // THERE ARE NO PLAYERS IN THE WAY
}