Josh Ketchum & Matt Field's Prolog Project

From Norsemathology
Jump to navigation Jump to search

Description of the Problem

A death has happened at the Clue Mansion. The body of an unknown man (we will call him Mr. Body) has been found, BUT no one knows where the murder occurred or with what weapon he was slain by or even who killed him. In this game of Clue, there are 5 characters, 5 weapons, 5 locations, and 1 murderer. Each character can have only one weapon and one room.

The suspects are: Professor Plum, Colonel Mustard, Mrs. White, Mr. Green, and Ms. Scarlett. The locations are: Kitchen, Billiards, Library, Hall, and Conservatory. The weapons are: Rope, Candle Stick, Pipe, Revolver, and Knife.

But given a series of 15 clues, can you solve who the murderer is, where they committed the murder, and what was the weapon? Without a pencil and paper to take notes and make tables of the many variables, thinking out who did it may be a little harder than expected.

That is where Prolog comes in.

Programming in Logic (also known as Prolog) is used often when human minds cannot keep track of the many facts and variables and uncertain values floating around. This logical programming is a perfect fit for the game of clue. All Prolog programs have two important things: a Prolog database and Prolog facts.

Description of the Database

In this game of Clue, the player is given 3 series of 5 clues, each pertaining to location, weapon, or innocence. The series of clues are listed below, each with the people abbreviation used in the code and the method name used for guessing.

Location Clues

  • Locations: kitchen, billiards, library, hall, conservatory
  • People: pp (Professor Plum), cm (Colonel Mustard), mw (Mrs. White), mg (Mr. Green), ms (Ms. Scarlet)
  • Use: guessRoom(person, room)
  1. Professor Plum is not in the Billiards or the Library.
  2. Ms. Scarlett is either in the Conservatory or in the Hall.
  3. Colonel Mustard is in the Billiards.
  4. Mrs. White is either in the Library, Billiards, or Conservatory.
  5. Mr. Green is not in the Conservatory, Library, or Kitchen.

Weapon Clues

  • Weapons: rope, candle (candle stick), pipe, revolver, knife
  • People: pp (Professor Plum), cm (Colonel Mustard), mw (Mrs. White), mg (Mr. Green), ms (Ms. Scarlet)
  • Use: guessWeapon(person, weapon)
  1. Ms. Scarlett doesn’t know how to use a gun, so she does not have the Revolver.
  2. Mr. Green either has the Revolver, Pipe, or Candle Stick.
  3. Mrs. White’s husband is a plumber, so she has the Pipe.
  4. Professor Plum either has the Rope or the Candle Stick.
  5. Colonel Mustard either has the Candle Stick or the Pipe.

Guilty or Innocent Clues

  • People: pp (Professor Plum), cm (Colonel Mustard), mw (Mrs. White), mg (Mr. Green), ms (Ms. Scarlet)
  • Use: isGuilty(person)
  1. Colonel Mustard said that Mr. Green and Ms. Scarlett are innocent
  2. Mr. Green yells Colonel Mustard and himself are innocent
  3. Ms. Scarlett blurts out that both Professor Plum and Colonel Mustard are innocent.
  4. Mrs. White sternly says that Ms. Scarlett and Professor Plum would never kill anyone.
  5. Professor Plum says that Ms. Scarlett is the murderer and Mrs. White is innocent.

Who killed Mr. Body???

The Code

The code as a prolog file (ready for compilation)


For extreme-complexity purposes, the solutions to location and the weapons are hard-coded into the code. However, the solution to "who did it" is not hard-coded into the code.

%%
%%	Josh Ketchum & Matt Field
%%	Mat385
%%	Dr. Long
%%	9/27/10
%%
%%	The following program is an interpretation of the classic game, clue.

%%	DATABASE FACTS:
location(pp,kitchen).
location(cm,billiards).
location(mw,library).
location(mg,hall).
location(ms,conservatory).
using(pp,rope).
using(cm,candle).
using(mw,pipe).
using(mg,revolver).
using(ms,knife).

says(cm).
says(mg).
says(ms).
says(mw).
says(pp).

guilty(X,Y,Z):-
	guessP(X),
	location(X,Y),
	using(X,Z).
%%	END DATABASE FACTS


%SOLUTION:

