8.14

Homework 3b🔗

home work!

Programming Language BSL

Due Date Thu at 9:00pm (Week 3)

Purpose Practice with self-referential data

(Reminder: when designing functions, keep the template of your data definition and the signature of your function in mind. You may need to design helper data definitions or helper functions, as well, in order to implement your original intended function.)

Exercises

Jenga!

In the game of Jenga, play starts with a tower made of levels of three blocks in alternating directions, and players take turns trying to remove a block from a lower level and placing it on the top of the tower. Whoever knocks the tower over loses.

We can represent a Jenga tower with the following data defintions:

; A JDir is one of
; - "leftright"
; - "updown"
; INTERPRETATION: Represents the long direction the bricks are laying in
;   a level of a Jenga tower, when looking down at the tower from above.
; A Jenga is one of:
; - "top"
; - (make-level JDir Boolean Boolean Boolean Jenga)
(define-struct level [dir side1 mid side2 above])
; INTERPRETATION: Represents either an empty Jenga tower, or a level of a
;   Jenga tower with a block direction, the presence (#true) or absence
;   (#false) of the three blocks in that level (side1 is either the leftmost
;   or the uppermost block, depending on the direction), and the rest of the
;   tower above it.

Exercise 1 Design a function num-levels which counts the total number of levels in a Jenga tower.

Exercise 2 Design a function num-blocks which counts the total number of blocks in a Jenga tower.

Exercise 3 Design a function alternating? determines whether a Jenga tower correctly alternates the direction of each level or not.

Exercise 4 Design a function top-level that returns either the topmost level of the tower or #f if no such level exists.

Exercise 5 Our data definition technically allows for some values that would represent bogus towers in the real world. Design a function hovering? that determines if any level in the tower contains no blocks at all.

Exercise 6 Challenge! (Not required, but good practice) Design a function unbalanced? which determines if the tower will fall: if any level in the tower (except the topmost one) contains only a single block on either side and no others.