def makeModel(data):
    data["color"] = "purplE"
    data["tmp"] = ""
    data["x"] = 200
    data["y"] = 200

def makeView(data, canvas):
    # Write your simulation view here, using the Tkinter canvas
    canvas.create_oval(data["x"]-50, data["y"]-50, data["x"]+50, data["y"]+50,
                       fill=data["color"])

def runRules(data, call):
    # Write your simulation rules here, by changing data
    pass

def keyPressed(data, event):
    print("Char:", event.char, "Keysym:", event.keysym)
    if event.keysym == "Return":
        data["color"] = data["tmp"]
        data["tmp"] = ""
    else:
        data["tmp"] = data["tmp"] + event.char

def mousePressed(data, event):
    #data["x"] = event.x
    #data["y"] = event.y
    
    import random
    colors = [ "red", "orange", "yellow", "green", "blue", "purple" ]
    
    # sqrt((x1-x2)**2 + (y1-y2)**2)
    if ((event.x - 200)**2 + (event.y - 200)**2)**0.5 <= 50:
        data["color"] = random.choice(colors)

# You do not need to be able to write the following functions;
# just modify the five functions above.

from tkinter import *

def timeLoop(data, canvas, call):
    runRules(data, call)

    canvas.delete(ALL)
    makeView(data, canvas)
    canvas.update()

    canvas.after(data["timeRate"], timeLoop, data, canvas, call + 1)

def keyEventHandler(data, canvas, event):
    keyPressed(data, event)
    
    canvas.delete(ALL)
    makeView(data, canvas)
    canvas.update()
    
def mouseEventHandler(data, canvas, event):
    mousePressed(data, event)
    
    canvas.delete(ALL)
    makeView(data, canvas)
    canvas.update()
    
def runSimulation(w, h, timeRate):
    data = { }
    data["timeRate"] = int(timeRate * 1000) # call will be in ms
    makeModel(data)
    
    root = Tk()
    canvas = Canvas(root, width=w, height=h)
    canvas.configure(bd=0, highlightthickness=0)
    canvas.pack()
    makeView(data, canvas)
    
    canvas.after(data["timeRate"], timeLoop, data, canvas, 1)
    
    root.bind("<Key>", lambda event : keyEventHandler(data, canvas, event))
    root.bind("<Button-1>", lambda event : mouseEventHandler(data, canvas, event))
    
    root.mainloop()

runSimulation(400, 400, 0.1)

###
    
# one iteration of the Random Distance Race
def runTrial():
    import random
    return random.randint(1, 6) + random.randint(1, 6)

def getExpectedValue(numTrials):
    total = 0
    for trial in range(numTrials):
        #if trial % 100000 == 0 and trial != 0:
        #    print(total / trial)
        numLaps = runTrial()
        total = total + numLaps
    return total / numTrials

###

def getExpectedValue2(numTrials):
    total = 0
    for trial in range(numTrials):
        numLaps = runTrial()
        if numLaps >= 10:
            total = total + 1
    return total / numTrials