/** This uses Frink's constraint solver to solve a Sudoku problem. See constraintSudokuTest.frink for examples of calling it. */ class Sudoku { // Any character other than [1-9] can be used for the "placeholder" // character. class solveFromArray[array] := { solver = newJava["frink.constraint.ConstraintSolver"] // Make a 2-D array of variables with values 1 to 9. q = new array[[9,9], {|row,col,solver| solver.makeIntRangeVariable[1,9]}, solver] for row = 0 to 8 // Rows all different solver.allDifferent[toVector[q@row]] for col = 0 to 8 // Cols all different solver.allDifferent[toVector[q.getColumn[col]]] for rBlock = 0 to 2 // 3x3 blocks all different for cBlock = 0 to 2 { v = newJava["java.util.Vector"] for r = 0 to 2 for c = 0 to 2 v.addElement[q@(rBlock*3+r)@(cBlock*3+c)] solver.allDifferent[v] } // Parse input for row=0 to 8 for col=0 to 8 { c = substrLen[array@row, col, 1] if c >= "1" and c <= "9" q@row@col.fix[parseInt[c]] } listener = newJava["frink.constraint.GridPrintingSolutionListener", [9,9]] //listener.setFirstOnly[true] // Comment this in to find only one solution. solver.solve[listener] } }