I need to create a Nim game in which the person plays against the computer. In o
ID: 3694464 • Letter: I
Question
I need to create a Nim game in which the person plays against the computer. In order to start a new game, I created a button that says "new game" and an entry widget that the user can type in the number of balls they want to play with in their next game. the function initializeNewGame should read the number entered in the entry widget and then start a new game with that number of balls. also did i add the entry widget in the GUI correctly? here is part of the code:
def createGUI():
global rootWindow
global canvas
global statusLabel
global textEntry
rootWindow = Tk()
canvasAndButtons = Frame(rootWindow)
canvas = Canvas(canvasAndButtons, height=canvasHeight, width=canvasWidth, relief=SUNKEN, borderwidth=2)
canvas.pack(side=LEFT)
buttonframe = Frame(canvasAndButtons)
button1 = Button(buttonframe, text='Take 1', command=lambda:takeBalls(1))
button2 = Button(buttonframe, text='Take 2', command=lambda:takeBalls(2))
button3 = Button(buttonframe, text='Take 3', command=lambda:takeBalls(3))
button4 = Button(buttonframe, text='New Game', command=initializeNewGame)
# Entry widget
e = Entry(buttonframe)
button1.pack()
button2.pack()
button3.pack()
button4.pack()
e.pack()
buttonframe.pack(side=RIGHT)
canvasAndButtons.pack()
statusLabel = Label(rootWindow, text="Play Nim")
statusLabel.pack()
def initializeNewGame():
Explanation / Answer
import Tk
class createGUI():
def __init__(self,master):
#global rootWindow
#global canvas
#global statusLabel
#global textEntry
self.master = master;
#rootWindow = Tk()
master.title("A simple GUI")
canvasAndButtons = Frame(rootWindow)
self.canvas = Canvas(canvasAndButtons, height=canvasHeight, width=canvasWidth, relief=SUNKEN, borderwidth=2)
self.canvas.pack(side=LEFT)
self.buttonframe = Frame(canvasAndButtons)
self.button1 = Button(buttonframe, text='Take 1', command=lambda:takeBalls(1))
self.button2 = Button(buttonframe, text='Take 2', command=lambda:takeBalls(2))
self.button3 = Button(buttonframe, text='Take 3', command=lambda:takeBalls(3))
self.button4 = Button(buttonframe, text='New Game', command=initializeNewGame)
# Entry widget
e = Entry(buttonframe)
self.button1.pack()
self.button2.pack()
self.button3.pack()
self.button4.pack()
e.pack()
buttonframe.pack(side=RIGHT)
canvasAndButtons.pack()
self.statusLabel = Label(rootWindow, text="Play Nim")
self.statusLabel.pack();
def initializeNewGame():
numberOfBalls = 5 # change this to read from Entry widget
initializeNimAndGUI(numberOfBalls)
print('---------',numberOfBalls);
root = Tk();
cobj = createGUI(root);
root.Mainloop();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.