I have three arrays,
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"]]
that stores the positions of the pieces, ('a' is empty, 'b' is a black pawn etc.) and two other 8x8 arrays filled with False named whiteMoves and blackMoves. What I want is a function
def getEnemyMoves():
global chess, whiteMoves, blackMoves
doSomethingToWhiteMoves()
doSomethingToBlackMoves()
that does for example these:
- If there is an enemy piece in chess[5][0] (or a3), whiteMoves[5][0] should turn True because the pawn in chess[7][1] (b2) can eat it.
- If a black queen would be in chess[7][0] (a1) and the king in chess[7][1] (b1), blackMoves[7][1] should turn True.
- whiteMoves[0][0] should be True if there is a black piece in danger in chess[0][0] (a8).
I have tried many different ways to do this, but they were very complex and none of them worked fully. This is the only thing to do before my chess game is finished.
Iterate over the pieces on the board. For each piece, narrow down the destination squares according to the move rules for that piece:
For each such candidate move, test whether additional constraints prevent that candidate move:
If you combine these two passes into a single one, you save some CPU cycles:
These things are complex. E.g. before finally deciding on a move, you have to test whether the king is in check.