Есть ли лучший способ поиска массива по направлению? - PullRequest
0 голосов
/ 24 октября 2019

Я обнаружил, что диагональ, идущая справа налево, не обнаруживается этим методом, и кажется, что эта функция будет неправильно определять успех, если части ориентированы в треугольник.

будут делать только прямые линии. Я попытался эффективно найти массив 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

, чтобы иметь возможность искать направление, позволяющее всякий раз, когда выполняется условие для глубины. Я ожидаю, что функция будет обнаруживать только прямые линии (вертикальные, горизонтальные, диагональные) одного и того же куска вместе

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...