0

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]))))
Julian
  • 23
  • 4
  • Are you defining a main function and using the design recipe? – Alex Knauth Feb 09 '17 at 21:36
  • Yes! I think so at least--the main function just needs a signature and a purpose statement. It takes in the world state and outputs the world state. I'm just not sure how to change the world state from a structure to a number right before the program ends. – Julian Feb 09 '17 at 22:36
  • More information: I am asking this question while making my first larger program, so I went ahead and typed up a much smaller version of my problem, which I'm using to experiment with what works. In this model, DrRacket outputs "(make-tc 3)" when it ends, but I want it to output something completely different like "hello". – Julian Feb 09 '17 at 22:59
  • This is not well designed. For one thing, your `main` doesn't respect its signature, since it returns a string and the signature says `TC`. Have you tried asking about this on Piazza? – Alex Knauth Feb 09 '17 at 23:51
  • So this isn't for outputting the score of the Typaholic game? Even if it's not your code should still respect your signature. – Alex Knauth Feb 11 '17 at 00:12
  • Yes! Your absolutely right (ended up deleting that comment since it wasn't helpful--sorry about that). -- I ended up going to office hours, and they helped me with the solution. What I was doing was too complicated, even though it worked in practice. I posted what I learned in the main post. – Julian Feb 11 '17 at 02:03
  • I would also recommend reading the section [Composing Functions](http://www.ccs.neu.edu/home/matthias/HtDP2e/part_two.html#%28part._sec~3acompounding2%29) in the book, and think about the "one function per task" principle and using a wish list. – Alex Knauth Feb 11 '17 at 04:12

1 Answers1

0

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]))))
Julian
  • 23
  • 4