兵’进行’将军’

2.1 说明

实现的大体过程

  1. 遍历所有的棋子
  2. 如果是”兵“则判断它能否攻击到对方的”将“

2.2 代码实现

    @staticmethod
    def judge_z_attack(attack_team, attack_row, attack_col, general_row, general_col):
        """
        判断卒是否攻击到"将"
        """
        if attack_team == "r" and attack_row < general_row:
            return False
        elif attack_team == "b" and attack_row > general_row:
            return False
        elif (attack_row - general_row) ** 2 + (attack_col - general_col) ** 2 == 1:
            return True
        # 2. 遍历我方所有的棋子
        for row, line in enumerate(self.chessboard_map):
            for col, chess in enumerate(line):
                if chess and chess.team == attact_player:
                    if chess.name == "z":  # 兵
                        # 传递5个参数(攻击方的标识,攻击方row,攻击方col,对方将row,对方将col)
                        if self.judge_z_attack(chess.team, chess.row, chess.col, *general_position):
                            return True

image-20210821200320139

print("将军....")

image-20210821200432022

2.3 运行效果

img

Copyright © itprojects.cn 2021 all right reserved,著作权归itprojects.cn所有,由王铭东编写该文件修订时间: 2021-08-21 20:35:23