import random

def makeModel(data):
    data["color"] = "red"
    data["tmp"] = ""
    data["cx"] = 400
    data["cy"] = 400

def makeView(data, canvas):
    canvas.create_oval(data["cx"] - 100, data["cy"] - 100,
                       data["cx"] + 100, data["cy"] + 100, fill=data["color"])

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

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

def mousePressed(data, event):
    colors = ["red", "orange", "yellow", 
              "green", "blue", "purple",
              "lightblue", "lightgreen", "pink",
              "gray", "magenta", "indigo"]
    dist = ((data["cx"] - event.x)**2 + (data["cy"] - event.y)**2)**0.5
    if dist <= 100:
        import random
        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(800, 800, 0.1)


###


# Return a distance for the random distance run
def runTrial():
    return random.randint(1, 6) + random.randint(1, 6)

def getExpectedValue(numTrials):
    longRaces = 0
    for trial in range(numTrials):
        if runTrial() >= 10:
            longRaces += 1
    return longRaces / numTrials
print("Proportion of runs that are 10 or more laps:", getExpectedValue(10000))