def createSingleConstraintPuzzle(possibleValues, numberOfCells, constraintType): puzzle = Puzzle(str(numberOfCells) + "Cell test puzzle with single constraint type", set(possibleValues)) cg = puzzle.addConstraintGroup("Group") constraint = cg.addConstraint(constraintType) for i in range(0,numberOfCells): cell = puzzle.grid.addCell() cg.addCell(cell) return puzzle
def createSingleConstraintPuzzle(possibleValues, numberOfCells, constraintType): puzzle = Puzzle(str(numberOfCells) + "Cell test puzzle with single constraint type", set(possibleValues)) cg = puzzle.addConstraintGroup("Group") constraint = cg.addConstraint(constraintType) for i in range(0, numberOfCells): cell = puzzle.grid.addCell() cg.addCell(cell) return puzzle
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
def createVerySimpleKakuro(): possibleValues = range(1, 10) puzzle = Puzzle("Very simple example Kakuro", set(possibleValues)) c1_1 = puzzle.grid.addCell(QtCore.QPoint(1, 1)) c1_2 = puzzle.grid.addCell(QtCore.QPoint(1, 2)) c2_1 = puzzle.grid.addCell(QtCore.QPoint(2, 1)) c2_2 = puzzle.grid.addCell(QtCore.QPoint(2, 2)) cg = puzzle.addConstraintGroup("Group 1") cg.addConstraint(UniqueValueConstraint) constraint = cg.addConstraint(TotalSumValueConstraint) constraint.setTotalValue(4) cg.addCell(c1_1) cg.addCell(c1_2) cg = puzzle.addConstraintGroup("Group 2") cg.addConstraint(UniqueValueConstraint) constraint = cg.addConstraint(TotalSumValueConstraint) constraint.setTotalValue(4) cg.addCell(c1_1) cg.addCell(c2_1) cg = puzzle.addConstraintGroup("Group 3") cg.addConstraint(UniqueValueConstraint) constraint = cg.addConstraint(TotalSumValueConstraint) constraint.setTotalValue(3) cg.addCell(c2_1) cg.addCell(c2_2) cg = puzzle.addConstraintGroup("Group 4") cg.addConstraint(UniqueValueConstraint) constraint = cg.addConstraint(TotalSumValueConstraint) constraint.setTotalValue(3) cg.addCell(c1_2) cg.addCell(c2_2) return puzzle
def createFutoshikiPuzzle(size): puzzle = Puzzle(str(size) + "x" + str(size) + " Futoshiki test puzzle", set(range(1, size + 1))) # create the grid for y in range(1, size + 1): for x in range(1, size + 1): puzzle.grid.addCell(QtCore.QPoint(x, y)) # create the basic constraints for x in range(0, size): row = puzzle.addConstraintGroup("Row " + str(x)) row.addConstraint(ExactCoverConstraint) column = puzzle.addConstraintGroup("Column " + str(x)) column.addConstraint(ExactCoverConstraint) for y in range(0, size): row.addCell(puzzle.grid.getCellFromSquareGrid(x, y)) column.addCell(puzzle.grid.getCellFromSquareGrid(y, x)) return puzzle
def createFutoshikiPuzzle(size): puzzle = Puzzle(str(size) + "x"+str(size)+" Futoshiki test puzzle", set(range(1, size+1))) # create the grid for y in range(1, size + 1): for x in range(1, size + 1): puzzle.grid.addCell(QtCore.QPoint(x,y)) # create the basic constraints for x in range(0, size): row = puzzle.addConstraintGroup("Row " + str(x)) row.addConstraint(ExactCoverConstraint) column = puzzle.addConstraintGroup("Column " + str(x)) column.addConstraint(ExactCoverConstraint) for y in range(0, size): row.addCell(puzzle.grid.getCellFromSquareGrid(x, y)) column.addCell(puzzle.grid.getCellFromSquareGrid(y, x)) return puzzle
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
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
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
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
def createVerySimpleKakuro(): possibleValues = range(1,10) puzzle = Puzzle("Very simple example Kakuro", set(possibleValues)) c1_1 = puzzle.grid.addCell(QtCore.QPoint(1,1)) c1_2 = puzzle.grid.addCell(QtCore.QPoint(1,2)) c2_1 = puzzle.grid.addCell(QtCore.QPoint(2,1)) c2_2 = puzzle.grid.addCell(QtCore.QPoint(2,2)) cg = puzzle.addConstraintGroup("Group 1") cg.addConstraint(UniqueValueConstraint) constraint = cg.addConstraint(TotalSumValueConstraint) constraint.setTotalValue(4) cg.addCell(c1_1) cg.addCell(c1_2) cg = puzzle.addConstraintGroup("Group 2") cg.addConstraint(UniqueValueConstraint) constraint = cg.addConstraint(TotalSumValueConstraint) constraint.setTotalValue(4) cg.addCell(c1_1) cg.addCell(c2_1) cg = puzzle.addConstraintGroup("Group 3") cg.addConstraint(UniqueValueConstraint) constraint = cg.addConstraint(TotalSumValueConstraint) constraint.setTotalValue(3) cg.addCell(c2_1) cg.addCell(c2_2) cg = puzzle.addConstraintGroup("Group 4") cg.addConstraint(UniqueValueConstraint) constraint = cg.addConstraint(TotalSumValueConstraint) constraint.setTotalValue(3) cg.addCell(c1_2) cg.addCell(c2_2) return puzzle
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