C ++检查国际象棋游戏中是否有玩家在路上
C++ check if there are players on the way in Chess game
我正在尝试检查是否有棋手正在下棋。
为此,我有以下变量
this->getLocX(),this->getLocY()(玩家所在位置),x,y(玩家想去的地方)
我有一个函数 boardP->hasPiece(x, y) returns 我 true(1)如果在给定(x,y)
的点中有玩家
我目前正在为主教做这件事,所以它可以而且需要在诊断中检查玩家(我已经检查过移动是否无效
这里是我试过的,但它不起作用,即使有玩家,玩家仍然可以移动,我可以知道,因为该程序是代表玩家的 c# 程序的本地主机。
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;
你真的需要一个解决方案,假设位置是 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
}
我正在尝试检查是否有棋手正在下棋。 为此,我有以下变量 this->getLocX(),this->getLocY()(玩家所在位置),x,y(玩家想去的地方)
我有一个函数 boardP->hasPiece(x, y) returns 我 true(1)如果在给定(x,y)
的点中有玩家我目前正在为主教做这件事,所以它可以而且需要在诊断中检查玩家(我已经检查过移动是否无效
这里是我试过的,但它不起作用,即使有玩家,玩家仍然可以移动,我可以知道,因为该程序是代表玩家的 c# 程序的本地主机。
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;
你真的需要一个解决方案,假设位置是 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
}