Prolog多米诺骨牌游戏

时间:2011-06-22 17:06:16

标签: prolog

我正在使用一组给定的多米诺骨牌片进行prolog游戏,它应该使用初始集中的所有片段制作正确的多米诺骨牌行。我们必须使用一个推理系统,我们必须在这个系统中构建初始状态和最终状态:

       initial(dominos([[1,4],[2,3],[4,2]],[])).
         final(dominos([],[[1,4],[4,2],[2,3]])).

第一次转换只是从第一个列表中选择一个pice并将其放入第二个列表,但是下一个转换应该验证第二个数字是否与要放入的第一个数字相匹配。我想我有第二个转变的头脑

第一次过渡:

transition(dominos(L,[]),A,dominos(L1,[A])):- select(A,L,L1). 

第二次转型:

transition(dominos(L,B),A,dominos(L1,[[X,Y]|[Y,_])):- select(B,L,L1).

如何验证条件?

2 个答案:

答案 0 :(得分:4)

考虑这样的事情:

% L1 is a list of domino pieces (of the form X-Y)
% L2 is L1 in domino order
domino_order(L1, L2) :-
    domino_order(L1, _, L2).

domino_order([], _, []) :- !.
domino_order(In, X, [X-Y | Out]) :-
    select(Piece, In, Remaining),
    swap_or_not(Piece, X-Y),
    domino_order(Remaining, Y, Out).

swap_or_not(X-Y, X-Y).
swap_or_not(X-Y, Y-X).

用法:

?- domino_order([5-4, 1-2, 4-3, 2-3], Out).
Out = [5-4, 4-3, 3-2, 2-1] ;
Out = [1-2, 2-3, 3-4, 4-5] ;
false.

答案 1 :(得分:0)

domino(X) :tasselli(L), member(T,L), componibile(X,L,[T]).
componibile(X,L,X) :length(L,N), length(X,N).
componibile(X,L,A) :member(T,L),
                    \+(member(T,A)),
                    T = t(_,N2),
                    A = [t(N2,_)|_],
                    componibile(X,L,[T|A]).

所以如果你有:

tasselli([T(3,4),T(5,3),T(4,1),T(1,5)])。

然后结果将是:

?多米诺(X)。

X = [t(4,1),t(1,5),t(5,3),t(3,4)];

X = [t(3,4),t(4,1),t(1,5),t(5,3)];

X = [t(1,5),t(5,3),t(3,4),t(4,1)];

X = [t(5,3),t(3,4),t(4,1),t(1,5)];

假。

相关问题