import tkinter as tk import re global bx global cp bx = [] cp = 1 global inputmove global inputboard global board global history global player global error def render(b): rx = ["X"*a for a in b] r = '\n'.join(rx) return r def playeris(n): return "player "+str(n)+"'s move" def checkwin(): global bx if False not in [(a==1 or a==2) for a in bx]: return True return False def play(): global bx global cp global history rm = inputmove.get() if not re.match("[0-9]+[-][0-9]+[,][0-9]+",rm): error.set("invalid move") return if not re.match("[0-9]+[-][0-9]+[,][0-9]+",rm).group(0)==rm: error.set("invalid move") return rf = rm.split("-") rf = [a.split(",") for a in rf] rf = sum(rf,[]) rf = [int(a) for a in rf] if rf[0] not in bx: error.set("invalid move") return if rf[0]!=rf[1]+rf[2]: error.set("invalid move") return if rf[1]==rf[2]: error.set("invalid move") return if rf[1]==0 or rf[2]==0: error.set("invalid move") return error.set("") hn = history.get() hn = hn + "\n" + rm history.set(hn) i = bx.index(rf[0]) del(bx[i]) bx.insert(i,rf[2]) bx.insert(i,rf[1]) board.set(render(bx)) if checkwin(): player.set("player "+str(cp)+" wins!") return cp = 3-cp player.set(playeris(cp)) def clear(): global bx global cp bx = [] cp = 1 history.set("") board.set("") player.set("") error.set("") def set(): global bx global cp error.set("") rb = inputboard.get() if not re.match("([0-9]+[,])*([0-9])+",rb): error.set("invalid board") return if not re.match("([0-9]+[,])*([0-9])+",rb).group(0)==rb: error.set("invalid board") return history.set("start board: "+rb) rb = rb.split(",") rb = [int(a) for a in rb] bx = rb cp = 1 board.set(render(bx)) player.set(playeris(cp)) game = tk.Tk() board = tk.StringVar() history = tk.StringVar() player = tk.StringVar() error = tk.StringVar() history.set("") board.set("") player.set("") error.set("") #frame on the right, for info to play the game playframe = tk.Frame(game) playframe.pack(side=tk.RIGHT) #subframes boardframe = tk.Frame(playframe) boardframe.pack() inputframe1 = tk.Frame(playframe) inputframe1.pack() inputframe2 = tk.Frame(playframe) inputframe2.pack() #contents boarddisplay = tk.Label(boardframe,textvariable=board) boarddisplay.pack() playbutton = tk.Button(inputframe1,text="play",height=2,command=play) playbutton.pack(fill='x',side=tk.BOTTOM) inputinstructions = tk.Label(inputframe1,text="Enter your move in the format X-Y,Z\nwhere X is the population of the aquarium you want to target,\nand Y and Z are the populations of your new aquariums.\nFor example: 7-4,3") inputinstructions.pack(fill='x',side=tk.TOP) inputmove = tk.Entry(inputframe1) inputmove.pack(fill='x') boardbutton = tk.Button(inputframe2,text="start new game",height=2,command=set) boardbutton.pack(fill='x',side=tk.BOTTOM) boardinstructions = tk.Label(inputframe2,text="Enter the starting board as numbers with commas.\nThe numbers will be the sizes of the piles.\nFor example: 7,5") boardinstructions.pack(fill='x',side=tk.TOP) inputboard = tk.Entry(inputframe2) inputboard.pack(fill='x') #frame on the left, to keep track of useful game details recordframe = tk.Frame(game) recordframe.pack(side=tk.LEFT) #subframes playerframe = tk.Frame(recordframe) playerframe.pack() historyframe = tk.Frame(recordframe) historyframe.pack() optionsframe = tk.Frame(recordframe) optionsframe.pack() errorframe = tk.Frame(recordframe) errorframe.pack() #contents playerdisplay = tk.Label(playerframe,textvariable=player) playerdisplay.pack() historydisplay = tk.Label(historyframe,textvariable=history) historydisplay.pack() quitbutton = tk.Button(optionsframe,text="quit",width=7,height=2,command=quit) quitbutton.pack(side=tk.LEFT) clearbutton = tk.Button(optionsframe,text="clear",width=7,height=2,command=clear) clearbutton.pack(side=tk.RIGHT) errordisplay = tk.Label(errorframe,textvariable=error) errordisplay.pack() game.mainloop()