8.14
1.2.8 Viewer
Source code at viewer.rkt
The viewer displays an animation of the game actions.
To display an entity, we assign an entity symbol. The bot symbol is different if it is laden with a block.
(test-case: "entity symbols" (check-equal? (entity-symbol type-bot #f) #\□) (check-equal? (entity-symbol type-bot 'laden) #\▣) (check-equal? (entity-symbol type-block #f) #\■) (check-equal? (entity-symbol type-base #f) #\▤) (check-equal? (entity-symbol type-edge #f) #\?))
Only bases, bots,and blocks should appear. Anything else is a mistake!
(define (entity-symbol entity-type laden?) (cond [(= entity-type type-bot) (if laden? #\▣ #\□)] [(= entity-type type-block) #\■] [(= entity-type type-base) #\▤] [else #\?]))
The animation can be paused and resumed with a mouse click.
(define run-actions #t)
(define my-canvas% (class canvas% (define/override (on-event event) (when (send event button-down?) (set! run-actions (not run-actions)))) (super-new)))
The animation is displayed.
(define (viewer title draw-procedure action-procedure #:size [size 50] #:style [style '()] #:run [run #t]) (define count 0) (set! run-actions run) (let* ([frame (new frame% [label title] [style style] [width (* 10 size)] [height (+ (* 11 size) 22)])] [font (make-font #:face "DejaVu Sans Mono")] [canvas (new my-canvas% [parent frame] [paint-callback (λ (canvas dc) (send dc set-font font) (define (draw-entity entity-type laden? x y #:color [color "black"]) (send dc set-text-foreground color) (send dc draw-text (string (entity-symbol entity-type laden?)) (* 10 x) (- (* 11 y) 6))) (draw-procedure draw-entity))])]) (define (iterate) (if run-actions (begin (action-procedure) (send frame set-status-text (number->string count)) (send canvas refresh-now #:flush? #t) (set! count (add1 count))) (sleep 0.001)) (iterate)) (send frame create-status-line) (send frame show #t) (thread iterate)))