Prolog Domino Solution

2019-07-25 16:34发布

I need an algorithm that given a set of domino pieces, returns every possible end to the game.

I have already found this one, Prolog domino game, but it only adds pieces to the beggining of the set, so it doesn't give you every possible solution.

I replaced this [5-4, 4-3, 3-2, 2-1], with this [[5,4], [4,3], [3,2], [2,1]], and tried adding this line domino_order(In, X, [Out|[X,Y]]) :- select(Piece, In, Remaining), swap_or_not(Piece, [X,Y]), domino_order(Remaining, Y, Out)., but it doesn't work.

1条回答
来,给爷笑一个
2楼-- · 2019-07-25 17:07

writing down the detailed logic would lead to somewhat complex code. I suggest instead to have a quick check for validity, and let Prolog work out the insertion points.

domino :-
    Spare = [4-7,3-4], Curr = [1-2,2-3],
    domino_row_add_spare(Curr, Spare, R),
    writeln(R).

domino_row_add_spare(C, [], C).
domino_row_add_spare(C, Sps, U) :-
    append(L, R, C),
    select(X-Y, Sps, Rest),
    (append(L, [X-Y|R], C1) ; append(L, [Y-X|R], C1)),
    valid(C1),
    domino_row_add_spare(C1, Rest, U).

valid([_]).
valid([_-X,X-Y|R]) :- valid([X-Y|R]).
查看更多
登录 后发表回答