Reputation: 83
I have a little question about my code : (it can be interpreted as a grid for example)
free(1,1).
free(1,3).
free(2,3).
free(2,1).
free(1,5).
free(5,6).
free(5,2).
free(5,4).
busy(5,1,white,pion).
busy(1,2,black,pion).
busy(1,4,black,pion).
busy(1,6,black,pion).
busy(5,3,white,pion).
busy(5,5,white,pion).
%all clauses for move/6
move(X,Y, X2,Y2,white,pion) :-
busy(X,Y,white,pion), free(X2,Y2), X2=X-1, Y2=Y-1.
move(X,Y, X2,Y2,white,pion) :-
busy(X,Y,white,pion), free(X2,Y2), X2=X-1, Y2=Y+1.
move(X,Y, X2,Y2,black,pion) :-
busy(X,Y,black,pion), free(X2,Y2), X2=X+1, Y2=Y-1.
move(X,Y, X2,Y2,black,pion) :-
busy(X,Y,black,pion), free(X2,Y2), X2=X+1, Y2=Y+1.
When I execute this :
move(1,2,X,Y,black,pion).
in SWI-Prolog it says : false. whereas it should says true and return two statements :
I do not understand why it doesn't work, could you please help me ?
Upvotes: 0
Views: 41
Reputation: 15338
It is simply because you use =
("make sure that left-hand-side and right hand side unify") instead of is/2
(evaluate the arithmetic expression on the right-hand side and unify with the left-hand-side) or even better #=
(constrain the arithmetic values to be the same, even if not all the variables can be resolved yet), here:
X2=X-1, Y2=Y-1.
This unifies X2
with the actual structure -(X,1)
. Which will fail, as X2
will be a value.
Use:
:- use_module(library(clpfd)). % for #= predicate
move(X,Y, X2,Y2,white,pion) :-
busy(X,Y,white,pion), free(X2,Y2), X2 #= X-1, Y2 #= Y-1.
Also, move the test forwards:
move(X,Y, X2,Y2,white,pion) :-
busy(X,Y,white,pion), X2 #= X-1, Y2 #= Y-1, free(X2,Y2).
Upvotes: 1