将国际象棋中敌人的可能走法获取到二维数组 - Python

Get enemy's possible moves in chess to a 2D array - Python

我有三个数组,

chess = [["c","d","e","f","g","e","d","c"],
         ["b","b","b","b","b","b","b","b"],
         ["a","a","a","a","a","a","a","a"],
         ["a","a","a","a","a","a","a","a"],
         ["a","a","a","a","a","a","a","a"],
         ["a","a","a","a","a","a","a","a"],
         ["h","h","h","h","h","h","h","h"],
         ["i","j","k","l","m","k","j","i"]]

存储棋子的位置,('a' 是空的,'b' 是黑兵等)和另外两个填充了 False 的 8x8 数组,名为 whiteMoves 和 blackMoves。我要的是函数

def getEnemyMoves():
    global chess, whiteMoves, blackMoves
    doSomethingToWhiteMoves()
    doSomethingToBlackMoves()

例如这些:

我尝试了很多不同的方法来做到这一点,但它们非常复杂,而且 none 的方法完全有效。这是我的棋局结束前唯一要做的事情。

遍历棋盘上的棋子。对于每一块,根据该块的移动规则缩小目标方块:

  • 国王可以移动到相邻的8个方格或每边两个方格
  • 兵可以向前移动一格或两格,也可以向前移动一格。
  • 等等

对于每个这样的候选移动,测试是否有额外的约束阻止该候选移动:

  • 国王只能向一侧移动两个方格,如果它城堡。
  • 棋子只能沿对角线前进,如果它吃掉了对手的棋子(可能是过路兵)。
  • 车不能跳过其他棋子
  • 等等

如果将这两个过程合并为一个过程,可以节省一些 CPU 个周期:

  • 如果a1上的车不能移动到a3,因为有挡块,肯定不能移动到a4、a5、a6、a7、a8。

这些东西复杂。例如。在最终决定一步之前,你必须测试国王是否被控制。