Prolog domino game

梦想与她 提交于 2020-01-29 09:51:53

问题


i'm making a game in prolog, with a given set of domino pieces, it should make a correct domino row using all the pieces in the initial set. we must use an inference system in which we must build the initial state and the final state like this:

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

the first transtion is just pick one pice from the 1st list and put it into the 2nd list, but the next ones should verify if the 2nd number matches the 1st number of the piece to be put. i think i have the 2nd transition's head

first transition:

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

second transition:

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

how do i verify the conditions?


回答1:


Consider something like this:

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

Usage:

?- 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.



回答2:


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

So if you have:

tasselli([t(3,4),t(5,3),t(4,1),t(1,5)]).

Then the result will be:

?domino(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)] ;

false.



来源:https://stackoverflow.com/questions/6443727/prolog-domino-game

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!