I'm making a sudoku solver in java, using a small prolog kb at it's core. The prolog "sudoku" rule requires a prolog list of lists. In java I have an int[][] with the sudoku values.
I've made the Query run succesfully with a prolog list of lists
e.g. Query q1 = new Query("problem(1, Rows), sudoku(Rows).");
where Rows
is a prolog list of lists,
but I need to also make it run with a Java int[][]
e.g. Query q1 = new Query("sudoku", intArrayTerm);
The relevant java code:
int s00 = parseTextField(t00);
int s01 = parseTextField(t01);
...
int s87 = parseTextField(t87);
int s88 = parseTextField(t88);
int[] row0 = {s00, s10, s20, s30, s40, s50, s60, s70, s80};
...
int[] row8 = {s08, s18, s28, s38, s48, s58, s68, s78, s88};
int[][] allRows = {row0, row1, row2, row3, row4, row5, row6, row7, row8};
Term rowsTerm = Util.intArrayArrayToList(allRows);
Query q0 = new Query("consult", new Term[]{new Atom("/home/mark/Documents/JavaProjects/SudokuSolver/src/com/company/sudoku.pl")});
System.out.println("consult " + (q0.hasSolution() ? "succeeded" : "failed"));
// Query q1 = new Query("problem(1, Rows), sudoku(Rows).");
Query q1 = new Query("sudoku", rowsTerm);
System.out.println("sudoku " + (q1.hasSolution() ? "succeeded" : "failed"));
Map<String, Term> rowsTermMap = q1.oneSolution();
Term solvedRowsTerm = (rowsTermMap.get("Rows"));
parseSolvedRowsTerm(solvedRowsTerm);
the prolog code:
sudoku(Rows) :-
length(Rows, 9), maplist(same_length(Rows), Rows),
append(Rows, Vs), Vs ins 1..9,
maplist(all_distinct, Rows),
transpose(Rows, Columns),
maplist(all_distinct, Columns),
Rows = [A,B,C,D,E,F,G,H,I],
blocks(A, B, C), blocks(D, E, F), blocks(G, H, I).
blocks([], [], []).
blocks([A,B,C|Bs1], [D,E,F|Bs2], [G,H,I|Bs3]) :-
all_distinct([A,B,C,D,E,F,G,H,I]),
blocks(Bs1, Bs2, Bs3).
problem(1, [[_,_,_, _,_,_, _,_,_],
[_,_,_, _,_,3, _,8,5],
[_,_,1, _,2,_, _,_,_],
[_,_,_, 5,_,7, _,_,_],
[_,_,4, _,_,_, 1,_,_],
[_,9,_, _,_,_, _,_,_],
[5,_,_, _,_,_, _,7,3],
[_,_,2, _,1,_, _,_,_],
[_,_,_, _,4,_, _,_,9]]).
the functions parseTextField
and parseSolvedRowsTerm
, actually the whole program, works fine with the commented-out Query q1
, but not with the not-commented-out Query q1