# 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", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1", "You win", "I win"] 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(6) for i in range(6)] takeOneSwitch = [False for i in range(6)] takeTwoSwitch = [False for i in range(6)] lights = [range(6) for i in range(13+2)] # tenLeftLab = range(6) # nineLeftLab = range(6) # eightLeftLab = range(6) # sevenLeftLab = range(6) # sixLeftLab = range(6) # fiveLeftLab = range(6) # fourLeftLab = range(6) # threeLeftLab = range(6) # twoLeftLab = range(6) # oneLeftLab = range(6) # youWinLab = range(6) # IWinLab = range(6) # Start widgets for j in range(1,6): 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 tenLeft global nineLeft global eightLeft global sevenLeft global sixLeft global fiveLeft global fourLeft global threeLeft global twoLeft global oneLeft global youWin global IWin tenLeft = [False for j in range(6)] nineLeft = [False for j in range(6)] eightLeft = [False for j in range(6)] sevenLeft = [False for j in range(6)] sixLeft = [False for j in range(6)] fiveLeft = [False for j in range(6)] fourLeft = [False for j in range(6)] threeLeft = [False for j in range(6)] twoLeft = [False for j in range(6)] oneLeft = [False for j in range(6)] youWin = [False for j in range(6)] IWin = [False for j in range(6)] def logic(): tenLeft[1] = True eightLeft[2] = takeOneSwitch[1] sixLeft[2] = takeTwoSwitch[1] sixLeft[3] = eightLeft[2] and takeOneSwitch[2] fourLeft[3] = eightLeft[2] and takeTwoSwitch[2] moveTwo = takeOneSwitch[2] or takeTwoSwitch[2] threeLeft[3] = sixLeft[2] and moveTwo moveThree = takeOneSwitch[3] or takeTwoSwitch[3] threeLeft[4] = sixLeft[3] and moveThree twoLeft[4] = fourLeft[3] and takeOneSwitch[3] winFourA = threeLeft[3] and moveThree winFourB = fourLeft[3] and takeTwoSwitch[3] IWin[4] = winFourA or winFourB moveFour = takeOneSwitch[4] or takeTwoSwitch[4] winFiveA = twoLeft[4] and takeOneSwitch[4] winFiveB = threeLeft[4] and moveFour IWin[5] = winFiveA or winFiveB youWin[5] = twoLeft[4] and takeTwoSwitch[4] def setlights(): # boom, boom---out go the lights for j in range(1,6): for i in range(2,len(labs)): lights[i][j].configure(image=offlight) # switch on the appropriate ones for j in range(6): if tenLeft[j]: lights[2][j].configure(image=onlight) if nineLeft[j]: lights[3][j].configure(image=onlight) if eightLeft[j]: lights[4][j].configure(image=onlight) if sevenLeft[j]: lights[5][j].configure(image=onlight) if sixLeft[j]: lights[6][j].configure(image=onlight) if fiveLeft[j]: lights[7][j].configure(image=onlight) if fourLeft[j]: lights[8][j].configure(image=onlight) if threeLeft[j]: lights[9][j].configure(image=onlight) if twoLeft[j]: lights[10][j].configure(image=onlight) if oneLeft[j]: lights[11][j].configure(image=onlight) if youWin[j]: lights[12][j].configure(image=onlight) if IWin[j]: lights[13][j].configure(image=onlight) # start with just the ten light on # lights[1][2].configure(image=onlight) resetlogic() logic() setlights() master.mainloop()