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 playerwins(n): return "player "+str(n)+" wins!" def play(): global bx global cp global history rm = inputmove.get() if not re.match("[0-9]+[-][0-9]+",rm): error.set("invalid move") return if not re.match("[0-9]+[-][0-9]+",rm).group(0)==rm: error.set("invalid move") return rf = rm.split("-") rf = [int(a) for a in rf] if rf[0] not in bx: error.set("invalid move") return if rf[1]>=rf[0]: error.set("invalid move") return error.set("") hn = history.get() hn = hn + "\n" + rm history.set(hn) i = bx.index(rf[0]) bx[i] = rf[1] board.set(render(bx)) if False not in [a==0 for a in bx]: player.set(playerwins(cp)) 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,height=8) 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\nwhere X is the size of the pile you want to change,\nand Y is the size you want to change that pile into.\nFor example: 3-0") 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: 5,4,3") 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()