Exemple #1
0
    def get_action(self, chess, fpos, tpos, standard=False):
        delta = (tpos[0] - fpos[0], tpos[1] - fpos[1])
        if Chess.is_red(chess):
            delta = (-delta[0], -delta[1])

        posx = self.get_pos(chess, tpos, standard)

        if Chess.chess(chess) in {
                Chess.ROOK, Chess.CANNON, Chess.PAWN, Chess.KING
        }:
            posy = self.CNUMS[abs(delta[1])]
            if Chess.is_black(chess) and standard:
                posy = self.ANUMS[abs(delta[1])]
        else:
            posy = posx

        if delta[1] > 0:
            action = '进'
            pos = posy
        elif delta[1] < 0:
            action = '退'
            pos = posy
        else:
            action = f'平'
            pos = posx

        return f'{action}{pos}'
Exemple #2
0
    def validatePosition(self, board, pos, chess):
        ctype = chess & Chess.CMASK
        if ctype in {Chess.ROOK, Chess.KNIGHT, Chess.CANNON}:
            return True
        if Chess.is_red(chess):
            pos = (8 - pos[0], 9 - pos[1])

        if ctype == Chess.KING:
            return self.validateKing(pos)
        if ctype == Chess.ADVISOR:
            return self.validateAdvisor(pos)
        if ctype == Chess.BISHOP:
            return self.validateBishop(pos)
        if ctype == Chess.PAWN:
            return self.validatePawn(board, pos, chess)
        return False
Exemple #3
0
    def get_method(self, board, fpos, tpos, standard=False):
        chess = board[fpos]

        name = self.get_name(chess, standard)
        pos = self.get_pos(chess, fpos, standard)
        action = self.get_action(chess, fpos, tpos, standard)

        method = f'{name}{pos}{action}'

        if Chess.chess(chess) not in {
                Chess.ROOK, Chess.KNIGHT, Chess.CANNON, Chess.PAWN
        }:
            return method

        chesses = self.get_column_chess(board, fpos)
        if len(chesses) == 1:
            return method

        # 同一列棋子多于一个
        if Chess.is_black(chess):
            chesses.reverse()

        if fpos == chesses[0]:
            pos = '前'
        else:
            pos = '后'

        method = f'{pos}{name}{action}'

        if Chess.chess(chess) in {Chess.ROOK, Chess.KNIGHT, Chess.CANNON}:
            return method

        # 以下为卒的情况

        wheres = np.argwhere(board == chess)

        columns = {}

        for where in wheres:
            columns.setdefault(where[0], 0)
            columns[where[0]] += 1

        for var in list(columns.keys()):
            if columns[var] == 1:
                del columns[var]

        if len(columns) == 1:

            idx = 1
            for where in chesses:
                if fpos == where:
                    break
                idx += 1

            if idx == 1:
                pos = '前'
            elif idx == 2 and len(chesses) == 3:
                pos = '中'
            else:
                pos = '后'

            return f'{pos}{name}{action}'

        # 两列的情况
        rangex = list(range(9))
        if Chess.is_red(chess):
            rangex.reverse()

        rangey = list(range(10))
        if Chess.is_black(chess):
            rangey.reverse()

        index = 0
        for x in rangex:
            if x not in columns:
                continue
            for y in rangey:
                pos = (x, y)
                if board[pos] != chess:
                    continue
                index += 1
                if fpos != pos:
                    continue
                pos = self.CNUMS[index]
                return f'{pos}{name}{action}'

        # 理论上不可能到这里
        raise Exception('格式化着法失败')