如何通过序言中的输入来填充谓词的参数? [英] How to fill in the parameters of predicates by input in prolog?

查看:83
本文介绍了如何通过序言中的输入来填充谓词的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

size(5).
black(1,3).
black(2,3).
black(3,2).
black(4,3).
black(5,1).
black(5,5).

words([do,ore,ma,lis,ur,as,po, so,pirus, oker,al,adam, ik]) .

:- use_module(library(lists),[nth1/3, select/3]).

crossword(Puzzle) :-
    words(WordList),
    word2chars(WordList,CharsList),
    make_empty_words(EmptyWords) ,
    fill_in(CharsList,EmptyWords),
    word2chars(Puzzle,EmptyWords).

word2chars([],[]).    
word2chars([Word|RestWords] ,[Chars|RestChars] ) :-
   atom_chars(Word,Chars),
   word2chars(RestWords,RestChars).

fill_in([],[]).
fill_in([Word|RestWords],Puzzle) :-
   select(Word,Puzzle,RestPuzzle),
   fill_in(RestWords,RestPuzzle).

make_empty_words(EmptyWords) :-
   size(Size),
   make_puzzle(Size,Puzzle),
   findall(black(I,J),black(I,J),Blacks) ,
   fillblacks(Blacks,Puzzle),
   empty_words(Puzzle,EmptyWords).

make_puzzle(Size,Puzzle) :-
   length(Puzzle,Size),
   make_lines(Puzzle,Size).

make_lines([],_).
make_lines([L|Ls],Size) :-
   length(L,Size),
   make_lines(Ls,Size).

fillblacks([],_).
fillblacks([black(I,J)|Blacks],Puzzle) :-
   nth1(I,Puzzle,LineI),
   nth1(J,LineI,black),
   fillblacks(Blacks,Puzzle).

empty_words(Puzzle,EmptyWords) :-
   empty_words(Puzzle,EmptyWords,TailEmptyWords),
   size(Size),
   transpose(Size,Puzzle,[],TransposedPuzzle),
   empty_words(TransposedPuzzle,TailEmptyWords,[] ).

empty_words([],Es,Es).
empty_words([L|Ls],Es,EsTail) :-
   empty_words_on_one_line(L,Es,Es1) ,
   empty_words(Ls,Es1,EsTail).

empty_words_on_one_line([], Tail, Tail).
empty_words_on_one_line([V1,V2|L],[[V1,V2|Vars]|R],Tail) :-
   var(V1), var(V2), !,
   more_empty(L,RestL,Vars),
   empty_words_on_one_line(RestL,R,Tail) .
empty_words_on_one_line([_| RestL],R, Tail) :-
    empty_words_on_one_line(RestL,R,Tail) .

more_empty([],[],[]).
more_empty([V|R],RestL,Vars) :-
   (  var(V) ->
      Vars = [V|RestVars],
      more_empty(R,RestL,RestVars)
   ;
      RestL = R,
      Vars = []
   ).

transpose(N,Puzzle,Acc,TransposedPuzzle) :-
   (  N == 0 ->
      TransposedPuzzle = Acc
   ;
      nth_elements(N,Puzzle,OneVert),
      M is N - 1,
      transpose(M,Puzzle,[OneVert|Acc], TransposedPuzzle)
   ).

nth_elements(_,[],[]).
nth_elements(N,[X|R],[NthX| S]) :-
   nth1(N,X,NthX),
   nth_elements(N,R,S).

它用于解决这样的填字游戏:

It is used for solving a crossword like this:

代码中默认设置了黑角位置,但是当我想查询填字游戏时,我想找到一种通过输入来给黑角位置的方法.

The black squares places are given by default in the code but I want to find a way to give the black squares places by input when I want to query crossword.

类似这样的东西:

black(Y1,X1).
black(Y2,X2).
black(Y3,X3).
black(Y4,X4).
black(Y5,X5).
black(Y6,X6).

