racketracket-student-languages

Basic Racket: End Values


I'm taking an intro programming class. We use the student languages in DrRacket.

Problem: I would like to return a certain value at the end of a big-bang game (require 2htdp/universe)`.

Current Output: When the game ends, DrRacket returns my current worldstate, which is a list of structures that I use in the game.

Progress towards solution: It seems like stop-with may be able to help me, but I'm not sure how to use it.

TL;DR:
Problem: Game Ends --> Returns World State (List of Structures)
Want: Game Ends --> Return Other Value (Number)

Let me know if I can clarify in any way! Thanks!

EDIT: I think I found the solution. I use the expression that I usually call for my end? function and put it instead as a cond branch in my on-tick function. When that function is called in my on-tick function, then it changes the world-state to whatever I want to output. Then, in my end? function, I just check to see whether the worldstate is something different than it usually is.

Thanks for the help!

Solution:

; 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]))))

Solution

  • Answered in the original post:

    ; 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]))))