在 Casting SPEL 中推送项目位置

发布于 2024-10-16 00:35:54 字数 974 浏览 2 评论 0原文

我正在经历 在 Lisp 中铸造 SPEL,这是处理拾取对象的建议解决方案:

(define *location* 'living-room)

(define *object-locations*
  '((whiskey-bottle living-room)
    (bucket living-room)
    (chain garden)
    (frog garden)))

(define (pickup-object object)
  (cond [(is-at? object *location* *object-locations*)
         (push! (list object 'body) *object-locations*)
         (string-append "You're now carrying the " (symbol->string object) ".")]
        [else "There's no such object in here."]))

我是唯一一个觉得这效率低下的人吗?据我了解,push!函数cons每次都会有一个新的*object-locations*玩家拿起一个物体。虽然这在像这样的小游戏中可能不是一个主要问题,但如果添加从库存中放下物品的选项,*object-locations* 列表可能会无限增长......例如,pickup-object 不应该替换 (威士忌瓶客厅)cdr,而不是添加另一个副本配对

我是 Lisp 新手,可能会弄错...有人可以解释一下我的假设是否正确,如果是,那么在 Lisp 文本冒险中处理拾取对象的最佳方法是什么?

I'm going through Casting SPELs in Lisp and this is the proposed solution to handling picking up objects:

(define *location* 'living-room)

(define *object-locations*
  '((whiskey-bottle living-room)
    (bucket living-room)
    (chain garden)
    (frog garden)))

(define (pickup-object object)
  (cond [(is-at? object *location* *object-locations*)
         (push! (list object 'body) *object-locations*)
         (string-append "You're now carrying the " (symbol->string object) ".")]
        [else "There's no such object in here."]))

Am I the only one who finds this inefficient? As far as I understand the push! function conses a new pair to *object-locations* every time the player picks up an object. While this may not be a major problem in a small game like this, if one would to add the option of putting down items from the inventory, the *object-locations* list could grow infinitely... Shouldn't pickup-object replace the cdr of (whiskey-bottle living-room), for example, instead of adding another copy of the pair?

I'm new to Lisp and may be mistaken... Could somebody please explain whether my assumptions are right, and if so, what would be the best way to handle picking up of objects in a Lisp text adventure?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无戏配角 2024-10-23 00:35:54

代码存在一些问题:

  • 列表 *object-locations* 是一个文字。不应修改文字。你不能破坏性地改变青蛙的位置。所以你需要把一个新的位置推到前面。
  • 随着您玩游戏,该列表会不断增长。
  • STRING-APPEND 为每个拾取操作创建一个新字符串。

  • 对于书本示例来说,它很简单且足够了。
  • 对象位置的堆栈将使某种撤消成为可能。
  • 项目和位置的新关联的推送是快速操作。
  • 它为本书的读者留下了提高代码效率的机会。

在 Common Lisp 中,很容易更改:

(defvar *object-locations*
  (copy-tree
   '((whiskey-bottle living-room)
     (bucket living-room)
     (chain garden)
     (frog garden))))

(defun get-location (object)
  (second (assoc object *object-locations*)))

(defun set-location (object location)
  (setf (second (assoc object *object-locations*))
        location))

CL-USER > (get-location 'frog)
GARDEN

CL-USER > (set-location 'frog 'living-room)
LIVING-ROOM

CL-USER > (get-location 'frog)
LIVING-ROOM

CL-USER > *object-locations*
((WHISKEY-BOTTLE LIVING-ROOM)
 (BUCKET LIVING-ROOM)
 (CHAIN GARDEN)
 (FROG LIVING-ROOM))

请参阅 Common Lisp:符号计算的温和介绍一书David S. Touretzky 对 Lisp 进行了非常基本的介绍。

There are a few problems with the code:

  • The list *object-locations* is a literal. Literals should not be modified. You can't change the location of frog destructively. So you need to push a new location in front.
  • The list grows as you walk through the game.
  • STRING-APPEND creates a new string for each pickup action.

But

  • It is simple and sufficient for a book example.
  • The stack for object locations would make some kind of undo possible.
  • The push of a new association of item and location is a fast operation.
  • It leaves the opportunity for the reader of the book to make the code more efficient.

In Common Lisp that's easy to change:

(defvar *object-locations*
  (copy-tree
   '((whiskey-bottle living-room)
     (bucket living-room)
     (chain garden)
     (frog garden))))

(defun get-location (object)
  (second (assoc object *object-locations*)))

(defun set-location (object location)
  (setf (second (assoc object *object-locations*))
        location))

CL-USER > (get-location 'frog)
GARDEN

CL-USER > (set-location 'frog 'living-room)
LIVING-ROOM

CL-USER > (get-location 'frog)
LIVING-ROOM

CL-USER > *object-locations*
((WHISKEY-BOTTLE LIVING-ROOM)
 (BUCKET LIVING-ROOM)
 (CHAIN GARDEN)
 (FROG LIVING-ROOM))

See the book Common Lisp: A Gentle Introduction to Symbolic Computation by David S. Touretzky for a really basic introduction to Lisp.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文