Need help with Intro to Comp Sci 2 (Python) problem: This is what is provided: (
ID: 3788954 • Letter: N
Question
Need help with Intro to Comp Sci 2 (Python) problem:
This is what is provided:
(Copy/Paste version):
from tkinter import Tk, Label, Entry, Button
from random import *
class Craps(Tk):
#set up the main window
def __init__(self, parent=None):
Tk.__init__(self, parent)
self.title('Play Craps')
self.new_game()
self.make_widgets()
#when a new game is started, the firstRoll will start at 0
def new_game(self):
self.firstRoll = 0
#create and place the widgets in the window
def make_widgets(self):
Label(self, text="Die 1").grid(row=0, column=0, columnspan=1)
Label(self, text="Die 2").grid(row=0, column=1, columnspan=1)
self.die1Ent = Entry(self) #entry field for die 1
self.die2Ent = Entry(self) #entry field for die 2
self.die1Ent.grid(row=1, column=0, columnspan=1)
self.die2Ent.grid(row=1, column=1, columnspan=1)
Label(self, text="First roll").grid(row=2, column=0, columnspan=1)
Label(self, text="Result").grid(row=2, column=1, columnspan=1)
self.firstRollEnt = Entry(self) #entry field for the first roll
self.resultEnt = Entry(self) #entry field the result of the current roll
self.firstRollEnt.grid(row=3, column=0, columnspan=1)
self.resultEnt.grid(row=3, column=1, columnspan=1)
Button(self, text="Roll the dice!", command=self.play).grid(row=4, column=0)
#complete the implementation of play()
def play(self):
#replace pass with your solution
Craps().mainloop()
-------------------------------------------------------------------------------------------------------------------
(Screenshot version):
Write a GUI class called Craps that implements the die game craps. This question asks you to write an event handler that deals with a single round of the game. To understand how to write the handler, you need to remember how the game works. It begins with the player throwing a pair of standard, six-sided dice. If the player rolls a 7 or 11 in the first round, the player wins. If the player rolls a 2, 3, or 12 in the first round, the player loses. For any other roll the player must roll again to determine whether he/she won. During subsequent rounds, the player wins if he/she rolls point, that is, rolls the same total as he/she did during the first round. During subsequent rounds, the player loses if he/she rolls a 7 In the template file find has all the methods completed except for the event handler play. The following is a description of the methods that are already written. None of these methods should be modified as you complete this question: a. init The constructor creates the main window, sets the title, calls the new game() method, and calls the make widgets0 method. b. new game: This method resets the first roll variable in the object. When the first roll is set to 0 it means that the player has just begun a new game. Once the playerExplanation / Answer
class Craps(Tk): 'a GUI that plays craps' def __init__(self, parent=None): 'constructor' Tk.__init__(self, parent) self.title("Play craps") self.new_game() self.make_widgets() def new_game(self): 'reset the game' self.firstroll = 0 def make_widgets(self): 'define the Craps widgets' Label(self, text="Die 1").grid(row=0, column=0) Label(self, text="Die 2").grid(row = 0, column = 1) self.ent1 = Entry(self, justify = CENTER) self.ent1.grid(row = 1, column = 0) self.ent2 = Entry(self, justify = CENTER) self.ent2.grid(row = 1, column = 1) Label(self, text = "First roll").grid(row = 2, column = 0) self.first = Entry(self, justify = CENTER) self.first.grid(row = 3, column = 0) Label(self, text = "Result").grid(row = 2, column = 1) self.result = Entry(self, justify = CENTER) self.result.grid(row = 3, column = 1) Button(self, text="Roll the dice!", command = self.play).grid(row = 4, column = 0, columnspan = 2) def play(self): 'the event handler' self.result.delete(0,END) roll1, roll2 = randrange(1,7), randrange(1,7) self.ent1.delete(0,END) self.ent2.delete(0,END) self.ent1.insert(END, roll1) self.ent2.insert(END, roll2) if self.firstroll == 0: self.first.delete(0,END) self.firstroll = roll1 + roll2 self.first.insert(END, self.firstroll) if self.firstroll == 7: self.result.insert(END, 'You won! Play Again?') self.new_game() elif self.firstroll == 2 or self.firstroll == 3 or self.firstroll == 12: self.result.insert(END, 'You lost. Play Again?') self.new_game() else: self.result.insert(END, 'Roll Again') else: total = roll1 + roll2 if total == 7: self.result.insert(END, 'You lost! Play Again?') self.new_game() elif total == self.firstroll: self.result.insert(END, 'You won! Play Again?') self.new_game() else: self.result.insert(END, 'Roll Again')
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.