Prolog 多米诺骨牌游戏

发布于 2024-11-16 14:07:32 字数 539 浏览 1 评论 0原文

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

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

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

第一个转换:

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

我如何验证条件?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人骨 2024-11-23 14:07:32

考虑这样的事情:

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

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.
十年九夏 2024-11-23 14:07:32
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]).

因此,如果您有:

tasseli([t(3,4),t(5,3),t(4,1),t(1,5)])。

那么结果将是:

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

错误的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文