Help printing picts for a wordle clone

I'm writing a wordle clone in racket. I'm using the pict module to draw colored rectangles around letters.

(define (letter char (color "green"))
  (cc-superimpose
   (colorize (filled-rectangle 40 50) color)
   (text (string-upcase (string char)) null 24 0)))

When I call this function from the top-level of the program, I can print out decorated words that display in the DrRacket REPL:

(define (graphical-test-2 word)
  (map letter (string->list word)))

(graphical-test-2 "hello")

However, if I try to call this from a game loop, nothing prints from inside the loop, only the final result prints:

(define (graphical-test-3)
  (do ((i 0 (add1 i)))
    ((> i 5))
    (graphical-test-2 "hello"))
  (graphical-test-2 "world"))

(only "WORLD" prints in colored letters).

If I print a string instead of displaying a pict, I can print from the inner loop:

(define (graphical-test-4)
  (do ((i 0 (add1 i)))
    ((> i 5))
    (printf "hello~n"))
  (printf "world~n"))

(prints 5 "hello"s and one "world").

Is there an easy way to get the picts from inside the loop to display as well?

1 Like

The key to understanding what is happening is knowing that graphical-test-2 does not actually display anything!

graphical-test-2 is a function which returns a single value, a picture, and it is the DrRacket REPL that displays the picture. This means that the function graphical-test-3 produces 5 pictures wit the word "hello", discards them, than returns the last one with the word "world". The text only equivalent is using format, not printf, since printf actually displays the text and returns void.

To show all the pictures, you can put all the intermediate pictures in a list and return that:

(define (graphical-test-5)
  (append
   (for/list ([_ (in-range 5)])
     (graphical-test-2 "hello"))
   (list (graphical-test-2 "world"))))

image

Alex.

5 Likes

Thanks, this is very helpful. I think I have some ideas on how to rewrite the interactive loop so that I can return the picts to the REPL.

Will post results here when I've made some progress.

1 Like

This might be a really lovely example for 2htdp/universe. Basically: you define click-handlers, key-handlers, a function that maps a program state to an image, and away you go.

Yeah, actually, this would be kind of a perfect fit for 2htdp/image....

2 Likes

Excellent. The simple REPL version works nicely now. When I have a sec, I'll clean up the code, push it to github, and then start reading about 2htdp/universe.

Thanks for the suggestion!

1 Like

I got the REPL version of my code working with the 2htdp/image libraries. The code is available here on github.

This weekend, or as soon as I have a bit of time, I'm going to dive into the 2htdp gui libs.

1 Like