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
Based on your question, you probably want to write the following clause:
Given you write this to the file, it will generate:
Furthermore it can validate whether two coordinates are located in a certain direction given
D
us instantiated:However it will not work with:
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: