我正在使用 this面向对象的国际象棋设计。我已经实现了为所有棋子生成有效的移动。现在我正在尝试实现将死检查。
我尝试制作一种方法,如果玩家有移动,则取消将死。但是程序以 StackOverflowError 结束。
我删除了方法。但是该方法的伪算法是这样的
boolean isGameOver(arg){
if(playerIsInCheck){
if(!hasValidMoves){
print("checkmate");
return true;
}
else{
return false;
}
}
else{
if(!hasValidMoves){
print("stalemate");
return true;
}
else{
return false;
}
}
}
我不知道如何检查移动是否取消了将死。谁能给我建议?我不需要用任何编程语言编写的所有代码。伪算法就足够了。
最佳答案
检查将死的算法如下:
public boolean checkmated(Player player) {
if (!player.getKing().inCheck() || player.isStalemated()) {
return false; //not checkmate if we are not
//in check at all or we are stalemated.
}
//therefore if we get here on out, we are currently in check...
Pieces myPieces = player.getPieces();
for (Piece each : myPieces) {
each.doMove(); //modify the state of the board
if (!player.getKing().inCheck()) { //now we can check the modified board
each.undoMove(); //undo, we dont want to change the board
return false;
//not checkmate, we can make a move,
//that results in our escape from checkmate.
}
each.undoMove();
}
return true;
//all pieces have been examined and none can make a move and we have
//confimred earlier that we have been previously checked by the opponent
//and that we are not in stalemate.
}
关于algorithm - 在国际象棋中检查将死,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30401046/