# File: nim.py # Good, the layout looks fine. Now, need to worry about changing the # view and executing the logic. # Plan: need an "update logic" function. # need to alter the display with the revised values. # Thought: Might need to learn object-oriented programming! from Tkinter import * master = Tk() labs = [ "Take One", "Take Two", "5", "4", "3", "2", "1", "Person\nwins", "Computer\nwins"] rows = 3 for i in range(len(labs)): Label(master, text=labs[i], font="Helvetica 24").grid(row=0,column=i) offswitch = PhotoImage(file='sw_off_gray.gif') onswitch = PhotoImage(file='sw_on_gray.gif') offlight = PhotoImage(file='lightBulbOff.gif') onlight = PhotoImage(file='lightBulbOn.gif') def clickswitch(i,j): if i == 0: takeOneSwitch[j] = (takeOneSwitch[j] == False) if takeOneSwitch[j]: switches[i][j].configure(image=onswitch) else: switches[i][j].configure(image=offswitch) else: takeTwoSwitch[j] = (takeTwoSwitch[j] == False) if takeTwoSwitch[j]: switches[i][j].configure(image=onswitch) else: switches[i][j].configure(image=offswitch) # change the display resetlogic() logic() setlights() # print ["click take one", takeOneSwitch[j]] # l1.configure(text="Uno") switches = [range(rows+1) for i in range(2)] takeOneSwitch = [False for i in range(rows+1)] takeTwoSwitch = [False for i in range(rows+1)] lights = [range(rows+1) for i in range(len(labs))] # Start widgets for j in range(1,rows+1): for i in [0,1]: def click(i=i,j=j): clickswitch(i,j) switches[i][j] = Button(master, image=offswitch, command=click) switches[i][j].grid(row=j,column=i) for i in range(2,len(labs)): lights[i][j] = Label(master, image=offlight) lights[i][j].grid(row=j,column=i) def resetlogic(): global fiveLeft global fourLeft global threeLeft global twoLeft global oneLeft global youWin global IWin fiveLeft = [False for j in range(rows+1)] fourLeft = [False for j in range(rows+1)] threeLeft = [False for j in range(rows+1)] twoLeft = [False for j in range(rows+1)] oneLeft = [False for j in range(rows+1)] youWin = [False for j in range(rows+1)] IWin = [False for j in range(rows+1)] def logic(): fiveLeft[1] = True threeLeft[2] = takeOneSwitch[1] twoLeft[2] = takeTwoSwitch[1] IWin[3] = threeLeft[2] and (takeOneSwitch[2] or takeTwoSwitch[2]) or (twoLeft[2] and takeOneSwitch[2]) youWin[3] = twoLeft[2] and takeTwoSwitch[2] def setlights(): # boom, boom---out go the lights for j in range(1,rows+1): for i in range(2,len(labs)): lights[i][j].configure(image=offlight) # switch on the appropriate ones for j in range(rows+1): if fiveLeft[j]: lights[2][j].configure(image=onlight) if fourLeft[j]: lights[3][j].configure(image=onlight) if threeLeft[j]: lights[4][j].configure(image=onlight) if twoLeft[j]: lights[5][j].configure(image=onlight) if oneLeft[j]: lights[6][j].configure(image=onlight) if youWin[j]: lights[7][j].configure(image=onlight) if IWin[j]: lights[8][j].configure(image=onlight) # start with just the ten light on # lights[1][2].configure(image=onlight) resetlogic() logic() setlights() master.mainloop()