// Abstract ------------------------------------------------------------------------------------------------------------------------------

This sample solves the 3x3 knight puzzle.

// Examples ------------------------------------------------------------------------------------------------------------------------------

?- #path(1,3,:l)
-> ( [1, 8, 3] ) := 1.00 (0.006) 1
-> ( [1, 6, 7, 2, 9, 4, 3] ) := 1.00 (0.013) 2
 
// Code ----------------------------------------------------------------------------------------------------------------------------------

move { // authorized moves for a "knight"

    (1,6);
    (1,8);
    (2,7);
    (2,9);
    (3,4);
    (3,8);
    (4,3);
    (4,9);
    (6,7);
    (6,1);
    (7,6);
    (7,2);
    (8,3);
    (8,1);
    (9,4);
    (9,2);

}

path { // walks the possible moves and try to find a path from a position to another one

    (:f,:t,:l)          :- #path(:f,:t,[:f],[],:lo), lst.cat(:f,:lo,:l);

    (:e,:e,_,_,[])^     :- true;
    (:f,:t,:s,:p,:po)   :- #move(:f,:i), lst.except(:i,:s), #path(:i,:t,[:i|:s],:p,:p2), lst.cat(:i,:p2,:po);

}

// ---------------------------------------------------------------------------------------------------------------------------------------

[Home] [Email] [Twitter] [LinkedIn]