Need help completeing. Write a program to play “Three Button Monte.” Your progra
ID: 3697243 • Letter: N
Question
Need help completeing. Write a program to play “Three Button Monte.” Your program should draw three buttons labeled “Door 1”, “Door 2”, and “Door 3” in a window and randomly select one of the buttons (without telling the user which one is selected.) The program prompts the user to click on one of the buttons. A click on the special button is a win, and a click on one of the other two is a loss. You should tell the user whether they won or lost, and in the case of a loss, which was the correct button. Your program should be entirely graphical; that is, all prompts and messages should be displayed in the graphics window. 1. Add comments at the top 2. Import randrange, graphics and button from random import randrange from graphics import * from button import Button 3. Define main a. Create a window with 350 and 100 coordinates titled “Three Button Monte” b. Set coordinates to .5, 0 and 3.5, 3 c. Create three buttons b1 = Button(win, Point(1,2), .75, 1, "Door 1") b1.activate() b2 = Button(win, Point(2,2), .75, 1, "Door 2") b2.activate() b3 = Button(win, Point(3,2), .75, 1, "Door 3") b3.activate() d. Create a Text message telling the user to Guess a door mess = Text(Point(2,.75), "Guess a door.") mess.setStyle("bold") mess.draw(win) e. Generate a random number secret = randrange(1,4) f. Initialize choice to None g. Create a while loop to determine when the user is done while choice == None: h. Within the while loop: i. get the users pt that was clicked pt = win.getMouse() ii. Add a for loop that loops through each button for button in [b1, b2, b3]: iii. Within the for loop: 1. Check to see if the button was clicked if button.clicked(pt): 2. If it was clicked, set choice to button i. Covert the value of getLabel for the choice to an integer choiceNum = int(choice.getLabel()[-1]) j. Check to see if the secret number and the choiceNum are the same. If it is, display a message that the user won. If they are not the saem, a message should display telling them that they lost and what the correct door was. if choiceNum == secret: mess.setText("You win!") else: mess.setText("You lose. The answer was door {0}.".format(secret)) k. Have the user click the mouse to close and then close the program 4. Call the main function.
Explanation / Answer
from random import randrange
from graphics import *
from DieAndButton import Button
def getDoorPick(win, b1, b2, b3):
# waits for a click in b1, b2, or b3
# returns the number of the door clicked
choice = None
while choice == None:
pt = win.getMouse()
for button in [b1, b2, b3]:
if button.clicked(pt):
choice = button
choiceNum = int(choice.getLabel()[-1])
return choiceNum
def updateScore(choice, msg, score, hits, misses):
# Compares choice to a random number
# Displays win or loss in msg
# Updates hits or misses and display of score
# Returns updated hits and misses
secret = randrange(1,4)
tallyStr = "Wins: {0:2} Losses: {1:2}"
if choice == secret:
msg.setText("You win!")
hits = hits + 1
else:
msg.setText("You lose. The answer was door {0}.".format(secret))
misses = misses + 1
score.setText(tallyStr.format(hits,misses))
return hits, misses
def quitOrPlay(win, quit, again):
# Get a click in quit or again
# Returns True if again clicked, false for quit
quit.activate()
again.activate()
pt = win.getMouse()
while not (quit.clicked(pt) or again.clicked(pt)):
pt = win.getMouse()
ans = again.clicked(pt)
quit.deactivate()
again.deactivate()
return ans
def main():
# Set up interface
win = GraphWin("Extended Three Button Monte", 350, 350)
win.setCoords(.5,-2, 3.5, 3)
b1 = Button(win, Point(1,2), .75, 1, "Door 1")
b1.activate()
b2 = Button(win, Point(2,2), .75, 1, "Door 2")
b2.activate()
b3 = Button(win, Point(3,2), .75, 1, "Door 3")
b3.activate()
again = Button(win, Point(1.25,0), 1, .75, "Play Again")
quit = Button(win, Point(2.75,0), 1, .75, "Quit")
mess = Text(Point(2,.75), "Guess a door")
mess.setStyle("bold")
mess.draw(win)
scoreBox = Text(Point(2,-1), "")
scoreBox.draw(win)
playAgain = True
hits = 0
misses = 0
while playAgain:
mess.setText("Guess a door")
pick = getDoorPick(win, b1, b2, b3)
hits, misses = updateScore(pick, mess, scoreBox, hits, misses)
playAgain = quitOrPlay(win, quit, again)
win.close()
if __name__ == '__main__':
main()
DieAndButton.py
from random import randrange
from graphics import *
from CButton import *
class Button:
def __init__(self, win, center, width, height, label):
w, h = width/2.0, height/2.0
x, y = center.getX(), center.getY()
self.xmax, self.xmin = x+w, x-w
self.ymax, self.ymin = y+h, y-h
p1 = Point(self.xmin, self.ymin)
p2 = Point(self.xmax, self.ymax)
self.rect = Rectangle(p1, p2)
self.rect.setFill('lightgray')
self.rect.draw(win)
self.label = Text(center, label)
self.label.setTextColor('black')
self.label.draw(win)
self.deactivate()
def clicked(self, p):
return (self.active and
self.xmin <= p.getX() <= self.xmax and
self.ymin <= p.getY() <= self.ymax)
def getLabel(self):
return self.label.getText()
def activate(self):
self.label.setFill('black')
self.rect.setWidth(2)
self.active = True
def deactivate(self):
self.label.setFill('lightgrey')
self.rect.setWidth(1)
self.active = False
class Dieview:
def __init__(self, win, center, size):
self.win = win
self.background = "white"
self.foreground = "black"
self.psize = 0.1 * size
hsize = size / 2.0
offset = 0.6 * hsize
self.value = 1
cx, cy = center.getX(), center.getY()
p1 = Point(cx-hsize, cy-hsize)
p2 = Point(cx+hsize, cy+hsize)
rect = Rectangle(p1, p2)
rect.draw(win)
rect.setFill(self.background)
self.pip1 = self.__makePip(cx-offset, cy-offset)
self.pip2 = self.__makePip(cx-offset, cy)
self.pip3 = self.__makePip(cx-offset, cy+offset)
self.pip4 = self.__makePip(cx, cy)
self.pip5 = self.__makePip(cx+offset, cy-offset)
self.pip6 = self.__makePip(cx+offset, cy)
self.pip7 = self.__makePip(cx+offset, cy+offset)
self.setValue(self.value)
def __makePip(self, x, y):
pip = Circle(Point(x,y), self.psize)
pip.setFill(self.background)
pip.setOutline(self.background)
pip.draw(self.win)
return pip
def setValue(self, value):
self.value = value
self.pip1.setFill(self.background)
self.pip2.setFill(self.background)
self.pip3.setFill(self.background)
self.pip4.setFill(self.background)
self.pip5.setFill(self.background)
self.pip6.setFill(self.background)
self.pip7.setFill(self.background)
if value == 1:
self.pip4.setFill(self.foreground)
elif value == 2:
self.pip1.setFill(self.foreground)
self.pip7.setFill(self.foreground)
elif value == 3:
self.pip1.setFill(self.foreground)
self.pip7.setFill(self.foreground)
self.pip4.setFill(self.foreground)
elif value == 4:
self.pip1.setFill(self.foreground)
self.pip3.setFill(self.foreground)
self.pip5.setFill(self.foreground)
self.pip7.setFill(self.foreground)
elif value == 5:
self.pip1.setFill(self.foreground)
self.pip3.setFill(self.foreground)
self.pip4.setFill(self.foreground)
self.pip5.setFill(self.foreground)
self.pip7.setFill(self.foreground)
elif value == 6:
self.pip1.setFill(self.foreground)
self.pip2.setFill(self.foreground)
self.pip3.setFill(self.foreground)
self.pip5.setFill(self.foreground)
self.pip6.setFill(self.foreground)
self.pip7.setFill(self.foreground)
def setColor(self, color):
self.foreground = color
self.setValue(self.value)
def main():
win = GraphWin("Dice Roller")
win.setCoords(0,0,10,10)
win.setBackground("green2")
die1 = Dieview(win, Point(3,7.5), 2)
die2 = Dieview(win, Point(7,7.5), 2)
rollButton = CButton(win, Point(5,4.5), 2, "Roll Dice")
rollButton.activate()
quitButton = CButton(win, Point(5,1), 1, "Quit")
pt = win.getMouse()
while not quitButton.clicked(pt):
if rollButton.clicked(pt):
color = randrange(1, 256), randrange(1, 256), randrange(1, 256)
value1 = randrange(1, 7)
die1.setValue(value1)
value2 = randrange(1,7)
die2.setValue(value2)
quitButton.activate()
die1.setColor(color)
die2.setColor(color)
pt = win.getMouse()
win.close()
if __name__ == '__main__':
main()
CButton.py
from graphics import *
class CButton:
def __init__(self, win, center, radius, label):
self.cx = center.getX()
self.cy = center.getY()
self.circ = Circle(center, radius)
self.circ.draw(win)
self.circ.setFill('gray')
self.rsquare = radius * radius
self.label = Text(center, label)
self.label.draw(win)
self.deactivate()
def clicked(self, p):
dx = p.getX() - self.cx
dy = p.getY() - self.cy
return self.active and dx*dx + dy*dy <= self.rsquare
def getLabel(self):
return self.label.getText()
def activate(self):
self.label.setFill('black')
self.circ.setWidth(2)
self.active = True
def deactivate(self):
self.label.setFill('lightgrey')
self.circ.setWidth(1)
self.active = False
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.