CSci 1301: Takehome 2

Due: Tuesday, November 21st at 11:59pm by e-mail (no late work accepted)

Total: 36 points

This is individual work. You are not allowed to spend more than 8 hours on this test. This includes any reading that you do to help you solve the problems.

Make sure to write your name in the file(s) you submit. You may submit one or two files.

Use helper functions as needed. All your functions (including all those you use in big-bang) must have documentation with the types of parameters and results, a function description, and, whenever possible, check-expect tests.
When defining your own structures, clearly explain what all the fields mean.
Comments, helpful variable names, and good code style and formatting are a very important part of your grade. You don't have to look for the shortest solution, but you should eliminate unnecessary code repetition if possible (use helper functions).

Task 1 (6 points): determine if a list has an even number of elements

Write a function even-length? that consumes a list and returns #true if the list has an even number of elements and #false otherwise. Do not use the function length. The function must pass the following check-expects:


(check-expect (even-length? (list 1 2 3)) #false)
(check-expect (even-length? (list 2 1)) #true)
(check-expect (even-length? (list 2)) #false)
(check-expect (even-length? (list "hi")) #false)
(check-expect (even-length? (list "hi" "bye")) #true)
(check-expect (even-length? empty) #true)
  
Think of how the return value of the function changes when one element is added to the list: if you know the result for the rest of the list, what is the result for the entire list?

Task 2 (30 points): a world problem

Your task is to write a world in which the world state is a list of structures representing boxes. The structures are defined as follows:


    (define-struct box [side x y])
    ;; Interpretation: side is the length of the side of a box,
    ;; x and y are the coordinates of the center of the box.

The world starts off with an empty list of boxes, and then boxes are added to form a tower in which each next box is added on top of the previous ones.

Assuming the canvas of 400 by 400, the list of boxes


    (list (make-box 80 200 170) (make-box 90 200 255) (make-box 100 200 350))

will produce the following picture:

You may use this list as the starting state for testing.

Your goal is to implement the following:

  1. The world in which the world state is a list of boxes, nothing changes on a clock tick, and the list of boxes is drawn on the canvas, as shown above (pick any color you want for your boxes).
  2. A function height that takes a list of boxes and returns the height of the tower of these boxes. For instance, for the above list the function returns 270.
    Make sure to write at least three check-expects for this function and a signature before writing the code.
  3. After finishing the first two tasks implement a key handling function that, when a key "a" is pressed, adds a new box to the list (at the beginning). The side of the new box is 10 pixels smaller than the side of the top (i.e. first) box in the tower. Think carefully of what its y coordinate is: For the list of boxes given above the new box would be (make-box 70 200 95). If your rendering function works as it should, the picture would look like this:

    Adding one more box will give you the following image, produced from a list of 5 box structures:

    If there are currently no boxes in the list, then the box at the bottom of the screen with the x coordinate 200 and side 100 is added.
    Note: use named constants for constant properties of the canvas and boxes, such as canvas width and height and the side of the largest box at the bottom of the canvas.
  4. Add key handling for a space bar so that pressing it returns the world to an empty list of boxes.
  5. Add a mouse event handler so that when the top box is clicked, it is removed from the list. It may be a good idea to use a helper function to check if a box is clicked.
  6. Extra credit, 8 points (only after everything else is finished): change the click handler so that when any box is clicked, it is removed, and the tower of boxes on top of it "drops" onto the box below it (or onto the floor if the bottom box is clicked). The result should be a new tower of boxes with no gaps and no overlapping boxes.

If something is not working, please comment it out and write comments about why you think it's not working and how you would be going about fixing it if you had more time. Make sure that the code you submit runs, even if it doesn't implement all of the functionality.

Your work will be graded based on:

Happy programming!

What to submit

Submit all your files as attachements in an email to me with the subject "Midterm 2" followed by your name or initials. Please include your name and a task number at the beginning of each file, in a comment. In your email message please include any comments about what works and what doesn't, as well as thoughts on how you would've continued if you had more time (if you run out of time). Also please include the total time you spent on the test.


CSci 1301 course web site.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.