手が疑似合法である場合にTrueを返す関数を作成しています。問題は、ポーンが開始位置にあるかどうかを調べるために、この行のすべての正方形をループして、ポーンがある正方形がその範囲内にあるかどうかを確認する必要があることです。これを行うより良い方法はありますか?ポーンがオンになっている行番号などをどうにかして計算できますか?
# Pawn.
if piece == PAWN:
if not from_piece & 8: # White pawn.
if start_point < end_point: # Can't move backwards.
return False
elif diff == 32 and start_point in range(96, 104) and not board[start_point - 16] and not board[end_point]:
pass # Valid case, skip next elif statement. Can move 2 spaces at the starting position.
elif diff != 16:
return False
else: # Black pawn.
if start_point > end_point: # Can't move backwards.
return False
elif diff == 32 and start_point in range(16, 24) and not board[end_point + 16] and not board[end_point]:
pass # Valid case, skip next elif statement. Can move 2 spaces at the starting position.
elif diff != 16:
return False
if diff == 16 and board[end_point]: # Can't move ahead one square if there's already a piece.
return False
elif (diff == 15 or diff == 17) and board[end_point]: # Can't move one step diagonal when there's no enemy piece.
return False
return True
白ポーンの値が 1、黒ポーンの値が 9 になるようにプログラムを作成しました。空のマスは 0 で表されます。
どうもありがとうございました。