コード例 #1
0
    def parsePuzzle(filename):

        start = time()
        #puzzle = PuzzleFactory.createExampleKakuro()

        # Set the initial data to work with
        myFile = open(filename)

        values = range(1, 10)
        stringArray = myFile.readlines()
        y = 0
        for line in stringArray:
            if (line.startswith("range")):
                min, max = [
                    int(x) for x in line[line.find("=") + 1:].split(",")
                ]
                puzzle = Puzzle("Test-kakuro", range(min, max + 1))

            if (line.startswith("cell")):
                x, y = [
                    int(x)
                    for x in line[line.find("(") + 1:line.find(')')].split(",")
                ]
                puzzle.getGrid().addCell(QtCore.QPoint(x, y))

            if (line.startswith("total")):
                value = int(line.split("=")[-1])
                cells = [
                    cell[1:-1].split(",")
                    for cell in line[line.find("(") +
                                     1:line.rfind(")")].split(";")
                ]

                cg = puzzle.addConstraintGroup("Line")
                for cell in cells:
                    try:
                        c = puzzle.getGrid().searchCellWithPosition(
                            QtCore.QPoint(int(cell[0]), int(cell[1])))
                    except ValueError:
                        print line
                    cg.addCell(c)

                uvc = cg.addConstraint(UniqueValueConstraint)
                tsvc = cg.addConstraint(TotalSumValueConstraint)

                tsvc.setTotalValue(value)

        solver = Solver(puzzle)

        return puzzle, solver
コード例 #2
0
    def parsePuzzle(filename):

        # Set the initial data to work with
        myFile = open("./simpleSumbrero.txt")

        values = range(1, 10)
        stringArray = myFile.readlines()
        y = 0
        for line in stringArray:
            line = line.strip()  # strip off whitespace

            if (line.startswith("range")):
                min, max = [
                    int(x) for x in line[line.find("=") + 1:].split(",")
                ]
                puzzle = Puzzle("Test-sumbrero", range(min, max + 1))

            if (line.startswith("cell")):
                x, y = [
                    float(x)
                    for x in line[line.find("(") + 1:line.find(')')].split(",")
                ]
                puzzle.getGrid().addCell(QtCore.QPoint(x, y))

            if (line.startswith("column")):
                column, value = line.split('=')
                column = int(column.split('(')[1].split(')')[0])

                # create a sum constraint
                cg = puzzle.addConstraintGroup("Column sum " + str(column))
                tsvc = cg.addConstraint(TotalSumValueConstraint)
                uvc = cg.addConstraint(UniqueValueConstraint)

                for cell in puzzle.grid.cells:
                    if (cell.position.x() == column):
                        cg.addCell(cell)

                tsvc.setTotalValue(int(value))
            # TODO: deal with the two different kinds of row sums

        solver = Solver(puzzle)

        return puzzle, solver
コード例 #3
0
    def parsePuzzle(filename):
            
        start = time()
        #puzzle = PuzzleFactory.createExampleKakuro()

        # Set the initial data to work with
        myFile = open(filename)

        values = range(1,10)
        stringArray = myFile.readlines()
        y = 0
        for line in  stringArray:
            if(line.startswith("range")):
                min, max = [int(x) for x in line[line.find("=")+1:].split(",")]
                puzzle = Puzzle("Test-kakuro", range(min, max+1))
                
            if(line.startswith("cell")):
                x,y = [int(x) for x in line[line.find("(")+1:line.find(')')].split(",")]
                puzzle.getGrid().addCell(QtCore.QPoint(x,y))
                
            if(line.startswith("total")):
                value = int(line.split("=")[-1])
                cells = [cell[1:-1].split(",") for cell in line[line.find("(")+1:line.rfind(")")].split(";")]
                
                cg = puzzle.addConstraintGroup("Line")
                for cell in cells:
                    try:
                        c = puzzle.getGrid().searchCellWithPosition(QtCore.QPoint(int(cell[0]), int(cell[1])))
                    except ValueError:
                        print line
                    cg.addCell(c)
                
                uvc = cg.addConstraint(UniqueValueConstraint)
                tsvc = cg.addConstraint(TotalSumValueConstraint)
                
                tsvc.setTotalValue(value)
                
        solver = Solver(puzzle)

        return puzzle, solver
