#################################################
# hw7.py
#
# Your name:
# Your andrew id:
# This assignment is SOLO!
#################################################

import math, copy, random

from cmu_graphics import *

#################################################
# Helper functions
#################################################

def almostEqual(d1, d2, epsilon=10**-7):
    # note: use math.isclose() outside 15-112 with Python version 3.5 or later
    return (abs(d2 - d1) < epsilon)

import decimal
def roundHalfUp(d):
    # Round to nearest with ties going away from zero.
    rounding = decimal.ROUND_HALF_UP
    # See other rounding options here:
    # https://docs.python.org/3/library/decimal.html#rounding-modes
    return int(decimal.Decimal(d).to_integral_value(rounding=rounding))

#################################################
# Tetris
#################################################
def gameDimensions():
    # These values are set to the writeup defaults
    rows = 15
    cols = 10
    cellSize = 20
    margin = 25
    return (rows, cols, cellSize, margin)

def onAppStart(app):
    # Delete this starter code and replace with your own
    app.label = 'Tetris!'
    app.color = 'orange'
    app.size = 1

def onKeyPress(app, event):
    # Delete this starter code and replace with your own
    app.color = random.choice(['red', 'orange', 'yellow', 'green', 'blue'])

def onStep(app):
    # Delete this starter code and replace with your own
    app.size += 10

def redrawAll(app):
    # Delete this starter code and replace with your own
    for s in ((app.size % 300), ((app.size + 150) % 300)):
        drawLabel(app.label, app.width/2, app.height/2, \
            fill=app.color, font='Arial', \
            size=s, bold=True)

def playTetris():
    # Note: Instead of hardcoding width and height, you should call
    # gameDimensions() and calculate the correct width and height values
    # from rows, cols, cellSize, and margin
    width = 400
    height = 400
    runApp(width=width, height=height)

#################################################
# Test Functions
#################################################

# None!  Though... you may wish to (optionally) add some test
#        functions of your own for any functions that do not
#        involve graphics


#################################################
# main
#################################################

def main():
    playTetris()

if __name__ == '__main__':
    main()
