Prolog 多米诺骨牌游戏 [英] Prolog domino game

查看:20
本文介绍了Prolog 多米诺骨牌游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在prolog 中制作一个游戏,使用给定的一组多米诺骨牌,它应该使用初始组中的所有骨牌组成正确的多米诺骨牌行.我们必须使用一个推理系统,我们必须在其中构建初始状态和最终状态,如下所示:

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

第一次过渡:

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

如何验证条件?

推荐答案

考虑这样的事情:

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

这篇关于Prolog 多米诺骨牌游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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