0

首先感谢您花时间考虑我的问题。我正在用 prolog 编写一个冒险风格的游戏,作为尝试学习它的一种方式。我写了以下代码片段:

i_am_at(melba_market_square).

exits :-
    i_am_at(X),
    path(X, Y, Z),
    write('Exits: '), nl, 
    write(Y), write(': '), write(Z), nl.

path(melba_market_square, s, melba_armory).
path(melba_market_square, n, melba_main_st_s).
path(melba_market_square, w, melba_sidra_alley_s).

我无法弄清楚/找到如何做的是让“exits”命令一次返回所有 3 条路径,而不是让我每次都按空格键。任何帮助,将不胜感激。非常感谢。

4

2 回答 2

1

好的,多亏了这里发布的帮助,我才弄清楚了。这是我的代码:

i_am_at(melba_market_square).

exits :-
    i_am_at(X),
    path(X, Y, Z),
    write('Exits: '), nl, 
    write(Y), write(': '), write(Z), nl.

path(melba_market_square, s, melba_armory).
path(melba_market_square, n, melba_main_st_s).
path(melba_market_square, w, melba_sidra_alley_s).

zzz :- /* This implements "User"'s where_to_go rule. */
    i_am_at(X),
    where_to_go(X, IntoDirections),
    write('Exits: '), write(IntoDirections), nl.

where_to_go(From, IntoDirections) :- 
    findall(Direction, path(From, Direction, _), IntoDirections).
于 2013-11-12T22:00:12.427 回答
0

我没有尝试过,但我认为它应该是这样的:

where_to_go(From, IntoDirections) :- findall(Direction, path(From, Direction, _), IntoDirections).

IntoDirections应该列出你可以去的所有方向。

于 2013-11-09T18:08:46.647 回答