and(V,X,Y,Z):-
	V,X,Y,Z.

guessRoom(X,Y) :-
	location(X,Y).

guessP(cm):-
       and(says(cm), says(pp), not(says(ms)), says(mw)).
guessP(mg):-
       and(not(says(mg)), says(ms), says(mw), not(says(pp))).
guessP(ms):-
       and(says(mg), says(ms), not(says(mw)), says(mw)).
guessP(mw):-
       and(says(cm), says(mg), says(ms), says(mw)).

guessP(pp):-
       and(says(cm), says(mg), says(pp), not(says(mw))).

guessWeapon(X,Y) :-
	using(X,Y).


choiceClues(X) :-
	X == 1, (write('\n1: Professor Plum is not in the Billards or the Library. \n
	2: Ms. Scarlett is either in the conservatory or in the Hall.\n
	3: Colonel Mustard is in the Billards.\n4: Mrs White is either in the Library, Billiards, or Conservatory.\n
	5: Mr. Green is not in the Conservatory, Library, or Kitchen.\n'));
	X == 2, (write('1: Ms Scarlett does not know how to use a gun, so she does not have the Revolver.\n
	2: Mr. Green either has the Revolver, Pipe or Candle Stick.\n
	3: Mrs. Whites husband is a plumber, so she has the Pipe.\n
	4: Professor Plum either ahs the Rope or the Candle Stick.\n
	5: Colonel Mustard either has the Candle Stick or the Pipe\n'));
	X == 3, (write('1: Colonel Mustard said Mr. Green and Ms. Scarlett are innocent.\n
	2: Mr. Green yells Colonel Mustard and himself are innocent.\n
	3: Ms. Scarlett blurts out that both Professor Plum and Colonel Mustard are innocent.\n
	4: Mrs. White sternly says that Ms. Scarlett and Professor Plum would never kill anyone.\n
	5. Professor Plum says that Ms. Scarlett is the murderer and Mrs. White is innocent.')).

choiceGuess(X) :-
	X == 1, (write('\nWho is where? :\n')),
	write('People choices: \npp - Professor Plum\nms - Ms. Scarlett\nmg - Mr. Green\ncm - Colonel Mustard\nmw - Mrs. White\n'),
	write('Location choices: conservatory, library, hall, billards, or kitchen\n'),
	write('Who: '),
	read(Sel),
	write('Where: '),
	read(Loc),
	guessRoom(Sel, Loc);
	X == 2, (write('\nWho has what?  (in fuction of guessWeapon(person, weapon)):\n')),
	write('People choices: \npp - Professor Plum\nms - Ms. Scarlett\nmg - Mr. Green\ncm - Colonel Mustard\nmw - Mrs. White\n'),
	write('Weapon choices: rope, revolver, knife, pipe, or candle\n\n'),
	write('Person: '),
	read(Sel),
	write('Weapon: '),
	read(Weap),

	guessWeapon(Sel, Weap);
	X == 3, (write('\nWho did it? (in fuction of guess(person)):\n
	pp - Professor Plum\nms - Ms. Scarlett\nmg - Mr. Green\ncm - Colonel Mustard\nmw - Mrs. White\n')),
	write('Selection: '),
	read(Sel),
	guessP(Sel).


%%	DATABASE RULES:
clues:-
	write('\nWhich would you like to read:'),
	repeat,
	write('\n[1] - Location Clues'),
	write('\n[2] - Weapon Clues'),
	write('\n[3] - Guilty or Innocent Clues'),
	write('\nChoice: '),
	read(Sel),
	choiceClues(Sel).

guess:-
	repeat,
	write('\nGuess again\nWhich would would you like to guess:'),
	write('\n[1] - Who is where'),
	write('\n[2] - Who has what'),
	write('\n[3] - Who is the murderer'),
	write('\nSelection:'),
	read(Sel1),
	choiceGuess(Sel1).

solve:-
	write('\ntype the person: '),
	read(Per),
	write('\ntype the location: '),
	read(Loc),
	write('\ntype the weapon: '),
	read(Weap),
	guilty(Per,Loc,Weap).

Example Output

1 ?- clues.

Which would you like to read:
[1] - Location Clues
[2] - Weapon Clues
[3] - Guilty or Innocent Clues
Choice: 2.
1: Ms Scarlett does not know how to use a gun, so she has the Pipe.
2: Mr. Green either has the Revolver, Pipe or Candle Stick.
3: Mrs. Whites husband is a plumber, so she has the Pipe.
4: Professor Plum either ahs the Rope or the Candle Stick.
5: Colonel Mustard either has the Candle Stick or the Pipe
true.


2 ?- clues.

Which would you like to read:
[1] - Location Clues
[2] - Weapon Clues
[3] - Guilty or Innocent Clues
Choice: 1.

1: Professor Plum is not in the Billards or the Library. 
2: Ms. Scarlett is either in the conservatory or in the Hall.
3: Colonel Mustard is in the Billards.
4: Mrs White is either in the Library, Billiards, or Conservatory.
5: Mr. Green is not in the Conservatory, Library, or Kitchen.
true .

3 ?- clues.

Which would you like to read:
[1] - Location Clues
[2] - Weapon Clues
[3] - Guilty or Innocent Clues
Choice: 2.
1: Ms Scarlett does not know how to use a gun, so she does not have the Revolver.
2: Mr. Green either has the Revolver, Pipe or Candle Stick.
3: Mrs. Whites husband is a plumber, so she has the Pipe.
4: Professor Plum either ahs the Rope or the Candle Stick.
5: Colonel Mustard either has the Candle Stick or the Pipe
true .

4 ?- clues.

Which would you like to read:
[1] - Location Clues
[2] - Weapon Clues
[3] - Guilty or Innocent Clues
Choice: 3.
1: Colonel Mustard said Mr. Green and Ms. Scarlett are innocent.
2: Mr. Green yells Colonel Mustard and himself are innocent.
3: Ms. Scarlett blurts out that both Professor Plum and Colonel Mustard are innocent.
4: Mrs. White sternly says that Ms. Scarlett and Professor Plum would never kill anyone.
5. Professor Plum says that Ms. Scarlett is the murderer and Mrs. White is innocent.
true .

5 ?- guess.

Guess again
Which would would you like to guess:
[1] - Who is where
[2] - Who has what
[3] - Who is the murderer
Selection:1.

Who is where? :
People choices: 
pp - Professor Plum
ms - Ms. Scarlett
mg - Mr. Green
cm - Colonel Mustard
mw - Mrs. White
Location choices: conservatory, library, hall, billards, or kitchen
Who: pp.
Where: library.

Guess again
Which would would you like to guess:
[1] - Who is where
[2] - Who has what
[3] - Who is the murderer
Selection:1.

Who is where? :
People choices: 
pp - Professor Plum
ms - Ms. Scarlett
mg - Mr. Green
cm - Colonel Mustard
mw - Mrs. White
Location choices: conservatory, library, hall, billards, or kitchen
Who: mw.
Where: library.
true .

6 ?- guess.

Guess again
Which would would you like to guess:
[1] - Who is where
[2] - Who has what
[3] - Who is the murderer
Selection:2.

Who has what?  (in fuction of guessWeapon(person, weapon)):
People choices: 
pp - Professor Plum
ms - Ms. Scarlett
mg - Mr. Green
cm - Colonel Mustard
mw - Mrs. White
Weapon choices: rope, revolver, knife, pipe, or candle

Person: pp.
Weapon: pipe.

Guess again
Which would would you like to guess:
[1] - Who is where
[2] - Who has what
[3] - Who is the murderer
Selection:2.

Who has what?  (in fuction of guessWeapon(person, weapon)):
People choices: 
pp - Professor Plum
ms - Ms. Scarlett
mg - Mr. Green
cm - Colonel Mustard
mw - Mrs. White
Weapon choices: rope, revolver, knife, pipe, or candle

Person: mw.
Weapon: pipe.
true .

7 ?- guess.

Guess again
Which would would you like to guess:
[1] - Who is where
[2] - Who has what
[3] - Who is the murderer
Selection:3.

Who did it? (in fuction of guess(person)):
pp - Professor Plum
ms - Ms. Scarlett
mg - Mr. Green
cm - Colonel Mustard
mw - Mrs. White
Selection: mw.
true.

8 ?- solve.

type the person: mw.

type the location: library.

type the weapon: pipe.
true.