【问题标题】:Basic Racket: End Values基本球拍:最终价值
【发布时间】:2017-02-09 19:51:53
【问题描述】:

我正在学习编程入门课程。我们在 DrRacket 中使用学生语言。

问题:我想在大爆炸游戏结束时返回某个值(需要 2htdp/universe)`。

当前输出:当游戏结束时,DrRacket 返回我当前的世界状态,这是我在游戏中使用的结构列表。

解决方案的进展:似乎stop-with 可以帮助我,但我不知道如何使用它。

TL;DR:
问题:游戏结束 --> 返回世界状态(结构列表)
Want: Game Ends --> 返回其他值(数字)

如果我能以任何方式澄清,请告诉我!谢谢!

编辑:我想我找到了解决方案。我使用我通常要求的表达方式来结束我的生活?函数并将其作为我的 on-tick 函数中的 cond 分支。当在我的 on-tick 函数中调用该函数时,它会将世界状态更改为我想要输出的任何内容。那么,到底我呢?函数,我只是检查一下世界状态是否与通常不同。

感谢您的帮助!

解决办法:

; A Test Case (TC) is a (make-tc Number)
(define-struct tc [number ticks])
; number is a number used to test this problem

; TC -> Number
; Begins the main big-bang function; outputs the inverse of tick speed
; times the number of ticks elapsed when the game ends. 
(define (main tick-speed)
   ( * (/ 1 tick-speed) 
        (tc-ticks (big-bang (make-tc 0 0)
           [to-draw draw]
           [on-tick check tick-speed]
           [stop-when end? end-scene]))))

【问题讨论】:

  • 您是否定义了主要功能并使用了设计配方?
  • 是的!我认为至少是这样——主要功能只需要一个签名和一个目的声明。它接收世界状态并输出世界状态。我只是不确定如何在程序结束之前将世界状态从结构更改为数字。
  • 更多信息:我在制作我的第一个更大的程序时提出了这个问题,所以我继续输入我的问题的一个小得多的版本,我用它来试验什么有效。在这个模型中,DrRacket 在结束时输出“(make-tc 3)”,但我希望它输出一些完全不同的东西,比如“hello”。
  • 这不是很好的设计。一方面,您的main 不尊重其签名,因为它返回一个字符串并且签名显示TC。你试过在 Piazza 上问这个问题吗?
  • 所以这不是输出Typaholic游戏的分数吗?即使不是你的代码也应该尊重你的签名。

标签: racket racket-student-languages


【解决方案1】:

在原帖中回答:

; A Test Case (TC) is a (make-tc Number)
(define-struct tc [number ticks])
; number is a number used to test this problem

; TC -> Number
; Begins the main big-bang function; outputs the inverse of tick speed
; times the number of ticks elapsed when the game ends. 
(define (main tick-speed)
   ( * (/ 1 tick-speed) 
        (tc-ticks (big-bang (make-tc 0 0)
           [to-draw draw]
           [on-tick check tick-speed]
           [stop-when end? end-scene]))))

【讨论】:

    猜你喜欢
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多