Hi, I'm trying implementing a predicate adjacent(W, Z) such that cell W ([X1,Y1]) is adjacent Z([X2,Y2]) iff: X1 = X2 + 1 or X1 = X2 -1 or Y1 = Y2 + 1 or Y1 = Y2 -1 . This predicate is used into an algorithm that make these two questions:
- given cell W, what are the cell Z adjacent to W? - given cell Z, what are the cells W adjacent to Z? I proposed the following solution:
adjacent(L1,L2) :- location_toward(L1,_,L2). location_toward([X,Y],[New_X,Y]) :- New_X is X+1.
location_toward([X,Y],[X,New_Y]) :- New_Y is Y+1.
location_toward([X,Y],[New_X,Y]) :- New_X is X-1.
location_toward([X,Y],[X,New_Y]) :- New_Y is Y-1.
Calling the predicate in the form: adjacent([1,1],Z) it lists all the adjacents cells (correct!) adjacent(W,[1,2]) it gives these exception: