当前位置: 首页>>代码示例>>Python>>正文


Python Board.swap_row方法代码示例

本文整理汇总了Python中Board.swap_row方法的典型用法代码示例。如果您正苦于以下问题:Python Board.swap_row方法的具体用法?Python Board.swap_row怎么用?Python Board.swap_row使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Board的用法示例。


在下文中一共展示了Board.swap_row方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: import Board [as 别名]
# 或者: from Board import swap_row [as 别名]
class Generator:

    # # constructor for generator, reads in a space delimited
    # def __init__(self, starting_file):

    #     # opening file
    #     f = open(starting_file)

    #     # reducing file to a list of numbers
    #     numbers = filter(lambda x: x in '123456789',list(reduce(lambda x,y:x+y,f.readlines())))
    #     print (numbers)
    #     numbers = map(int,numbers)
    #     ['1', '2', '3', '4', '5', '6', '7', '8', '9', '4', '5', '6', '7', '8', '9', '1', '2', '3', '7', '8', '9', '1', '2', '3', '4', '5', '6', '2', '1', '4', '3', '6', '5', '8', '9', '7', '3', '6', '5', '8', '9', '7', '2', '1', '4', '8', '9', '7', '2', '1', '4', '3', '6', '5', '5', '3', '1', '6', '4', '2', '9', '7', '8', '6', '4', '2', '9', '7', '8', '5', '3', '1', '9', '7', '8', '5', '3', '1', '6', '4', '2']

    #     # closing file
    #     f.close()

    #     # constructing board
    #     self.board = Board(numbers)

        # constructor for generator, reads in a space delimited
    def __init__(self):
        numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '4', '5', '6', '7', '8', '9', '1', '2', '3', '7', '8', '9', '1', '2', '3', '4', '5', '6', '2', '1', '4', '3', '6', '5', '8', '9', '7', '3', '6', '5', '8', '9', '7', '2', '1', '4', '8', '9', '7', '2', '1', '4', '3', '6', '5', '5', '3', '1', '6', '4', '2', '9', '7', '8', '6', '4', '2', '9', '7', '8', '5', '3', '1', '9', '7', '8', '5', '3', '1', '6', '4', '2']
        numbers = map(int,numbers)
        self.board = Board(numbers)

    # function randomizes an existing complete puzzle
    def randomize(self, iterations):

        # not allowing transformations on a partial puzzle
        if len(self.board.get_used_cells())==81:

            # looping through iterations
            for x in range(0, iterations):

                # to get a random column/row
                case = random.randint(0, 3)

                # to get a random band/stack
                block = random.randint(0, 2) * 3

                # in order to select which row and column we shuffle an array of
                # indices and take both elements
                options = range(0,3)
                random.shuffle(options)
                piece1, piece2 = options[0],options[1]

                # pick case according to random to do transformation
                if case == 0:
                    self.board.swap_row(block + piece1, block + piece2)
                elif case == 1:
                    self.board.swap_column(block + piece1, block + piece2)
                elif case == 2:
                    self.board.swap_stack(piece1, piece2)
                elif case == 3:
                    self.board.swap_band(piece1, piece2)
        else:
            raise Exception('Rearranging partial board may compromise uniqueness.')

    # method gets all possible values for a particular cell, if there is only one
    # then we can remove that cell
    def reduce_via_logical(self, cutoff = 81):
        cells = self.board.get_used_cells()
        random.shuffle(cells)
        for cell in cells:
                if len(self.board.get_possibles(cell)) == 1:
                    cell.value = 0
                    cutoff -= 1
                if cutoff == 0:
                    break

    # method attempts to remove a cell and checks that solution is still unique
    def reduce_via_random(self, cutoff=81):
        temp = self.board
        existing = temp.get_used_cells()

        # sorting used cells by density heuristic, highest to lowest
        new_set = [(x,self.board.get_density(x)) for x in existing]
        elements= [x[0] for x in sorted(new_set, key=lambda x: x[1], reverse=True)]

        # for each cell in sorted list
        for cell in elements:
            original = cell.value

            # get list of other values to try in its place
            complement = [x for x in range(1,10) if x != original]
            ambiguous = False

            # check each value in list of other possibilities to try
            for x in complement:

                # set cell to value
                cell.value = x

                # create instance of solver
                s = Solver(temp)

                # if solver can fill every box and the solution is valid then
                # puzzle becomes ambiguous after removing particular cell, so we can break out
                if s.solve() and s.is_valid():
#.........这里部分代码省略.........
开发者ID:locusxt,项目名称:MySudoku,代码行数:103,代码来源:Generator.py


注:本文中的Board.swap_row方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。