crossword(Puzzle,Y1,X1,Y2,X2,...) :-
    words(WordList),
    word2chars(WordList,CharsList),
    make_empty_words(EmptyWords,Size) ,
    fill_in(CharsList,EmptyWords),
    word2chars(Puzzle,EmptyWords).

推荐答案

正如@lurker所述,我尝试重写代码,并将黑色方块作为程序的输入,如下所示:

As @lurker mentioned I tried rewriting the code and giving the black squares as input to program as below:

:- use_module(library(lists),[nth1/3, select/3]).

crossword(Puzzle,Size,Blacks,WordList) :-
    word2chars(WordList,CharsList),
    make_empty_words(EmptyWords,Size,Blacks) ,
    fill_in(CharsList,EmptyWords),
    word2chars(Puzzle,EmptyWords).

word2chars([],[]).
word2chars([Word|RestWords] ,[Chars|RestChars] ) :-
    atom_chars(Word,Chars),
    word2chars(RestWords,RestChars).

fill_in([],[]).
fill_in([Word|RestWords],Puzzle) :-
    select(Word,Puzzle,RestPuzzle),
    fill_in(RestWords,RestPuzzle).

make_empty_words(EmptyWords,Size,Blacks) :-
    make_puzzle(Size,Puzzle),
    fillblacks(Blacks,Puzzle),
    empty_words(Puzzle,EmptyWords).

make_puzzle(Size,Puzzle) :-
    length(Puzzle,Size),
    make_lines(Puzzle,Size).

make_lines([],_).
make_lines([L|Ls],Size) :-
    length(L,Size),
    make_lines(Ls,Size).

fillblacks([],_).   
fillblacks([black(I,J)|Blacks],Puzzle) :-
    nth1(I,Puzzle,LineI),
    nth1(J,LineI,black),
    fillblacks(Blacks,Puzzle).

empty_words(Puzzle,EmptyWords) :-
    empty_words(Puzzle,EmptyWords,TailEmptyWords),
    transpose(Size,Puzzle,[],TransposedPuzzle),
    empty_words(TransposedPuzzle,TailEmptyWords,[] ).

empty_words([],Es,Es).
empty_words([L|Ls],Es,EsTail) :-
    empty_words_on_one_line(L,Es,Es1) ,
    empty_words(Ls,Es1,EsTail).

empty_words_on_one_line([], Tail, Tail).
empty_words_on_one_line([V1,V2|L],[[V1,V2|Vars]|R],Tail) :-
    var(V1), var(V2), !,
    more_empty(L,RestL,Vars),
    empty_words_on_one_line(RestL,R,Tail) .

empty_words_on_one_line([_| RestL],R, Tail) :-
    empty_words_on_one_line(RestL,R,Tail) .

more_empty([],[],[]).
more_empty([V|R],RestL,Vars) :-
    (   var(V)
    ->  Vars = [V|RestVars],
        more_empty(R,RestL,RestVars)
    ;   RestL = R,
        Vars = []
    ).

transpose(N,Puzzle,Acc,TransposedPuzzle) :-
    (   N == 0
    ->  TransposedPuzzle = Acc
    ;   nth_elements(N,Puzzle,OneVert),
        M is N - 1,
        transpose(M,Puzzle,[OneVert|Acc], TransposedPuzzle)
    ).

nth_elements(_,[],[]).
nth_elements(N,[X|R],[NthX| S]) :-
    nth1(N,X,NthX),
    nth_elements(N,R,S).

现在,通过以下输入,代码将返回谜题的答案:

now by the following input the code returns the answer to the puzzle:

crossword(Puzzle,5,[black(1,3),black(2,3),black(3,2),black(4,3),
black(5,1),black(5,5)],[do,ore,ma,lis,ur,as,pu, so,pirus, uker,al,adam, ik]).

输出将是:

Puzzle = [as,pu,do,ik,ore,ma,ur,lis,adam,so,al,pirus,uker] 

这篇关于如何通过序言中的输入来填充谓词的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