Search code examples
prologcombinatoricsknights-tour

Call a rule in Prolog


I'm trying to solve Knight Tour problem. I want to call a rule to evaluate possible movements, but I can't do that my rule returns the next position in the variable that I send.

    move(X,Y):-
      X is X+1,
      Y is Y-2.
    move(X,Y):-
      X is X+2,
      Y is Y-1.

In the console I send move(2,2) for example and I hope that returns 3,0 and 4,1 but returns false.


Solution

  • You need to use new variables and add new parameters like:

    move(X, Y, New_X, New_Y):-
          New_X is X+1,
          New_Y is Y-2.
    

    That's because when you call move(2,2) X and Y are instantiated to the value 2 and they can't change, so you could pass two new uninstantiated variables which will be returned instantiated.

    For example now if you call: move(2,2,X,Y)
    X,Y will be instantiated (when predicate returns/succeeds) and the predicate will return you the right values in X,Y .