예제 #1
0
    def _step(self, solutions, depth):
        if depth == 0:
            return solutions
        else:
            next_solutions = []
            for score, moves, board, row, column in solutions:
                for delta_r, delta_c in self._swaps(board, row, column):
                    swapped_board = Board.copy_board(board).swap(row, column, row + delta_r, column + delta_c)
                    if self._remember(swapped_board):
                        # only add move to solutions if it is a board layout that has not been seen before
                        next_solutions.append((score + self._score(swapped_board), (moves + ((delta_r, delta_c),)), swapped_board, row + delta_r, column + delta_c))

            if next_solutions:
                # prune solutions down before recursing to next depth
                return self._step(self._prune(next_solutions), depth - 1)
            else:
                return solutions
예제 #2
0
    def _step(self, score, moves, board, row, column, depth):
        if depth == 0:
            return (score, moves, board)
        else:
            # get possible swaps from current row, column position
            swaps = self._swaps(board, row, column)

            solutions = []
            for delta_r, delta_c in swaps:
                swapped_board = Board.copy_board(board).swap(
                    row, column, row + delta_r, column + delta_c)
                if self._remember(swapped_board):
                    # only add move to solutions if it is a board layout that has not been seen before
                    solutions.append((self._score(swapped_board),
                                      swapped_board, (delta_r, delta_c)))

            if solutions:
                # calculate score on all possible swaps and order them from highest to lowest. recurse into the highest one
                best = max(solutions, key=lambda x: x[0])
                return self._step(score + best[0], (moves + (best[2], )),
                                  best[1], row + best[2][0],
                                  column + best[2][1], depth - 1)
            else:
                return (score, moves, board)