用prolog解填字游戏

一道常见的填字游戏题目,题目如下:
用prolog解填字游戏
在上面的白色方框内填入适合的单词,可供选择的单词有:dog,run,top,five,four,lost,mess,unit,baker,forum,green,super, prolog, vanish, wonder,yellow。

遵照prolog的原则,只要描述清楚整个问题就可以了,不需要去描述怎么解决问题。试着写了如下代码:
word(d,o,g). word(r,u,n).
word(t,o,p). word(f,i,v,e).
word(f,o,u,r). word(l,o,s,t).
word(m,e,s,s). word(u,n,i,t).
word(b,a,k,e,r). word(f,o,r,u,m).
word(g,r,e,e,n). word(s,u,p,e,r).
word(p,r,o,l,o,g). word(v,a,n,i,s,h).
word(w,o,n,d,e,r). word(y,e,l,l,o,w).
query:-
    word(L1,L2,L3,L4,L5),
    word(L9,L10,L11,L12,L13,L14),
    word(L1,L6,L9,L15),
    word(L3,L7,L11),
    word(L5,L8,L13,L16),
    writef('word1 is : %w%w%w%w%w\n', [L1, L2, L3, L4, L5]),
    writef('word2 is : %w%w%w%w%w%w\n', [L9,L10,L11,L12,L13,L14]),
    writef('word3 is : %w%w%w%w\n', [L1,L6,L9,L15]),
    writef('word4 is : %w%w%w\n', [L3,L7,L11]),
    writef('word5 is : %w%w%w%w\n', [L5,L8,L13,L16]). 

把代码保存为test.pl文件,命令行键入swipl打开swi-prolog的repl。然后加载test.pl文件,加载完毕后键入query. 神奇的答案就出来啦~~~。如图:
用prolog解填字游戏

你可能感兴趣的:(游戏,Prolog,逻辑编程)