有没有更好的方法来搜索相对于方向的numpy数组?

时间:2019-10-24 13:45:53

标签: python arrays numpy search

我发现此方法未检测到从右到左的对角线,而且如果将零件定向为三角形,则似乎该功能将错误地检测成功。只能做直线

我试图有效地搜索numpy数组,这是关于方向的游戏板。如果无法找到满足目标的目标,我们将每个方向都作为深度时间进行迭代,然后在相反的方向上进行搜索。如果失败了,那么我们尝试一个新的方向

def directional_search(self, player, board, bound_x, bound_y, depth):

    ## depth parameter specifies how many times we must get a match before we can declare success

    ### This function searches a game board in a direction until it finds the piece if
    ## immediately if we fail to find the piece we are looking for or hit an edge then we go to our original position

    directions = [0, 1, 1, 0, 1, 1]

    ##### TEST for functionality in diagonal

    ## each row is now a direction
    directions = np.array(directions).reshape(3, 2)

    np.random.shuffle(directions)

    for z in range(len(directions)):

        Y, X = self.convert_point(a=int(player.action), bound_x=bound_x, bound_y=bound_y)

        count_piece = 0

        if Y + directions[z][0] < bound_y and X + directions[z][1] < bound_x:

            for x in range(depth):

                Y = Y + directions[z][0]
                X = X + directions[z][1]


                # NOTE: TO SELF check if Y and X represent an actual point on the board before checking if the piece
                ### If we fall off the edge no reason in continuing to search

                if Y > bound_y - 1 or X > bound_x - 1:
                    break

                if not board[Y][X] == player.piece:
                    break
                else:
                    count_piece = count_piece + 1

                if count_piece == depth:
                    return 1

        #### Searching in the opposite drection
        Y, X = self.convert_point(a=int(player.action), bound_x=bound_x, bound_y=bound_y)

        if Y - directions[z][0] > -1 and X - directions[z][1] > -1:

            for x in range(depth):
                Y = Y - directions[z][0]
                X = X - directions[z][1]

                if Y < -1 or X < -1:
                    break

                if not board[Y][X] == player.piece:
                    break
                else:
                    count_piece = count_piece + 1

                if count_piece == depth:
                    return 1

    return 0

能够在满足深度条件的方向上进行搜索。我希望该功能只能检测到同一块的直线(垂直,水平,对角线)

0 个答案:

没有答案