Ejemplo n.º 1
0
 def get_pos(self, chess, pos, standard=False):
     if Chess.is_black(chess):
         num = pos[0] + 1
     else:
         num = 9 - pos[0]
     if standard and Chess.is_black(chess):
         return self.ANUMS[num]
     return self.CNUMS[num]
Ejemplo n.º 2
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}'
Ejemplo n.º 3
0
    def validatePawn(self, board, pos, chess):
        if pos[1] > 4:
            return True
        if pos[1] < 3:
            return False
        if pos[0] % 2 != 0:
            return False

        if Chess.is_black(chess):
            if board[(pos[0], 3)] == chess:
                return False
            if board[(pos[0], 4)] == chess:
                return False
        else:
            if board[(8 - pos[0], 5)] == chess:
                return False
            if board[(8 - pos[0], 6)] == chess:
                return False

        return True
Ejemplo n.º 4
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('格式化着法失败')