Solving a Murder Mystery using Prolog

murder-mystery-cluedo
In the middle of last winter, eight guests were invited to a luxurious retreat at the Duke of York Grand Hotel. On the last day of their three-day getaway, the guests were free to vacate to their own occupations. Mrs White and Reverend Green did some “gardening” walking alongside the water fountains, Colonel Mustard and Professor Plum played golf (alone though, purposefully avoiding each other). The other guests spent their days either in their rooms or in the lounge, by the log fire. Later on in the afternoon, all the guests were indoors and Colonel Mustard was seen playing cards with Reverend Green and Mrs Peacock.

As the guests were called for dinner, they soon realised that Dr Black was missing. He was later found lying down on the floor of his bedroom. Dr Black had been shot dead using an old fashion revolver. Except from a few muddy footprints at the entrance of his bedroom, there was no other evidence left by the murderer.

Here is the list of all the guests for the weekend and the rooms they were staying in. Note that the hotel consists of twin bedrooms accommodating two guests per room. We also know that three of the guests (Reverend Green, Colonel Mustard and Madame Rose) own a revolver that they brought with them and kept in their room.

murder-mystery-clues

– Click on the picture to zoom in/enlarge –

Prolog?

Prolog is a language built around the Logical Paradigm: a declarative approach to problem-solving.

There are only three basic constructs in Prolog: facts, rules, and queries.

A collection of facts and rules is called a knowledge base (or a database) and Prolog programming is all about writing knowledge bases. That is, Prolog programs simply are knowledge bases, collections of facts and rules which describe some collection of relationships that we find interesting.

So how do we use a Prolog program? By posing queries. That is, by asking questions about the information stored in the knowledge base. The computer will automatically find the answer (either True or False) to our queries.

Source: http://www.learnprolognow.org/

Knowledge Base (Facts & Rules)

Let’s see how a detective with some good computer science skills would solve this murder mystery. Their first step would be to write all the facts about the case using the Prolog syntax:

/* Facts */
man(dr_black).
man(reverend_green).
man(colonel_mustard).
man(professor_plum).

woman(mrs_peacock).
woman(madame_rose).
woman(miss_scarlett).
woman(mrs_white).

victim(dr_black).

playing_cards(colonel_mustard).
playing_cards(reverend_green).
playing_cards(mrs_peacock).

gardening(mrs_white).
gardening(reverend_green).

played_golf(professor_plum).
played_golf(colonel_mustard).

smoker(miss_scarlett).
smoker(colonel_mustard).
smoker(mrs_white).
smoker(dr_black).
smoker(mrs_peacock).

room(room_21).
room(room_22).
room(room_23).
room(room_24).
room(room_25).

stay_in(dr_black,room_22).
stay_in(reverend_green,room_24).
stay_in(miss_scarlett,room_21).
stay_in(colonel_mustard,room_24).
stay_in(professor_plum,room_22).
stay_in(mrs_peacock,room_23).
stay_in(madame_rose,room_21).
stay_in(mrs_white,room_23).

owns_revolver(reverend_green).
owns_revolver(colonel_mustard).
owns_revolver(madame_rose).

Then our detective would write some rules and apply them to these facts.

A suspect is either a man or a woman who is not the victim!

#Rule #1:

This is how this rule would be implemented in Prolog:

suspect(X):- man(X), \+victim(X).
suspect(X):- woman(X), \+victim(X).

Any suspect who was seen playing cards at the time of the crime has a valid alibi!

#Rule #2:

This is how this rule would be implemented in Prolog:

has_alibi(X):- suspect(X), playing_cards(X).

Anyone who did some gardening, played golf or is a smoker has been outside at least once throughout the day.

#Rule #3:

This is how this rule would be implemented in Prolog:

went_outside(X):- gardening(X).
went_outside(X):- smoker(X).
went_outside(X):- played_golf(X).

The rooms of the hotel are twin rooms that can accomomdate two guests. Two guests share the same room if they both are booked in the same room.

#Rule #4:

This is how this rule would be implemented in Prolog:

share_room(X,Y):- room(R), stay_in(X,R), stay_in(Y,R), X \= Y.

Anyone who owns a revolver or share a room with another guest who owns a revolver, had access to a revolver!

#Rule #5:

This is how this rule would be implemented in Prolog:

revolver_access(X):- owns_revolver(X).
revolver_access(X):- share_room(X,Y), owns_revolver(Y).

Solving this murder case!

Step 1: Can you use all the above facts & rules to work out the actual murderer?

Step 2: Can you write one final rule needed to solve this murder mystery in just one query?

To find our culprit we need to find a suspect, who went outside (we found some muddy footprints on the floor next to Dr Black’s body), who has no alibi and who had access to a revolver!

You can access the detective’s knowledge base:
https://swish.swi-prolog.org/p/murder-mystery.pl
Murder Mystery CodeOpen in New Window

Solution:

This is how our final rule would be implemented in Prolog:
View Solution!

Going a step further…

Now that you have solved this task, update the case by adding additional characters/suspects and additional clues. Make sure that at the end, the murderer of your case can still be found in just one query.

As additional suspects you could add some additional guests:

  • Mr Peach staying in room 25
  • Dr Orchid staying in room 25

Decide whether they went outside or not, whether they own a revolver or not, whether they were seen at the table playing card games or not! You can also add new indoors or outdoors activities.

You may also consider adding hotel staff members e.g.:

  • A barman,
  • A receptionist.

Staff members would have access to all the rooms!

Did you like this challenge?

Click on a star to rate it!

Average rating 4.1 / 5. Vote count: 33

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: