newby question - once more solitaire - infinite loop?

From: Matthias Hahn (hahn_at_ira.uka.de)
Date: 03/15/04


Date: 15 Mar 2004 00:18:17 -0800

Hi,

I know, there are solutions for the solitaire game in prolog.
But I'd like to know what I did wrong in following code. In
the marked position I get an infinite loop though I thought this
should not happen because of backtracking. But there are always
chosen the same Tokens. The starting point is: ?- res.
In the list 'Tokens' the actual Tokens on the field are stored.

Perhaps somebody can give me a hint.

Thanks in advance,

Matthias Hahn

 
% solitaire.pl

%definition of the neighboures
south(a2,b2).
south(a3,b3).
south(a4,b4).
south(b2,c2).
south(b3,c3).
south(b4,c4).
south(c2,d2).
south(c3,d3).
south(c4,d4).
south(d2,e2).
south(d3,e3).
south(d4,e4).
south(e2,f2).
south(e3,f3).
south(e4,f4).
south(f2,g2).
south(f3,g3).
south(f4,g4).

south(c0,d0).
south(c1,d1).
south(c5,d5).
south(c6,d6).
south(d0,e0).
south(d1,e1).
south(d5,e5).
south(d6,e6).

north(b2,a2).
north(b3,a3).
north(b4,a4).
north(c2,b2).
north(c3,b3).
north(c4,b4).
north(d2,c2).
north(d3,c3).
north(d4,c4).
north(e2,d2).
north(e3,d3).
north(e4,d4).
north(f2,e2).
north(f3,e3).
north(f4,e4).
north(g2,f2).
north(g3,f3).
north(g4,f4).

north(d0,c0).
north(d1,c1).
north(d5,c5).
north(d6,c6).
north(e0,d0).
north(e1,d1).
north(e5,d5).
north(e6,d6).

west(c1,c0).
west(d1,d0).
west(e1,e0).
west(c2,c1).
west(d2,d1).
west(e2,e1).
west(c3,c2).
west(d3,d2).
west(e3,e2).
west(c4,c3).
west(d4,d3).
west(e4,e3).
west(c5,c4).
west(d5,d4).
west(e5,e4).
west(c6,c5).
west(d6,d5).
west(e6,e5).

west(a3,a2).
west(b3,b2).
west(f3,f2).
west(g3,g2).
west(a4,a3).
west(b4,b3).
west(f4,f3).
west(g4,g3).

east(c0,c1).
east(d0,d1).
east(e0,e1).
east(c1,c2).
east(d1,d2).
east(e1,e2).
east(c2,c3).
east(d2,d3).
east(e2,e3).
east(c3,c4).
east(d3,d4).
east(e3,e4).
east(c4,c5).
east(d4,d5).
east(e4,e5).
east(c5,c6).
east(d5,d6).
east(e5,e6).

east(a2,a3).
east(b2,b3).
east(f2,f3).
east(g2,g3).
east(a3,a4).
east(b3,b4).
east(f3,f4).
east(g3,g4).

delete(_,[],[]).
delete(X, [X|Z], Z).
delete(X, [H|Z], [H|T]) :- !, delete(X, Z, T).

printout([A|[]]) :-
        write(A), nl.

printout([A | List]) :-
        write(A), write(', '), printout(List).

% Jump from A to B via X only possible if no Token on B but
% Tokens on A and X
jump(A, X, B, Tokens) :-
        (north(A, X), north(X, B), member(A, Tokens),
        member(X, Tokens), not member(B, Tokens));
        (south(A, X), south(X, B), member(A, Tokens),
        member(X, Tokens), not member(B, Tokens));
        (west(A, X), west(X, B), member(A, Tokens),
        member(X, Tokens), not member(B, Tokens));
        (east(A, X), east(X, B), member(A, Tokens),
        member(X, Tokens), not member(B, Tokens)).

% Here the program should end with one correct solution
move([A, X], Result) :-
        jump(A, X, d3),
        write('Solution: '), printout(.([A,d3], Result)), nl.

move(Tokens, []) :-
% write('tokens: '), printout(Tokens),
    jump(A, X, B, Tokens),
% jump(A, X, B, Tokens).
% write(A), write(' '), write(X), write(' '), write(B), nl,
    delete(A, Tokens, Tmp1),
    delete(X, Tmp1, Tmp2),
    move(.(B, Tmp2), [[A, B]]).
         

move(Tokens, Jumps) :-
% write('jump: '), printout(Jumps),
% write('tokens: '), printout(Tokens),
    jump(A, X, B, Tokens),
%write(A), write(' '), write(X), write(' '), write(B), nl,
%read(Z),
    delete(A, Tokens, Tmp1),
    delete(X, Tmp1, Tmp2),
    printout(Tmp2),
% ==================== infinite loop =========================
% read(Z),
    move(.(B, Tmp2), .([A,B], Jumps)).
        

% starting point
res :- %tell('moves.txt'),
        move([ a2, a3, a4,
                 b2, b3, b4,
         c0, c1, c2, c3, c4, c5, c6,
         d0, d1, d2, d4, d5, d6,
         e0, e1, e2, e3, e4, e5, e6,
                 f2, f3, f4,
                 g2, g3, g4],
         []).%,
         %told('moves.txt').



Relevant Pages