15-112Fall 2011 Quiz 8
15 Minutes.  No calculators, no notes, no books, no computers.
Questions about Tetris refer only to our specific implementation as outlined in lab6.

1.       [15 pts] True or False:   Circle all the TRUE statements regarding our Tetris implementation:
A)  drawFallingPiece would work the same if we swapped the order of the following two lines:
     for row in xrange(canvas.data.fallingPieceRows):
        for col in xrange(canvas.data.fallingPieceCols):
B) timerFired continues to be called even after gameOver() is called.
C) removeFullRows should not copy rows like this: board[newRow] = board[oldRow]
D) To rotate a piece, we first verify that the rotation is legal, and then actually rotate the piece.
E) moveFallingPiece can only move the piece down, right, or left. It is not possible to use it to move the piece up.

2.       [15 pts] For each of these Tetris functions, mark an X to indicate whether it is part of the Model, View, or Controller:

Function

Model

View

Controller

drawScore

 

 

 

init

 

 

 

mousePressed

 

 

 

moveFallingPiece

 

 

 

redrawAll

 

 

 

timerFired

 

 

 

 

3.       [10 pts] For each of these values in canvas.data, mark an X to indicate its data type:

Value

1d-list

2d-list-of-integers

2d-list-of-strings

2d-list-of-booleans

3d-list

canvas.data.fallingPiece

 

 

 

 

 

canvas.data.tetrisPieces

 

 

 

 

 

canvas.data.board

 

 

 

 

 

canvas.data.tetrisPieceColors

 

 

 

 

 

 

4.       [20 pts] Suppose we want to add a “C” piece to Tetris, shaped like this:
     **
     *
     **
Fill in the blank in the code below

cPiece =






tetrisPieces = [ iPiece, jPiece, lPiece, oPiece, sPiece, tPiece, zPiece, cPiece ]


 

5.       [15 pts] Sketch what the following will draw in the 400x400 canvas provided:

def f(canvas, cx, cy, r, n):
    skip = 3
    for step in xrange(n):
        theta0 = math.radians(skip*step*360/n)
        theta1 = math.radians(skip*(step+1)*360/n)
        x0 = cx + r*math.cos(theta0)
        y0 = cy - r*math.sin(theta0)
        x1 = cx + r*math.cos(theta1)
        y1 = cy - r*math.sin(theta1)
        canvas.create_line(x0,y0,x1,y1)
f(canvas, 200, 200, 200, 7)

 

 

6.       [5 pts] In the preceding problem, changing xrange(n) to xrange(2*n) does not change the drawing.  Briefly explain.

 

7.       [20 pts] Consider this code:
def f(n):
    print n,
    if (n == 0):
        return 0
    elif (n > 10):
        return 1 + f(n%10)
    else:
        return 2 + f(n/2)
print f(16)

a.       What will this code print?

b.      Clearly label the base case(s) of the function f.

c.       Clearly label the recursive case(s) of the function f.

d.      Give an integer value n such that f(n) infinitely recurses.

8.       [2.5 pts] bonus/optional: as we have learned, zip(*a) creates the transpose of the 2d list a (swapping rows and cols).  So if a is [[1,0,0],[1,1,1]], then zip(*a) is [[1,1],[0,1],[0,1]].  As it is, zip(*piece) almost rotates the given Tetris piece counter-clockwise according to our Tetris design.  With this in mind, and in just a few words, what do we need to do to zip(*piece) so that it rotates the piece counter-clockwise?

 

9.  [2.5pts] bonus/optional: What will this code print?  Show your work.
def f(g,h): return h+g(f,h/2) if (h>0) else 1
def g(h,i): return i*h(g,i/3) if (i>0) else 1
print f(f,f(g,g(f,3)))