سؤال

I am writing a rule in Prolog to create a fact, pit(x,y). This rule below is called three times from my main function, and it is inserting three pits in which none of them is at (1,1) or (1,2) or (2,1) but the problem is that sometimes 2 pits have the same x and y where x and y can be from 1 to 4 only. (4x4 grid)

placePit(_) :-   Px is random(4)+1,
                 Py is random(4)+1,
                 write(Px),
                 write(' '),
                 writeln(Py),
                 (Px =\= 1; 
                 Py =\= 1),
                 (Px =\= 1;
                 Py =\= 2),
                 (Px =\= 2;
                 Py =\= 1)
                 ->
                 pit(Px,Py);
                 placePit(4).

I don't want this to happen, so I write another rule to check whether 2 pits are the same first and will extend later to REMOVE EITHER ONE from the database. From what I have tested, it doesn't get fired at all even though 2 pits appear to be the same. What am I doing wrong? How to remove duplicate facts?

pit(A,B) :- pit(C,D), 
            A = C, 
            B = D, 
            write('Duplicate').

PS. I am very new at Prolog. Any suggestion is appreciated.

هل كانت مفيدة؟

المحلول

maybe this could help, in assumption you're actually required to generate facts:

:- dynamic(pit/2).

pit(1,1).
pit(1,2).
pit(2,1).

placePit(N) :-
  N > 0,
  Px is random(4)+1,
  Py is random(4)+1,
  ( \+ pit(Px, Py)         % if not exist
  -> assertz(pit(Px, Py)), % store
     M is N-1              % generate another
  ;  M = N                 % nothing changed, retry
  ),
  placePit(M).             % recursion is the proper Prolog way to do cycles
placePit(0).               % end of recursion (we call it 'base case')

you should call as

?- placePit(3).

It shows a bit of syntactic detail, like the 'if/then/else', that in Prolog has a peculiar form.

edit When done, you could remove unwanted pit/2, to get your db 'clean'.

?- maplist(retract, [pit(1,1),pit(1,2),pit(2,1)]).

(note that I assumed - based on your description - that a DB stored pit/2 was of value for further processing).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top