Assignment 3

Due date: 2/2 @ 9:50 am

Problem 0 [5%]:

Find one interesting article from the weeks news on the use of software/computers in society. Summarize the article in your own words with a single 200-word paragraph, as a pair, one playing writer, the other playing editor. Add both the article and the summary in with the rest of your problem set.


Purpose:

The goal of this problem set is to practice designing data representations using unions of classes and functions for processing unions. You must follow the design recipe. The graders will look for data definitions, contracts, purpose statements, examples/tests, and properly organized function definitions. For the latter, you must design templates. You do not need to include the templates with your homework, however. If you do, comment them out. -- The problem set also contains problems concerning computation rules.


HtDP Problem:

7.2.2

Problem A1:

Develop structure and data definitions for bank accounts. The bank customer can open either a checking account or a savings account. A checking account requires a minimum deposit. Each savings account earns interest at a specified rate. All accounts have a unique number. Additionally, for each account the bank records the name of the owner of the account and the current balance.

Develop the function canWithdraw? that determines whether a customer can withdraw the specified amount from a bank account.


Problem A2:

Evaluate the following expressions step by step and write down next to each step whether it is (1) arithmetic (of any form), (2) function application ("plugging in") or (3) a conditional step:

  1. (define (dist-to-O x y)
    (sqrt (+ (* x x) (* y y))))
    
    (dist-to-O 3 4)
    
  2. (define-struct point (x y))
    
    (define (dist-to-O p)
      (sqrt (+ (sqr (point-x p)) (sqr (point-y p)))))
    
    (dist-to-O (make-point 3 4))
    
  3. (define (step x)
      (cond
        [(< 1 x) (sqr x)]
        [(< 0 x) (* 2 x)]
        [else    (sqr (+ x 1))]))
    
    (step 0)
    

For sanity checks, you may want to write down each step inside of drscheme. Since computation means calculating, you know that every step must produce the same answer. So if you're ever unsure whether you're calculation is still on track, just run the whole program and watch the same answer pop up for as many steps as you have written down.


Problem A3:

A web designer keeps a database of files she needs. The files can be text files, images, or sound files. For each file she records the name of the file (and some other information we do not care about here). For text files she records the number of lines. For images she records the image width and height, and whether the image is black-and-white, or colored. For sound file she records the sound duration in seconds.

Develop structure and data definitions for the web designer's files.

Develop the function size that computes the size of the web designer's file in bytes. Each character in a text file uses one byte, and each line has exactly 60 characters. Each pixel of a black-and-white image uses one byte, the color image needs three bytes per pixel. (The width and height data represent the number of pixels across and the number of pixels from the top to the bottom.) Each second of the playtime of a sound file consumes 30 bytes.


Problem A4: Rat Race Mini Project

In this problem you will design a simple iteractive game Rat Race. A black rat lives in a cage (on the screen) and moves around looking for food. There is one morsel of green cheese somewhere in the cage. When the rat finds the cheese, he eats it and grows. However, as the rat looks for food, he gets hungrier with each tick of the time and shrinks in size. The game ends when the rat dies of starvation. For now, we do not keep the score to see how long the rat lives, but you can add that feature as an extra (ugraded) option.

The game is shown as a scene of a fixed width and height (400 by 400). The rat is shown as a disk with the radius corresponding to its size. The cheese is a rectangle, it size represents the amount of food in it. For each 10 pixels square of cheese a rat eats, the rat's size grows by one. The rat can eat the cheese, when the rat's center is within the rectangle that represents the cheese. When the cheese is eaten, a new cheese morsel of random size appears at a new random location.

  1. Develop a data definition for a rat in a Rat Race game.
  2. Design the function rat-move that consumes one of the Symbols: 'up 'down 'left 'right and produces a rat that has moved 3 pixels in the given direction. If the user provides a Character, instead of a Symbol, or provides a different Symbol, the function just produces the same rat as was given. (You do not need to worry about rat going out of bounds.)
  3. Design the function rat-starve that decreases the size of the rat by one.
  4. Design the function rat-dead? that determiens whether the rat has starved to death (its size is one or less).
  5. Develop a data definition for a food in the Rat Race game.
  6. Design the function more-food that produces a new food of random size in a random location well within the given width and height. By 'well within' we mean that the food will be at least 30 pixels from the border. The width and height of the food should be between 5 and 15.

    Use to following (predefined) function to generate the needed random numbers:

      ;; to produce a random natural number between 0 and n
      ;; random: Nat -> Nat
      (define (random n) ...)
      
  7. Design the function can-eat? that determines whether a rat can eat the food.
  8. Design the function rat-eat that consumes a rat and a food that rat can eat, and produces a new 'fatter' rat.

    You can now use all your functions to complete the game. Start with the image of the background:

    ;; The rat world background
    (define WIDTH 400)
    (define HEIGHT 400)
    
    ;; the basic scene 
    (define IMAGE0 
      (place-image (rectangle WIDTH HEIGHT 'solid 'blue) 
                   (/ WIDTH 2) (/ HEIGHT 2) 
                   (empty-scene WIDTH HEIGHT)))
    
    
  9. Design the function rat-draw that consumes a rat and an image (scene) and places a circle with the radius equal to rat's size at the current rat's position.

    Use the example of a function places a solid red circle of size 10 at location (30 40) within the given scene as a guide:

    ;; draw a circle onto the given scene
    ;; circle-draw: Number Number Number Image -> Image
    (define (circle-draw radius x y scene)
      (place-image (circle radius 'solid 'red)
                   x y
                   scene))
    
  10. Design the function food-draw that places the food shown as a rectangle of green cheese in the given scene.
  11. The Rat World consists of one rat and one piece of the food that rat is looking for. Develop a data definition for the RatWorld and make an example of the intial world (call it world0).
  12. Design the function draw-rat-world that consumes a RatWorld and places the images of the rat and the food onto IMAGE0.
  13. Design the function rat-world-change that consumes a RatWorld and a KeyEvent (i.e. either a Character or a Symbol) and produces a new RatWorld where the rat has moved depending on the given KeyEvent value.
  14. We now have all the pieces we need to play the game. The last function considers the next step in the game as the clock ticks:

  15. Design the function rat-world-tick that consumes a RatWorld and produces a new world as follows:

    If the rat has found food, the new world consists of a rat that has eaten the food, and a new morsel of food.

    If the rat is dead, the answer clause is:

    (end-of-time "The rat died")

    Otherwise, the function produces a new world with the same food, and with the rat starved a bit.

Before you play the game make sure all your functions have been designed according to the DESIGN RECIPE -- with all the tests in place.

Here is the program that will play the game:

;;-------------------------------------------------------------------------
;; run program run 

(big-bang WIDTH HEIGHT 2 world0)
(on-redraw draw-rat-world)
(on-tick-event rat-world-tick)
(on-key-event rat-world-change)



Last modified: Sat Jan 27 10:59:00 EST 2007