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


Python Canvas.transform方法代码示例

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


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

示例1: GridSolver

# 需要导入模块: from canvas import Canvas [as 别名]
# 或者: from canvas.Canvas import transform [as 别名]
class GridSolver(object):
    """
    Solves nonogram puzzle based on input provided

    Example:
    from gridsolver import GridSolver
    g = GridSolver({'rows':[[1,2], [4], [3], [1,1]], 'cols':[[4], [2], [3], [2,1]]})
    g.solve()
    print g.grid.show()
    rows = [[1,2], [4], [3], [1,1]]
    cols = [[4], [2], [3], [2,1]]
    X-XX
    XXXX
    XXX-
    X--X
    """
    grid = Canvas()
    input = {'rows':[[]], 'cols':[[]]}  # no zeroes (can ensure this in a method)
    rows = [[]]
    cols = [[]]
    width = 0
    height = 0
    unknown_char = 'O'
    dot_char = '-'
    fill_char = 'X'
    max_iterations = 10
    
    def __init__(self, input = {'rows':[[]], 'cols':[[]]}, max_iterations=10):
        self.cols = input['cols']
        self.width = len(self.cols)
        self.rows = input['rows']
        self.height = len(self.rows)
        self.grid = Canvas(self.width, self.height, self.unknown_char)
        self.input = input
        self.max_iterations = max_iterations
    
    def fill_grid_pixel(self, x, y):
        self.grid.bitmap[x][y] = self.fill_char
    
    def dot_grid_pixel(self, x, y):
        self.grid.bitmap[x][y] = self.dot_char
    
    def is_grid_filled(self, x, y):
        return self.grid.bitmap[x][y] == self.fill_char
    
    def is_grid_dotted(self, x, y):
        return self.grid.bitmap[x][y] == self.dot_char
    
    def is_grid_unknown(self, x, y):
        return self.grid.bitmap[x][y] == self.unknown_char   # = not (self.is_grid_dotted(x, y) or self.is_grid_filled(x, y))
    
    def is_grid_complete(self):
        return not True in map(lambda col: self.unknown_char in col, self.grid.bitmap)
    
    def is_grid_row_complete(self, row_index):
        # checks if no unknowns in row
        return not self.unknown_char in self.grid.get_row(row_index)
    
    def is_grid_col_complete(self, col_index):
        # checks if no unknowns in col
        return not self.unknown_char in self.grid.get_col(col_index)
    
    def solve(self, max_iterations=10):
        self.max_iterations = max_iterations
        iterations = 0
        for n in range(0, self.max_iterations):
            # print n
            for col_index in range(self.width):
                #if not self.is_grid_col_complete(col_index):
                self.grid.bitmap[col_index] = self.solve_col(col_index)
            for row_index in range(self.height):
                #if not self.is_grid_row_complete(row_index):
                for col_index, pixel in enumerate(self.solve_row(row_index)):
                    if self.is_grid_unknown(col_index, row_index):
                        self.grid.colour_pixel(col_index, row_index, pixel)
            if self.is_grid_complete():
                iterations = n + 1
                break
        # print iterations
        return self.grid.transform()

    def solve_row(self, row_index):
        return self.solve_line(row_index, True)

    def solve_col(self, col_index):
        return self.solve_line(col_index, False)
        
    def solve_line(self, line_index, is_row_not_col):
        # check all combos in this line
        if is_row_not_col:
            combos = self.calculate_combos(self.rows[line_index], self.width)
        else:
            combos = self.calculate_combos(self.cols[line_index], self.height)
        cross_combo = None          # create cross referenced combo
        for combo in combos:
            # first loop to ignore combos that do not fit with canvas
            bad_combo = False
            for opposing_index, pixel in enumerate(combo):
                col_index, row_index = col_row_index(line_index, opposing_index, is_row_not_col)
                if (pixel == self.fill_char and self.is_grid_dotted(col_index, row_index)) or \
#.........这里部分代码省略.........
开发者ID:tomkingston,项目名称:nonosolver,代码行数:103,代码来源:gridsolver.py


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