【发布时间】:2020-01-05 18:57:16
【问题描述】:
我需要在 Prolog 中编写一个程序,让一个人去加油站,拿一杯苏打水和一份报纸,然后回到他的车上,结果应该是这样的:
?- go.
>> goto(gas_station).
You are in the gas_station.
>> goto(car).
You can't get there from here.
>> open(car_door).
>> open(gas_station_door).
>> take(soda).
You now have a soda.
>> goto(car).
You are in the car
Thanks for getting the newspaper.
这是我到目前为止所做的:
place(car).
place(gas_station).
item(player , soda).
item(player , newspaper).
at(soda , gas_station).
at(newspaper , gas_station).
at(player , gas_station) :- door(car_door , open),
door(gas_station_door, open), nl.
at(player , gas_station) :-
write('Can't get there'), nl.
open(X) :-
assert(door(X , open).
goto(X) :-
at(player , X),
retract(at(player , X),
write('You are in the gas station.'),
nl.
take(X) :-
item(player , X),
write('You now have a soda and a newspaper'),
nl.
我是 Prolog 的新手,我只想知道到目前为止我所做的是否正确,我是否在正确的轨道上,以及如何从这里继续,因为我是堆栈,我不确定它是否有效应该怎么做或如何让那个人回到车上,我会很感激任何帮助,谢谢。
【问题讨论】:
-
是David Matuszek的课程吗?看看Example on Github 公平吗?该示例不使用读取循环,它只是修改事实数据库,用户直接输入操作作为查询
n、e、w、s等。非常非单调的逻辑。这是一种方法。 -
assert和retract语句肯定缺少一些括号。 -
也许您正在寻找planner 解决方案。