0

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!

false
  • 10,264
  • 13
  • 101
  • 209
Assaf
  • 1,112
  • 4
  • 14
  • 35
  • 1
    Well where do you use `X` and `Y` in your goal `is_move_legal(Board, P1, P2, _, b)`? Note that the first attribute of the `findall` is the "template" that specifies *what* to return, here you say, return `X -> Y`, but `X` and `Y` are in no way related to `Board`, `P1`, `P2`, so that means you simply add free variables. – Willem Van Onsem Nov 10 '18 at 12:36
  • When I use X and Y they are coming as input from the user, so the predicate satisfies them as well, – Assaf Nov 10 '18 at 12:37
  • @WillemVanOnsem thank you! it works it when I wrote findall((P1->P2),is_move_legal(Board,P1,P2,_,b),Moves). – Assaf Nov 10 '18 at 12:38

0 Answers0