Search code examples
prologsliding-tile-puzzle

Mandist predicate not giving expected results


So I'm trying to find the direction that the blank tile in the 8 tile puzzle will be sliding,

I am using X/Y to determine the tiles.

Here's the code I have.

mandist( X /Y , X1 / Y1 , D, Direction):-
       D is abs(X - X1 )+ abs(Y - Y1 ),
       Y is Y1  ->(
           (   X is X1 +1 -> Direction ='left')
           ;
           (   X1 is X +1 -> Direction = 'right'))
       ;  X is X1 ->(
               (   Y1 is Y +1 -> Direction ='up')
           ;
           (   Y is Y1 +1 -> Direction ='down')).

The problem I'm getting is that when calling the mandist predicate it isn't giving me the results I had hoped for.

I am sure the problem is with the if statement, I have wrote some psuedocode so you can understand what I'm trying to do,

if(Y == Y1){
    // Change the X Axis
    if(X == X1 +1){
        This is a left move
    }
    else{
        This is a right move
    }
}else if (X == X1){
    // Change the Y Axis 
    if(Y == Y1 + 1){
        This is an up move
    }
    else{
        This is a down move
    }
}

Example:

move([1/1, 3/1, 1/3, 2/3, 3/3, 1/2, 2/2, 3/2,2,1], X, Direction)

This then calls the mandist predicate

With D set to 1, so it ensures its a legal move

Here is the unexpected result:

mandist(1/1, X,1, Direction).
Direction = up ;
false

I am also expecting it to say Direction = right because position 1/1 is the very bottom left of the 3x3 grid and the only moves from the bottom left are either up or right

1/3 2/3 3/3

1/2 2/2 3/2

1/1 2/1 3/1


Solution

  • Based on your question, you probably want to write the following clause:

    mandist(X/Y,XD/Y,D,right) :-
        XD is X+D.
    mandist(X/Y,X/YD,D,up) :-
        YD is Y+D.
    mandist(X/Y,XD/Y,D,left) :-
        XD is X-D.
    mandist(X/Y,X/YD,D,down) :-
        YD is Y-D.
    

    Given you write this to the file, it will generate:

    ?- mandist(1/1,X,1,Direction).
    X = 2/1,
    Direction = right ;
    X = 1/2,
    Direction = up ;
    X = 0/1,
    Direction = left ;
    X = 1/0,
    Direction = down.
    

    Furthermore it can validate whether two coordinates are located in a certain direction given D us instantiated:

    ?- mandist(1/1,1/2,1,Direction).
    Direction = up ;
    false.
    

    However it will not work with:

    ?- mandist(1/1,1/2,D,Direction).
    ERROR: is/2: Arguments are not sufficiently instantiated
    

    There is however a way to do this. Please updated your question if that is a requirement.

    EDIT:

    Since there are bounds, you can simply add them to the clauses. If you can assume that the first pair of coordinates are valid, it is simply:

    mandist(X/Y,XD/Y,D,right) :-
        XD is X+D,
        XD < 4.
    mandist(X/Y,X/YD,D,up) :-
        YD is Y+D,
        YD < 4.
    mandist(X/Y,XD/Y,D,left) :-
        XD is X-D,
        XD > 0.
    mandist(X/Y,X/YD,D,down) :-
        YD is Y-D,
        YD > 0.