“或"序言中的程序 [英] "Or" procedure in prolog
问题描述
我正在为大学做一个序幕程序,有点像cluedo游戏.我有六个性格各异的犯罪嫌疑人:
I'm doing a prolog program for college that is a bit like the cluedo game. I have six suspects with different traits:
suspect(Name, Age, Weapon, Shape, Object, Shoes)
目标是实施一系列线索,以便程序说出这六个嫌疑犯的不同特征.例如:
The goal is to implement a series of clues so that the program says which the different traits of all the six suspects. For example:
suspect(Hannibal Lecter,67,knife,'in good shape',mac,'high heels')
我在尝试实现线索时遇到问题
I'm having problems trying to implement the clue
or(suspect1, suspect2, suspect3, listOfSuspects)
该线索是说suspect1
具有与suspect2
或与suspect3
相同的特征,但不能同时具有两者的特征.示例:要表明50岁的犯罪嫌疑人有戒指或mac,但不能同时拥有:
This clue is supposed to say that the suspect1
has the same traits as suspect2
OR as suspect3
but not both. Example: To indicate that the suspect who is 50 years old has a ring or a mac, but not both:
or(suspect(_, 50, _, _, _, _),
suspect(_, _, _, _, ring, _),
suspect(_, _, _, _, mac, _), listOfSuspects).
任何帮助将不胜感激.
推荐答案
以下是一些提示.您可以声明Suspect1
具有与Suspect2
相同的特征,只是通过Suspect1 = Suspect2
,并通过member(Suspect1, Suspects)
来确定列表Suspects
的任何成员是否具有Suspect1
的特征.
Here are some hints. You can state that Suspect1
has the same traits as Suspect2
just by Suspect1 = Suspect2
, and to find out whether any member of a list Suspects
has the traits of Suspect1
by member(Suspect1, Suspects)
.
在Prolog中处理析取的通常方法是引入带有两个子句的谓词.例如,用于检查犯罪嫌疑人是否有戒指或Mac的谓词是
The usual way of handling a disjunction in Prolog is by introducing a predicate with two clauses. E.g., a predicate that checks whether a suspect has either a ring or a mac is
ring_or_mac(suspect(_, _, _, _, ring, _)).
ring_or_mac(suspect(_, _, _, _, mac, _)).
如果以正确的方式将这些提示放在一起,就可以解决您的问题.
If you put these hints together in the right way, you have a solution to your problem.
这篇关于“或"序言中的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!