1

我正在编辑以下游戏,还有 2 个错误;两者都是粗体和斜体的“!”。(在此论坛中以 * 列出)。在使用它们的所有其他时间都不会发生这种情况,我已经搜索了其他答案,删除了空格等,但似乎找不到答案。如果您有任何建议,请告诉我!谢谢 :)

第一个错误出现在标题为“门”的部分和标题为“键”的部分之后

/*
  This is a little adventure game.  There are three
  entities: you, a treasure, and an ogre.  There are
  six places: a valley, a path, a cliff, a fork, a maze,
  and a mountaintop.  Your goal is to get the treasure
  without being killed first.
*/



/*
  First, text descriptions of all the places in
  the game.
*/
description(valley,
  'You are in a pleasant valley, with a trail ahead.').
description(path,
  'You are on a path, with ravines on both sides.').
description(cliff,
  'You are teetering on the edge of a cliff.').
description(fork,
  'You are at a fork in the path.').
description(maze(_),
  'You are in a maze of twisty trails, all alike.').
description(mountaintop,
  'You are on the mountaintop.').
description(gate,
  'You are at the gate.').
description(teleport,
  'you teleport to the gate.').
description(key,
  'you stuble on a rock and look under it.').
description(didntdropkey,
  'dont forget to drop the .....').




/*
  report prints the description of your current
  location.
*/
report :-
  at(you,X),
  description(X,Y),
  write(Y), nl.

assert(at(you,cliff)).
report.
retract(at(you,cliff)).
assert(at(you,valley)).
report.



/*
  These connect predicates establish the map.
  The meaning of connect(X,Dir,Y) is that if you
  are at X and you move in direction Dir, you
  get to Y.  Recognized directions are
  forward, right, and left.
*/
connect(valley,forward,path).
connect(path,right,cliff).
connect(path,left,cliff).
connect(path,forward,fork).
connect(fork,left,maze(0)).
connect(fork,right,gate).
connect(gate,right,mountaintop).
connect(gate,left,didntdropkey).
connect(gate,forward,didntdropkey).
connect(maze(0),left,maze(1)).
connect(maze(0),right,maze(3)).
connect(maze(1),left,maze(0)).
connect(maze(1),right,maze(2)).
connect(key,left,teleport).
connect(key,right,maze(0)).
connect(maze(3),left,maze(0)).
connect(maze(3),right,maze(3)).

/*
  move(Dir) moves you in direction Dir, then
  prints the description of your new location.
*/
move(Dir) :-
  at(you,Loc),
  connect(Loc,Dir,Next),
  retract(at(you,Loc)),
  assert(at(you,Next)),
  report,
  !.
/*
  But if the argument was not a legal direction,
  print an error message and don't move.
*/
move(_) :-
  write('That is not a legal move.\n'),
  report.

/*
  Shorthand for moves.
*/
forward :- move(forward).
left :- move(left).
right :- move(right).

/*
  If you and the ogre are at the same place, it
  kills you.
*/
ogre :-
  at(ogre,Loc),
  at(you,Loc),
  write('An ogre sucks your brain out through\n'),
  write('your eye sockets, and you die.\n'),
  retract(at(you,Loc)),
  assert(at(you,done)),
  !.
/*
  But if you and the ogre are not in the same place,
  nothing happens.
*/
ogre.

/*
  If you and the treasure are at the same place, you
  win.
*/

key :-
  at(you,key),
  write('you have found the key'),
  retract(at(you,maze(2)),
  assert(at(you,key)),
  ***!.***

key.


  /*
 this print out that you're at the key need to
 add the key to be used later.
*/
/* this is the teleport function this action is taken if you get the key and
turn right then you teleport to the gate*/

teleport :-
    at(you,teleport),
    write('you teleported to the gate'),
    retract(at(teleport)),
    assert(at(you,gate)),
    !.
teleport.

didntdropkey :-
  at(you,didntdropkey),
  write('you didnt drop the key and you were stuck by lightning.\n'),
  retract(at(you,dintdropkey)),
  assert(at(you,done)),
  !.
didntdropkey.


treasure :-
  at(treasure,Loc),
  at(you,Loc),
  write('There is a treasure here.\n'),
  write('Congratulations, you win!\n'),
  retract(at(you,Loc)),
  assert(at(you,done)),
  !.
/*
  But if you and the treasure are not in the same
  place, nothing happens.
*/
treasure.

/*
  If you are at the cliff, you fall off and die.
*/

gate :-
at(you,gate),
write('You use the key found earlier to open the gate.\n'),
move(at(you, mountaintop),
move(at(you, treasure),
***!.***
 gate.


cliff :-
  at(you,cliff),
  write('You fall off and die.\n'),
  retract(at(you,cliff)),
  assert(at(you,done)),
  !.
/*
  But if you are not at the cliff nothing happens.
*/
cliff.

/*
  Main loop.  Stop if player won or lost.
*/
main :-
  at(you,done),
  write('Thanks for playing.\n'),
  !.
/*
  Main loop.  Not done, so get a move from the user
  and make it.  Then run all our special behaviors.
  Then repeat.
*/
main :-
  write('\nNext move -- '),
  read(Move),
  call(Move),
  ogre,
  treasure,
  gate,
  cliff,
  main.

/*
  This is the starting point for the game.  We
  assert the initial conditions, print an initial
  report, then start the main loop.
*/
go :-
  retractall(at(_,_)), % clean up from previous runs
  assert(at(you,valley)),
  assert(at(ogre,maze(3))),
  assert(at(treasure,mountaintop)),
  write('This is an adventure game. \n'),
  write('Legal moves are left, right, or forward.\n'),
  write('End each move with a period.\n\n'),
  report,
  main.
4

1 回答 1

1
retract(at(you,maze(2)),
                       ^
move(at(you, mountaintop),
                         ^
move(at(you, treasure),
                      ^

你在这些地方缺少一个关闭的括号。这使得 Prolog 认为这些子句不会在剪切处结束。

于 2013-05-14T18:32:41.397 回答