馬克是正確的金錢。你可能想用這樣的事情來替代你的整個代碼:
:- dynamic at/2, holding/1.
at(player, room1).
at(revolver, room1).
take(X) :-
at(player, Place),
at(X, Place),
!,
format('You pick up the ~a.~n', [X]),
retract(at(X,Place)),
assert(holding(X)).
take(X) :-
holding(X),
!,
format('You''re already holding the ~a!~n', [X]).
有很多,你可以進一步採取這種有趣的方式。操作員is_at
可能使代碼更易讀:
take(X) :-
player is_at Place,
X is_at Place,
...
你還可以有獲得物品和諸如此類的東西右一些不錯的案例推理:
subject(X, Some_X) :- mass_noun(X), !, atom_concat('some ', X, Some_X).
subject(X, The_X) :- atom_concat('the ', X, The_X).
mass_noun(water).
,那麼你可以將這些融入輸出套路:
take(X) :-
...
subject(X, Subj),
format('You take ~a.~n', [Subj]),
...
你可以做一些DCG中有趣的東西來生成輸出過:
:- use_module(library(dcg/basics)).
success(take(X)) --> "You took ", subject(X).
subject(X) --> "the ", atom(X).
你可以甚至更通用的,像這樣的一些表演:
success_term(Command) --> { Command =.. CommandList }, success(CommandList).
success([Command, DirectObject]) -->
"You ", past_tense(Command), " ", subject(DirectObject), ".".
subject(Noun) --> "the ", atom(Noun).
past_tense(X) --> { past_tense(X, Y) }, atom(Y).
past_tense(take, took).
past_tense(X, Xed) :- atom_concat(X, 'ed', Xed).
然後運行像這樣:phrase(success_term(take(revolver)), X), format('~s~n', [X])
,你就會得到You took the revolver.
,這是一種整潔。
這些文字冒險很多有趣的代碼。如果你還沒有,我建議你通過Amzi Prolog Nani Search tutorial。那裏有很多很棒的點子!
當你說「當我運行我的代碼時......」究竟是什麼意思?你輸入了什麼?我玩了你展示的代碼,它的行爲和我所期望的一樣。如果你輸入'take(左輪手槍)',你需要記住有多個「解決方案」。第一個會寫「你拿起左輪手槍」,然後第二個(如果你用';'來要求)會修改數據。 – lurker
當我運行的代碼,左輪手槍正在從'Place'收回,但我怎麼能阻止消息當用戶再次使用'拿(左輪)''查詢您拿起被再次顯示revolver.' ? – Shevliaskovic
取(X): - i_am_holding(X), 寫( '已經特殊照顧抱着它!'), NL。 我明白了。無論如何感謝 – Shevliaskovic