コード例 #4
0
    def parsePuzzle(filename):

        # Set the initial data to work with
        myFile = open("./simpleSumbrero.txt")


        values = range(1,10)
        stringArray = myFile.readlines()
        y = 0
        for line in stringArray:
            line = line.strip() # strip off whitespace
            
            if(line.startswith("range")):
                min, max = [int(x) for x in line[line.find("=")+1:].split(",")]
                puzzle = Puzzle("Test-sumbrero", range(min, max+1))
                
            if(line.startswith("cell")):
                x,y = [float(x) for x in line[line.find("(")+1:line.find(')')].split(",")]
                puzzle.getGrid().addCell(QtCore.QPoint(x,y))
            
            if(line.startswith("column")):
                column, value = line.split('=')
                column = int(column.split('(')[1].split(')')[0])
                
                # create a sum constraint
                cg = puzzle.addConstraintGroup("Column sum " + str(column))
                tsvc = cg.addConstraint(TotalSumValueConstraint)
                uvc = cg.addConstraint(UniqueValueConstraint)
                
                for cell in puzzle.grid.cells:
                    if(cell.position.x() == column):
                        cg.addCell(cell)

                tsvc.setTotalValue(int(value))
            # TODO: deal with the two different kinds of row sums
            
        solver = Solver(puzzle)

        return puzzle, solver
コード例 #5
0
    def createEmptySudoku():
        values = set(range(1, 10))

        blocks = []
        rows = []
        columns = []

        sudoku = Puzzle("Sudoku", values)
        grid = sudoku.getGrid()

        for i in range(0, 9):
            cg = sudoku.addConstraintGroup("Block " + str(i + 1))
            cg.addConstraint(ExactCoverConstraint)
            blocks.append(cg)

            cg = sudoku.addConstraintGroup("Row " + str(i + 1))
            cg.addConstraint(ExactCoverConstraint)
            rows.append(cg)

            cg = sudoku.addConstraintGroup("Column " + str(i + 1))
            cg.addConstraint(ExactCoverConstraint)
            columns.append(cg)

        # Initialize data structures (sudoku)
        for i in range(0, 81):
            x = i % 9
            y = int(floor(i / 9))
            block = int(floor(x / 3) + 3 * floor(y / 3))
            # print x,y, block

            cell = grid.addCell(QtCore.QPoint(x + 1, y + 1))
            # add to relevant block
            cell.addConstraintGroup(blocks[block])
            # add to relevant row
            cell.addConstraintGroup(rows[y])
            # add to relevant column
            cell.addConstraintGroup(columns[x])

        return sudoku
コード例 #6
0
 def createEmptySudoku():
     values = set(range(1,10))
     
     blocks = []
     rows = []
     columns = []
     
     sudoku = Puzzle("Sudoku", values)
     grid = sudoku.getGrid()
     
     for i in range(0,9):
         cg = sudoku.addConstraintGroup("Block " + str(i+1))
         cg.addConstraint(ExactCoverConstraint)
         blocks.append(cg)
     
         cg = sudoku.addConstraintGroup("Row " + str(i+1))
         cg.addConstraint(ExactCoverConstraint)
         rows.append(cg)
     
         cg = sudoku.addConstraintGroup("Column " + str(i+1))
         cg.addConstraint(ExactCoverConstraint)
         columns.append(cg)
         
     # Initialize data structures (sudoku)
     for i in range(0,81):
         x = i% 9
         y = int(floor(i/9))
         block = int(floor(x/3) + 3*floor(y/3))
         #print x,y, block
         
         cell = grid.addCell(QtCore.QPoint(x+1,y+1))
         # add to relevant block
         cell.addConstraintGroup(blocks[block])
         # add to relevant row
         cell.addConstraintGroup(rows[y])
         # add to relevant column
         cell.addConstraintGroup(columns[x])
         
     return sudoku