简体   繁体   English

Mandist谓词未给出预期结果

[英]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, 因此,我试图找到8块拼图中的空白砖滑动的方向,

I am using X/Y to determine the tiles. 我正在使用X / Y确定图块。

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. 我得到的问题是,在调用mandist谓词时,它没有给我我所希望的结果。

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语句上,我已经写了一些伪代码,这样您就可以了解我要执行的操作,

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 然后这称为mandist谓词

With D set to 1, so it ensures its a legal move 将D设置为1,以确保其合法移动

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 我也期望它说Direction = right因为位置1/1是3x3网格的最左下角,并且从左下角开始的唯一移动是向上或向右

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: 根据您的问题,您可能需要编写以下子句:

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 : 此外,它可以验证在给定实例D两个坐标是否位于某个方向上:

?- 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM