在 Casting SPEL 中推送项目位置
我正在经历 在 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 cons
es 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代码存在一些问题:
但
在 Common Lisp 中,很容易更改:
请参阅 Common Lisp:符号计算的温和介绍一书David S. Touretzky 对 Lisp 进行了非常基本的介绍。
There are a few problems with the code:
But
In Common Lisp that's easy to change:
See the book Common Lisp: A Gentle Introduction to Symbolic Computation by David S. Touretzky for a really basic introduction to Lisp.