I am supposed to write a Prolog program to simulate a small version of the game
ID: 652618 • Letter: I
Question
I am supposed to write a Prolog program to simulate a small version of the game clue.
I am having an issue with getting multiple suspects for the killer when I have the unique suspect (The real killer) fact commented out. Basically, I should have a single fact that results in the unique suspect being found. If this single fact is commented out, my code should find multiple possible suspects.
I have all of the logic working correctly, but I cannot seem to get the behavior I have just described above.
Here is the code: Can someone please help me out
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%% CLUE %%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 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).
murder(X, Victim) :-
greed(X, Victim).
suspect_found(profPlum).
suspect_found(X) :-
X = profPlum;
X = missScarlet;
X = msGreen;
X = colMustard.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Solution
suspect(Killer, mrBoddy) :-
murder(Killer, mrBoddy),
suspect_found(Killer).
Explanation / Answer
It is clear that the suspect_found predicate has multiple solutions (because of the utilization of semicolons ; in its body) which it doesn't use any of the opposite predicates (rules and facts) in the program. Thus it's solely natural the answer from it's profPlum and by backtracking it additionally yields missScarlet, msGreen and colMustard.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.