1. Write a python program to play “Three Button Monte”. Your program should draw
ID: 3766846 • Letter: 1
Question
1. Write a python 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 then 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. Extend the program by allowing the player to play multiple rounds and displaying the number of wins and losses. Add a “Quit” button for ending the game.
2. Extend the game for a player to play multiple rounds, displaying the number of wins and losses. Add a "Quit" button for ending the game.
Note: Your program should be entirely graphical; that is, all prompts and messages should be displayed in the graphics window.
Explanation / Answer
from random import randrange
from graphics import *
class Door:
'''object for door.'''
def __init__(self,win,center,width,height,label):
self.secret = 0
self.hasClick = 0
x,y = center.getX(),center.getY()
self.xmin, self.xmax = x-width/2,x+width/2
self.ymin, self.ymax = y-height/2,y+height/2
self.rec = Rectangle(Point(self.xmin,self.ymax),Point(self.xmax,self.ymin))
self.rec.setFill(color_rgb(255,247,133))
self.rec.setWidth(0)
self.rec.draw(win)
self.knob2 = Circle(Point(x+(width/3),y-(height/100)),width/12)
self.knob2.setFill(color_rgb(231,205,20))
self.knob2.setWidth(0)
self.knob2.draw(win)
self.knob1 = Circle(Point(x+(width/3.15),y),width/12)
self.knob1.setFill(color_rgb(255,229,40))
self.knob1.setWidth(0)
self.knob1.draw(win)
self.label2 = Rectangle(Point(x-(width/3),y+(height/2.5)),Point(x+(width/3),y++(height/3.8)))
self.label2.setFill(color_rgb(231,205,20))
self.label2.setWidth(0)
self.label2.draw(win)
self.label1= Rectangle(Point(x-(width/2.8),y+(height/2.35)),Point(x+(width/3.2),y++(height/3.6)))
self.label1.setFill(color_rgb(255,229,40))
self.label1.setWidth(0)
self.label1.draw(win)
self.text = Text(Point(x,y+(height/2.8)),label)
self.text.setSize(10)
self.text.draw(win)
def setSecret(self):
'''Set this door to a secret door.'''
self.secret = 1
print()
def turnGreen(self):
'''Turn door color to green to present this is a correct door.'''
self.rec.setFill(color_rgb(51,229,70))
self.knob2.setFill(color_rgb(51,176,0))
self.knob1.setFill(color_rgb(62,200,0))
self.label2.setFill(color_rgb(51,176,0))
self.label1.setFill(color_rgb(62,200,0))
def turnRed(self):
'''Turn door to red if user click on wrong door.'''
self.rec.setFill(color_rgb(234,53,65))
self.knob2.setFill(color_rgb(142,33,41))
self.knob1.setFill(color_rgb(240,49,61))
self.label2.setFill(color_rgb(142,33,41))
self.label1.setFill(color_rgb(240,49,61))
def clicked(self,p):
'''Remember if door has click by user
and also return Bool data'''
if self.xmin <= p.getX() <= self.xmax and self.ymin <= p.getY() <= self.ymax:
self.hasClick = 1
return (self.xmin <= p.getX() <= self.xmax and
self.ymin <= p.getY() <= self.ymax)
def reveal(self):
'''Key door feature. It will check self.secret and self.hasClcik
and respond accordingly to both action.'''
if self.secret == 1 and self.hasClick == 1:
self.turnGreen()
return 'win'
elif self.secret == 0 and self.hasClick == 1:
self.turnRed()
elif self.secret ==1 and self.hasClick ==0:
self.turnGreen()
return 'secret'
def main():
win,msg,d1,d2,d3 = drawGUI()
secretDoor(d1,d2,d3)
getInput(win,d1,d2,d3)
result(msg,d1,d2,d3)
exit(win)
def drawGUI():
win = GraphWin('Three Button Monte',390,300)
win.setCoords(0,0,130,100)
win.setBackground('white')
d1 = Door(win,Point(25,60),30,53,'Door 1')
d2 = Door(win,Point(65,60),30,53,'Door 2')
d3 = Door(win,Point(105,60),30,53,'Door 3')
title = Text(Point(65,95),'Three Button Monte')
title.setSize(17)
title.draw(win)
msgBox = Rectangle(Point(10,25),Point(120,9))
msgBox.setFill('yellow3')
msgBox.setWidth(0)
msgBox.draw(win)
msg = Text(Point(65,17),'Guess which one is a correct exit door?')
msg.setSize(13)
msg.draw(win)
return win,msg,d1,d2,d3
def secretDoor(d1,d2,d3):
i = randrange(1,4)
if i == 1: d1.setSecret()
elif i == 2 : d2.setSecret()
else: d3.setSecret()
def getInput(win,d1,d2,d3):
p = win.getMouse()
while not d1.clicked(p) and not d2.clicked(p) and not d3.clicked(p):
p = win.getMouse()
def result(msg,d1,d2,d3):
''' Door will change its color (red,green)'''
x = d1.reveal()
y = d2.reveal()
z = d3.reveal()
if x == 'win' or y == 'win' or z == 'win':
msg.setText('You win! Click anywhere to close!')
else:
if x == 'secret': wayout = '1'
elif y == 'secret': wayout = '2'
else: wayout = '3'
msg.setText('You lose! the correct door is door {}!
Click anywhere to close!'.format(wayout))
def exit(win):
'''Wait for las mouse click and exit'''
win.getMouse()
win.close()
if __name__ == '__main__': main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.