algorithm - 在国际象棋中检查将死

标签 algorithm chess

我正在使用 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/

相关文章:

C++ |矩阵添加奇怪的值

java - 如何检查主教的空对角线?

algorithm - 如何超越字节流中包含的压缩字节序列?

algorithm - 查找一组间隔的覆盖范围

algorithm - 系统编程 |调度时间

java - 将静态变量存储在临时变量中

ios - 如何在 xcode 项目中发布树结构?

java - Swing中的按钮点击事件

python - 使用分而治之找到数字数组的反转

algorithm - 使用四叉树时如何处理在四边形之间移动的对象?