I am writing a Checkers game in Prolog, and I want to write a predicate to print all possible moves.
I have a predicate who checks all the legal moves than can be made -
is_move_legal(Board,p(X1,Y1),p(X2,Y2),DoEat, Player):-
Player = w, % making sure the "right" player is playing here - white
get(Board,p(X1,Y1),w),
(
get(Board,p(X2,Y2),1); % make sure no player is in the dest cell
get(Board,p(X2,Y2),0) % make sure no player is in the dest cell
),
between(1,8,X1),between(1,8,X2),between(1,8,Y1),between(1,8,Y2),
(
(DoEat = 0, X2 is X1-1,Y2 is Y1-1); % we don't eat
(DoEat = 0, X2 is X1-1,Y2 is Y1+1);
(DoEat = 1, X2 is X1-2, Y2 is Y1-2, X3 is X1-1, Y3 is Y1-1, (get(Board,p(X3,Y3),b);get(Board,p(X3,Y3),bk)),remove(Board,p(X3,Y3))); % eat the black soldier
(DoEat = 1, X2 is X1-2, Y2 is Y1+2, X3 is X1-1, Y3 is Y1+1, (get(Board,p(X3,Y3),b);get(Board,p(X3,Y3),bk)),remove(Board,p(X3,Y3))) % eat the black soldier
).
I have similair predicates for the black soldiers and for "kings" soldiers.
This is the findall predicate -
% find all possible moves
moves(Board,Moves):-
findall((X->Y),is_move_legal(Board,P1,P2,_,b),Moves).
It seems that it does find the moves, however this it the output I get -
[(_8090->_8092),(_8078->_8080),(_8066->_8068),(_8054->_8056),(_8042->_8044),(_8030->_8032),(_8018->_8020),(_8006->_8008)]
What I am trying to do, is to satisfy the p(X1,Y1), p(X2,Y2) arguments in the is_move_legal
predicate.
EDIT:
From a comment here I realized the mistake -Rather then (X->Y), write -
findall((P1->P2),is_move_legal(Board,P1,P2,_,b),Moves).
Your help is much appreciated.
Thank you!