剩余棋子’将军’

4.1 说明

只要将军的大体原理过程明白了,至于其他的棋子大体过程也是类似的,因此我们就一次性的将剩余棋子的将军功能全部实现

4.2 代码实现

    def judge_j_attack(self, attack_row, attack_col, general_row, general_col):
        """
        判断 两个将是否相对
        """
        if attack_col == general_col:
            # 在同一列
            min_row, max_row = (attack_row, general_row) if attack_row < general_row else (general_row, attack_row)

            chess_num = 0
            for i in range(min_row + 1, max_row):
                if self.chessboard_map[i][general_col]:
                    chess_num += 1
            if chess_num == 0:
                return True

    def judge_m_attack(self, attack_row, attack_col, general_row, general_col):
        """
        判断马是否攻击到"将"
        """
        if attack_row == general_row or attack_col == general_col:
            return False
        else:
            # "马走日",利用这个特点会得出,如果此马能够攻击到"将",那么两条边的平方和一定是5
            col_length = (attack_col - general_col) ** 2
            row_length = (attack_row - general_row) ** 2
            if col_length + row_length == 5:
                # 判断是否蹩马腿
                if col_length == 1:
                    if general_row < attack_row and not self.chessboard_map[attack_row - 1][attack_col]:
                        return True
                    elif general_row > attack_row and not self.chessboard_map[attack_row + 1][attack_col]:
                        return True
                elif col_length == 4:
                    if general_col < attack_col and not self.chessboard_map[attack_row][attack_col - 1]:
                        return True
                    elif general_col > attack_col and not self.chessboard_map[attack_row][attack_col + 1]:
                        return True

    def judge_c_and_p_attack(self, attack_chess_name, attack_row, attack_col, general_row, general_col):
        """
        判断"车"、"炮"能否攻击到对方"将"
        """
        check_chess_num = 1 if attack_chess_name == "p" else 0
        chess_num = 0
        if attack_row == general_row:
            # 在同一行
            min_col, max_col = (attack_col, general_col) if attack_col < general_col else (general_col, attack_col)
            for i in range(min_col + 1, max_col):
                if self.chessboard_map[attack_row][i]:
                    chess_num += 1
            if chess_num == check_chess_num:
                return True
        elif attack_col == general_col:
            # 在同一列
            min_row, max_row = (attack_row, general_row) if attack_row < general_row else (general_row, attack_row)
            for i in range(min_row + 1, max_row):
                if self.chessboard_map[i][general_col]:
                    chess_num += 1
            if chess_num == check_chess_num:
                return True

image-20210821201039536

elif chess.name == "p":  # 炮
    if self.judge_c_and_p_attack(chess.name, chess.row, chess.col, *general_position):
        return True
elif chess.name == "c":  # 车
    if self.judge_c_and_p_attack(chess.name, chess.row, chess.col, *general_position):
        return True
elif chess.name == "m":  # 马
    if self.judge_m_attack(chess.row, chess.col, *general_position):
        return True
elif chess.name == "x":  # 象
    pass
elif chess.name == "s":  # 士
    pass
elif chess.name == "j":  # 将
    if self.judge_j_attack(chess.row, chess.col, *general_position):
        return True

image-20210821201144219

4.3 运行效果

"炮"将军的效果

img

"两将"相对的效果

img

"马"将军的效果

img

"车"将军的效果

img

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