RU beehive logo ITEC dept promo banner
ITEC 380
2009fall
ibarland

homeinfolecturesexamshwsarchive

hw03
structs in structs
due Sep.28 (Mon)

Due Sep.28 (Mon) (hardcopy, and on WebCT) Your name and the assignment-number must be in a comment at the start of the file.

The following problems deal with frogger. Follow all steps of the design recipe (from lect02c—design recipe; functions returning structs and/or How to Design Programs. However, you do not need to provide test cases for functions which return images/scenes.

  1. Create a “world” structure, which contains a princess and two trucks. Make two examples of the data. Name at least one of them (say, “w0”), so you can use it for test-cases later on.
  2. Write the method update-world : world -> world, which returns a new world after one tick has passed.
    (It goes without saying1, that you'll lose points if you don't call the update/move functions you wrote for last week's homework (or solution) as helpers.)

    Note that updating the world structure has nothing to do with drawing it.

  3. Write the method

    ; draw-truck : truck, scene -> scene
    ; Return a scene just like the one given,
    ; except with the truck overlayed onto it.
    
    A scene is the same thing as an image, except that instead of using overlay (or overlay/xy) as in hw01, I suggest using:
    ; overlay/xy:nw : image number number image -> scene
    ; Overlay `pic` onto `bg`,
    ; where `pic`s northwest corner is offset by `x`,`y`.
    ; The result is clipped to `bg`s dimensions.
    ;
    ; (This function is just like overlay/xy, except that all images 
    ; use their northwest corner as their reference point ("pinhole").
    ; The result also has its pinhole in the nw corner (i.e., it's a 'scene').)
    ;
    (define (overlay/xy:nw bg x y pic)
      (place-image (put-pinhole pic 0 0) x y (put-pinhole bg 0 0)))
    
    (where the two numbers are the x,y offset from the upper-left corner of the scene). This function can be found in image-functions.ss.

    No check-expects are needed for the drawing-functions (since it's hard to create the result-images before you write your function). To make sure your function works, test your function, you can try drawing a particular truck onto an empty scene, or a non-empty scene:

    ; evaluates to a scene of a lone truck:
    (draw-truck (make-truck ) (empty-scene 200 300))
    
    ; evaluates to a scene of a truck and an orange circle:
    (draw-truck (make-truck ) 
                (overlay/xy:nw (circle 40 'solid 'orange) 
                               100
                               120
                               (empty-scene 200 300)))
    

  4. A note on images:

    1. No copyrighted images (unless you have permission).
    2. Good sources are clip-art websites with free samples, like www.free-clipart-pictures.net. Be sure to always credit the source in your code. If you find a good site, please share it on the WebCT discussion boards.
    3. you can paste images into your code into drscheme using Scheme > Insert image....
    4. if you want to scale an image larger or smaller, you can call the functions found in image-functions.ss.
    5. Similar rules for sound effects, if you want to add those; some free sounds via Creative Commons License are at: sound-effects
    I suggest making your images substantially smaller than the demo version I've shown in class.

  5. Write the method draw-princess : princess, scene -> scene, which either returns the scene with the princess placed on it, or (if her time is up2) just returns the scene unchanged. (Use if or cond.)
  6. Write the method draw-world : world -> scene, which creates an empty-scene and then draws its contents onto that.
  7. If you evaluate
    (big-bang w0
              [on-tick update-world]
              [on-draw draw-world])
    
    (where w0 is one of the example world structures you made for #1), you should see your two trucks move across the scene as the princess watches (before she disappears).
  8. Write the method

    truck-handle-key : truck key-event -> truck
    so that if passed the key "+" the truck speeds up, if given "-" it slows down, otherwise the truck is unchanged.

    Note that a key is either a string of length 1 (like "a" or "+"), or it is one of the special values "left", "right", "up", "down", or one of a few other options. You can use the function3 key=?. and the predicate key-event?

    (Don't forget to follow the design recipe; create test cases before you write the code! You should be clear before you start writing code, that this method doesn't change the truck's location at all.)

  9. Write the method world-handle-key : world key -> world. Note that only trucks worry about keypresses. Following the design recipe, this should be straightforward; you need only three add'l words added to your template for any world-handling function.
  10. Call big-bang again, this time adding the clause [on-key world-handle-key]. You should be able to notice the trucks speeding up or slowing down.
  11. Write the method overlap?, to tell if two bounding-boxes overlap.
    cool hint, from robotics:: We can reduce this problem to a simpler one: checking if an expanded version of the first rectangle merely contains one particular point: the southeast corner of the second rectangle.
    For example: consider a 20x30 rectangle whose northwest corner is at (500,400) and a second rectangle which is 80x60. These two rectangles overlap exactly when the second rectangle's southeast corner is inside the (20+80)×(30+60) rectangle rooted at (500,400).
    Some test cases are given here, but you must make at least two more which test different situations.
    ; The eight inputs are: x,y,width,height of two rectangles.
    (check-expect (overlap?  0  0 10 10   5  5  2  2) true)
    (check-expect (overlap?  0  0 10 10   5  5 20  2) true)
    (check-expect (overlap?  0  0 10 10  15 15  2  2) false)
    
  12. (0pts) Add a frog to your world. (This is 0pts because it won't actually be due until next week.)
  13. Book #3.5: Give a BNF description for Java's boolean expressions involving &&, ||, !, and the relational operators.
  14. Book #3.6: give a parse tree for (a) and (b), and a derivation for (c).
  15. Book #3.9: Add unary minus (to effectively get higher precedence than +,*) to the grammar:
    <assign> → <id> = <expr>
    <id> → A | B | C
    <expr> → <expr> + <term> | <term>
    <term> → <term> * <factor> | <factor>
    <factor> → ( <expr> )  |  <id>
    
  16. Book #3.11: Which strings derivable from the grammar?
    1. baab
    2. bbbab
    3. bbaaaaa
    4. bbaab

1Don't you hate it, when people say that?      

2 Alternately, you might have have update-princess teleport the princess and reset her timer, in which case you know that the structure is always “valid” so you don't need to check it here in draw-princess. That's overall better from a design standpoint, since validity-checking is not something that the drawing method should be bothering with.      

3Although you could also use string=? and string? just fine, it's more appropriate to use the more specific functions, whenever you know your data is a key-event.      

homeinfolecturesexamshwsarchive


©2009, Ian Barland, Radford University
Last modified 2009.Oct.17 (Sat)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme