我正在尝试用 python 构建一个国际象棋游戏。到目前为止,我已经了解了棋子的显示和基本的移动机制。我现在正在尝试实施检查。
为此,使用这个视频的想法,我想做的是对于每一个伪合法的移动,我在棋盘上进行,计算所有可能的移动,然后检查是否有任何目标方块是“我的国王”然后从伪合法动作列表中删除该动作,然后在棋盘上取消该动作。
问题是它不能正常工作。对于某些动作,它可以正常工作,但对于其他动作则不然。
这是检查支票的功能。它采用了我要检查的棋子的伪合法动作列表。
def check_check(moves):
temp_board = vars.board
#For each of the possible moves
y_start, x_start = vars.number_to_position_map[moves[0].start_square_num]
for move in moves:
#If its to the same square it doesnt matter
if move.start_square_num == move.target_square_num:
continue
#Get the squares
y_target, x_target = vars.number_to_position_map[move.target_square_num]
print(y_target,x_target)
#Save the piece in target square
target_square_piece = temp_board[y_target][x_target].piece
#Make move
temp_board[y_start][x_start].piece = None
temp_board[y_target][x_target].piece = move.piece
#Calculate moves
all_moves = calculate_all_moves()
#TODO: FIX THIS
for new_move in all_moves:
#If it's one of my pieces I dont care, and If it's in the same square I don't care
if new_move.piece.is_white != move.piece.is_white #and new_move.start_square_num != #new_move.target_square_num:
#Get target square
y, x = vars.number_to_position_map[new_move.target_square_num]
if not move.piece.is_white:
if temp_board[y][x].piece == vars.black_king_piece:
moves.remove(move)
print("HERE")
print(temp_board[y][x].piece.is_white,temp_board[y][x].piece.value)
else:
if temp_board[y][x].piece == vars.white_king_piece:
moves.remove(move)
print("HERE?")
#
temp_board[y_start][x_start].piece = move.piece
temp_board[y_target][x_target].piece = target_square_piece
return moves
在国际象棋中检查支票
注意:我在 if 中评论了一个条件,因为问这个问题的格式不工作,但必须取消评论