Reputation: 324
So I am supposed to recreate a small implementation of the game clue.
I am given a set of facts, and must output a list of possible suspects found by Prolog. Given a single fact for a "unique" suspect, The program should then only output the unique suspect found.
I have all of the logic done, now I am just having a hard time figuring out how to stop the program from finding suspects when I give it the "unique" suspect fact.
Could I get a little help from another eye to see where i am going wrong with this, my beginner proficiency has baffled me on how to get this to stop or organize the cuts in order to get it to stop.
Here is the code:
%% Facts
rich(mrBoddy).
affair_with(msGreen, mrBoddy).
affair_with(missScarlet, mrBoddy).
married_to(profPlum, msGreen).
greedy(colMustard).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Rules
affair(X, Y) :- affair_with(X, Y).
affair(X, Y) :- affair_with(Y, X), !.
married(X, Y) :- married_to(X, Y).
married(X, Y) :- married_to(Y, X), !.
greed(X, Victim) :-
greedy(X),
not(rich(X)),
rich(Victim).
hate(X, Victim) :-
married(X, Someone),
affair(Someone, Victim).
murder(X, Victim) :-
hate(X, Victim);
greed(X, Victim).
%%% THIS IS THE UNIQUE SUSPECT FACT--- suspect_found(profPlum).
suspect_found(X) :-
X = profPlum;
X = missScarlet;
X = msGreen;
X = colMustard.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Solution
suspect(Killer, mrBoddy) :-
murder(Killer, mrBoddy),
suspect_found(Killer).
Upvotes: 1
Views: 1102
Reputation: 542
It is clear that the suspect_found/1
predicate has multiple solutions (because of the use of semicolons ;
in its body) and that it does not use any of the other predicates (rules and facts) in your program. Therefore it is only natural the first answer from it is profPlum
and by backtracking it also
yields missScarlet
, msGreen
and colMustard
.
Upvotes: 1