8.14

Homework 2a🔗

home work!

Programming Language #lang htdp/bsl

MUST BE DONE WITH YOUR PARTNER

Due Date Tues at 9:00pm (Week 2)

Purpose Start working on course project, practice with design recipe

Submit all of the following in a single .rkt file to Handins. You will get immediate feedback and can resubmit.

Exercises

Everyday I’m Shuffling

(Note: read Section 5.4, Defining Structure Types before starting this problem. You may also want to scroll back to sections 5.1-5.3 for a simpler, built-in example of structures. If you and your partner cannot make headway on this problem until after Monday’s lecture, when we cover structures, start work on the images problem below.)

; A CSG (CoinShuffleGame) is a (make-csg CoinOrFalse CoinOrFalse CoinOrFalse)
(define-struct csg [left middle right])
; and represents the three cups in a coin shuffle game, and what is under them
 
; A CoinOrFalse is one of:
; - #false
; - Number
; and represents either no coin or the coin's monetary value
 
; A Guess is one of:
; - "left"
; - "middle"
; - "right

Exercise 1 Provide examples and templates for the above data definitions. Recall that when data definitions refer to other complex data types, the template must reflect this by calling the appropriate template.

Exercise 2 Design a function, shuffle-right, which moves all cup values in a CSG to the right. The right cup’s contents should loop back to the left cup.

Exercise 3 Design a function, csg-value, which given a CSG and a Guess, outputs the monetary value of the guessed cup. A cup with no coins is worth 0.

Exercise 4 Design the function inflation, which given a CSG and a number, adds that monetary value to all of the coins in the cup, and leaves empty cups as is.

Challenge: Stacking bricks

One of the challenges in designing code is figuring out how best to break down a problem into simpler pieces. On this problem, you’re going to draw two different common brick-patterns...two different ways each. In some respects this is similar to the image breakdown that you did for the country flags, but these images are very deliberately much more repetitive. The puzzle for you in this problem is to recognize and take advantage of the repetition, rather than tediously writing out a very repetititve expression as your answer.

Herringbone

   

Staggered

image

   

image

(As you can see from the picture, bricks have very nearly a 2:1 aspect ratio, such that when you include the mortar between the bricks, two short brick sides can stack next to one long brick side.)

  • Design four functions in total: herringbone-1, herringbone-2, staggered-1 and staggered-2. Each function should take four parameters, that somehow describe the size of each brick, the thickness of the mortar between the bricks, the color of the bricks, and the color of the mortar. Make sure your purpose statements clearly explain what your parameters mean! The images above choose different colors and sizes deliberately, and your code should be equally flexible. Your functions don’t have to draw precisely the same quantity of bricks as in these two sample images, or draw an arbitrarily large region of bricks, but they should draw a large enough region of bricks that the pattern is fully repeated in both directions.
    • The ...-1 versions of these functions should draw each of these images using only combinations of the functions in Overlaying Images, and the techniques covered in class so far, to lay out the brick pattern.

    • The ...-2 versions of these functions should draw each of these images using only combinations of the functions in Placing Images & Scenes, and the techniques covered in class so far, to lay out the brick pattern.

    • (You may use whichever functions you like to draw the bricks themselves.)

  • These images are clearly repetitive, and your code should not be. You will therefore likely want to design one or more helper functions for drawing smaller sub-pieces of these images. Give these functions meaningful names and clear purpose statements, to explain your work.

  • You should provide at least one check-expect example of using each of your functions. Since testing images is somewhat awkward, you may want to write examples similar to

    (check-expect (circle 15 "outline" "blue") image)

    that literally paste in examples of what you expect your output to look like. (This is not always an acceptable testing strategy! But for this problem, it will suffice.) You may not use the two images above as your only example; demonstrate a new one of your own.

  • In a comment after your functions, answer the following questions and explain your reasoning: Which version of each function was easier to figure out? If you were to lay out larger regions of these brick patterns, which version of each function would be easier to extend or reuse (still using the restrictions for the ...-1 and ...-2 versions above)? Hint: some of the ideas in the functions you designed for the placement exam (which go beyond what we’ve covered in class so far) might come in handy here!

Hints: