Prolog domino game

前端 未结 2 1578
借酒劲吻你
借酒劲吻你 2021-01-07 06:55

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

2条回答
  •  遥遥无期
    2021-01-07 07:38

    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.
    

提交回复
热议问题