[plt-scheme] newbie: student-riot exercise

From: arnuld (arnuld3 at gmail.com)
Date: Thu Jan 26 04:57:04 EST 2006

hello everyone,
                               i have created a programme which works fine
and not in the way intended by problem statement. here is the problem
statement copied as it is from HtDP.
*
-- Exercise 11.3.5.*   Develop a program that visualizes a student riot. In
preparation of a student riot, a small group of students meets to make
paint-filled balloons. The typical riot uses 'red only. Then, on the evening
of the riot, the students enter a university's progressive theater with the
balloons and throw them all over the seats.

 The program's only input should be a natural number, which represents the
number of balloons thrown. The visualization should use a canvas that
contains a black grid and the positions of the balls.Assume a random
distribution of the balls over the theater's seats. Each box in the grid
represents a seat. Configure the program so the change of one variable
definition changes the number of columns in the grid and a change to another
changes the number of rows.

*Hint:* Develop auxiliary functions that draw some given number of lines in
the vertical and the horizontal direction.  ----
my solution works not with function call :                   (riot n)
but with
(draw-baloons (riot n))

also < riot > consumes only one agument but i had to use radius of disk (red
baloons) as an extra in its body as shown down here.

may you help me on modifying <riot > according to the problem statement?

"arnuld"

---------------------- start of programme
-------------------------------------------
(define ROWS 5)
(define COLS 5)
;; i created variable definitions of COLS & ROWS because problem wants so
;; hence you can change the values of COLS & ROWS here in variable
definitions and effect will be
;; transfered into the variables & functions which use these 2 variables
;; same is for canvas

(define canvas-x 300)
(define canvas-y 300)
(define dist-ROWS (quotient canvas-y ROWS))
(define dist-COLS (quotient canvas-y COLS))
(define radius 5)

(define (riot n)
  (cond
    [(zero? n) empty]
    [else (cons radius (riot (sub1 n)))]))

(define (draw-balloons a-lon)
  (cond
    [(empty? a-lon) true]
    [else (draw-all a-lon)]))

(define (draw-all a-lon)
  (and (draw-solid-disk  (make-posn (random canvas-x)
                                    (random canvas-y))
                         (first a-lon)
                         'red)
       (sleep-for-a-while 1)
       (draw-balloons (rest a-lon))))

;; ------------ draw columns and rows here -------------------------------

(define (draw-COLS COLS)
  (cond
    [(zero? COLS) true]
    [else (and (draw-solid-line (make-posn (* COLS dist-COLS) 0)
                                (make-posn (* COLS dist-COLS) canvas-y)
'black)
               (draw-COLS (sub1 COLS)))]))

;; change the values of COLS & ROWS in variable definitions and effect will
be here into these
;; functions

(define (draw-ROWS ROWS)
  (cond
    [(zero? ROWS) true]
    [else (and (draw-solid-line (make-posn 0 (* ROWS dist-ROWS))
                                (make-posn canvas-x (* ROWS dist-ROWS))
'black)
               (draw-ROWS (sub1 ROWS)))]))

;; tests
(start canvas-x canvas-y)
(draw-COLS COLS)
(draw-ROWS ROWS)

(draw-balloons (riot 20))

(sleep-for-a-while 1)
(stop)

------------------------ end of programme
------------------------------------------

--
"the great intellectuals"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20060126/6f248d48/attachment.html>

Posted on the users mailing list.