1

我正在用java制作一个数独求解器,它的核心是一个小的prolog kb。序言“数独”规则需要序言列表。在 java 中,我有一个带有数独值的 int[][]。

我已经使用列表的序言列表成功运行查询,例如列表的序言列表Query q1 = new Query("problem(1, Rows), sudoku(Rows).");在哪里Rows,但我还需要使用 Java int[][] 使其运行,例如Query q1 = new Query("sudoku", intArrayTerm);

相关的java代码:

    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);

序言代码:

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]]).  

函数parseTextFieldand parseSolvedRowsTerm,实际上是整个程序,在注释掉的情况下可以正常工作Query q1,但在未注释掉的情况下则不行Query q1

4

1 回答 1

0

解决了!在 github.com/zlumyo 的 Credits 中添加了一个额外的参数,q1 为了方便,我偷了他的 BuildMatrix,他的代码给了我额外参数的想法。

Query q1 = new Query("sudoku("+ buildMatrix(allRows) +", Result)");

'Buildmatrix' 基本上只是一个 StringBuilder 辅助函数:

private String buildMatrix(int[][] cells) { // build matrix as string
    StringBuilder result = new StringBuilder("[");

    ArrayList<String> strList = new ArrayList<>();
    for (int[] i : cells) {
        strList.add(buildList(i));
    }

    result.append(String.join(",", strList));

    result.append("]");

    return result.toString();
}

private String buildList(int[] line) { // build matrix as string
    StringBuilder result = new StringBuilder("[");

    ArrayList<String> intList = new ArrayList<>();
    for (int i : line) {
        String stringval;

        if(i == 0){
            stringval = "_";
        }else{
            stringval = String.valueOf(i);
        }   // if statement is a small adaptation to the version github.com/zlumyo made, because my prolog sudoku had a slightly different format for the list.

        intList.add(stringval);
    }

    result.append(String.join(",", intList));

    result.append("]");

    return result.toString();
}

序言代码没有太大变化,只是一个额外的参数和一个额外的行。

sudoku(Rows, Result) :-
    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),
    Rows = Result. %extra line
于 2019-02-18T14:45:12.200 回答