Strimko Game : B-Prolog to SWI Prolog - problem with function.

105 views
Skip to first unread message

kabaret10...@gmail.com

unread,
Nov 28, 2018, 4:25:06 PM11/28/18
to SWI-Prolog
Hello there,
I'd like to make GU(with XPCE library) in  program made by  Hakan Kjellerstrand which is a strimko game.
I've got some problem...
f.e. how to replace function foreach - SWI prolog can handle only 2 parameters in this function.
here's the code


go :-
        strimko(2).

go2 :-
        strimko(2),
        strimko(67),
        strimko(68),
        strimko(69),
        strimko(70).


strimko(P) :-
        format('\nProblem ~w\n',[P]),
        problem(P,N,Streams,Placed),

        new_array(X,[N,N]),
        array_to_list(X,XList),
        XList :: 1..N,

        % is a latin square
        alldifferent_matrix(X),

        % streams
        foreach(S in 1..N,
                [Positions],
                (Positions @= [X[I,J] : I in 1..N, J in 1..N, S =:= Streams[I,J]],
                 alldifferent(Positions)
                )
               ),
        

        % placed
        foreach(I in 1..Placed^length, 
                (X[ Placed[I,1], Placed[I,2] ] #= Placed[I,3])),

        labeling(XList),
        pretty_print(X),
        nl.


pretty_print(X) :-
        N @= X^length,
        foreach(I in 1..N,
                (
                    foreach(J in 1..N, format(' ~d ', [X[I,J]])),
                    nl
                )),
        nl.



%
% Ensure a Latin square, 
% i.e. all rows and all columns are different
%
alldifferent_matrix(Board) :-
        Rows @= Board^rows,
        foreach(Row in Rows, alldifferent(Row)),
        Columns @= Board^columns,
        foreach(Column in Columns, alldifferent(Column)).


%
% Strimko Monthly #02

%
problem(2,7,Streams, Placed) :-
    Streams = [[1,1,2,2,2,2,2],
               [1,1,2,3,3,3,2],
               [1,4,1,3,3,5,5],
               [4,4,3,1,3,5,5],
               [4,6,6,6,7,7,5],
               [6,4,6,4,5,5,7],
               [6,6,4,7,7,7,7]],
    Placed =  [[2,1,1],
               [2,3,7],
               [2,5,6],
               [2,7,4],
               [3,2,7],
               [3,6,1],
               [4,1,4],
               [4,7,5],
               [5,2,2],
               [5,6,6]].

% 
% Strimko Weekly Set 067

problem(67,5,Streams, Placed) :-
 Streams = [[1,1,1,2,3],
             [1,2,2,2,3],
             [1,2,4,5,3],
             [5,4,5,4,3],
             [4,5,5,4,3
             ]],
 Placed = [[1,3,4],
           [1,4,1],
           [3,3,2],
           [3,5,3],
           [5,4,5]].

%
% Strimko Weekly Set 068

problem(68,4,Streams,Placed) :-

Streams = [[1,2,2,4],
           [2,1,4,2],
           [3,4,1,3],
           [4,3,3,1]],
Placed =    [[2,2,3],
             [2,3,2],
             [3,3,1]].


% Strimko Weekly Set 069

problem(69,6,Streams,Placed) :-
   Streams = [[1,2,3,3,3,4],
              [2,1,3,5,4,3],
              [2,1,3,5,5,4],
              [2,6,1,6,5,4],
              [2,6,1,6,4,5],
              [6,2,6,1,5,4]],
   Placed =  [[2,2,4],
              [2,3,1],
              [2,4,3],
              [2,5,2],
              [3,2,1],
              [3,5,6],
              [4,3,5],
              [4,4,2]].

% Strimko Weekly Set 070

problem(70,5,Streams,Placed) :-
Streams =  [[1,2,3,3,3],
            [2,1,1,3,1],
            [2,2,3,1,4],
            [5,2,5,4,4],
            [5,5,5,4,4]],
Placed =   [[1,1,1],
            [2,5,4],
            [4,1,2],
            [5,4,5]].

Have you got any ideas?

Reply all
Reply to author
Forward
0 new messages