I have to create a simulation to repeatedly play the Monty Hall game. I've created a class. This is what I have so far. I have created the buttons, activated them, deactivated the stay and quit buttons. I have a button class that is used to activate and deactivate buttons and a graphics class to create window.
What I need to now is:
randomly choose the winning door for the startGame function

get a user to select one of the doors, set instant variable for the getOriginalDoor function

deactivate door they chose, display message indicating a losing door, wait for the user to select stay or select another door, update instant variable if they change doors, return false if they stayed, true if they did not ..all for the askSwitch function

update instant variable to keep track of wins and losses when switching and staying for the playGame function

repeatedly restart game until they press the quit button, when they press quit print the four totals of results for switch win and lose, stay win and lose for run function

Please help! I am so lost now. Any help would be greatly appreciated. Thanks so much in advance.

import sys
import random as r

from graphics import *
from Button import *

class ThreeDoor:

    def __init__ (self):

        self._createWindow()
        self.stayWin = 0

    def _createWindow(self):

        self.win = GraphWin("ThreeDoor", 800, 800)
        self.playerWtext = Text(Point(500, 500), " ")
        self.playerWtext.draw(self.win)
        self.playerLtext = Text(Point(500, 600), " ")
        self.playerLtext.draw(self.win)
        self.playerStext = Text(Point(500, 700), " ")
        self.playerStext.draw(self.win)
        self.door1 = Button(self.win, Point(100, 200), 100, 200, "Door 1")
        self.door2 = Button(self.win, Point(300, 200), 100, 200, "Door 2")
        self.door3 = Button(self.win, Point(500, 200), 100, 200, "Door 3")
        self.doors = [self.door1, self.door2, self.door3]
        self.stay = Button(self.win, Point(700, 200), 100, 50, "Stay")
        self.stay.activate()
        self.quit = Button(self.win, Point(700, 300), 100, 50, "Quit")
        self.quit.activate()

    def __startGame(self):

        self.stay.deactivate()
        self.quit.deactivate()
        self.select = Text(Point(300, 500), "Select a door" )
        self.select.draw(self.win)
        self.door1.activate()
        self.door2.activate()
        self.door3.activate()
     

    def __getOriginalDoor(self):

        
       
        pass

    def __askSwitch(self):
        pass

    def __playGame(self):
        
         self.__startGame()
         self.__getOriginalDoor()

    def run(self):
        self.__playGame()
        self.win.getMouse()
        self.win.close()

def main():
    game = ThreeDoor()
    game.run()

if __name__ == '__main__':
    main()

Edited 4 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

import sys
import random as r

from graphics import *
from Button import *

class ThreeDoor:

    def __init__ (self):

        self._createWindow()
        self.stayWin = 0

    def _createWindow(self):

        self.win = GraphWin("ThreeDoor", 800, 800)
        self.playerWtext = Text(Point(500, 500), " ")
        self.playerWtext.draw(self.win)
        self.playerLtext = Text(Point(500, 600), " ")
        self.playerLtext.draw(self.win)
        self.playerStext = Text(Point(500, 700), " ")
        self.playerStext.draw(self.win)
        self.door1 = Button(self.win, Point(100, 200), 100, 200, "Door 1")
        self.door2 = Button(self.win, Point(300, 200), 100, 200, "Door 2")
        self.door3 = Button(self.win, Point(500, 200), 100, 200, "Door 3")
        self.doors = [self.door1, self.door2, self.door3]
        self.stay = Button(self.win, Point(700, 200), 100, 50, "Stay")
        self.stay.activate()
        self.quit = Button(self.win, Point(700, 300), 100, 50, "Quit")
        self.quit.activate()

    def __startGame(self):

        self.stay.deactivate()
        self.quit.deactivate()
        self.select = Text(Point(300, 500), "Select a door" )
        self.select.draw(self.win)
        self.door1.activate()
        self.door2.activate()
        self.door3.activate()


    def __getOriginalDoor(self):



        pass

    def __askSwitch(self):
        pass

    def __playGame(self):

         self.__startGame()
         self.__getOriginalDoor()

    def run(self):
        self.__playGame()
        self.win.getMouse()
        self.win.close()

def main():
    game = ThreeDoor()
    game.run()

if __name__ == '__main__':
    main()

Is any of this your code or template given by teacher? Always use code-tags!

Edited 3 Years Ago by mike_2000_17: Fixed formatting

All of this is my code. The only thing my teacher gave me was the function names, button class and graphics class. The rest of the code is things I did myself.

We don't have the Button class so can't test your code. Also, if the Button class does not provide a callback (way to call a function when clicked) you will have to write your own. So far you have done very little, even the easy things like randomly choose one door, get the player's selection, etc. so there probably won't be many replies.

Here is the Button class

from graphics import *

class Button:

"""A button is a labeled rectangle in a window.
It is activated or deactivated with the activate()
and deactivate() methods. The clicked(p) method
returns true if the button is active and p is inside it."""

def __init__(self, win, center, width, height, label):
""" Creates a rectangular button, eg:
qb = Button(myWin, Point(30,25), 20, 10, 'Quit') """

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.draw(win)
self.deactivate()

def clicked(self, p):
"Returns true if button active and p is inside"
return self.active and \
self.xmin <= p.getX() <= self.xmax and \
self.ymin <= p.getY() <= self.ymax

def getLabel(self):
"Returns the label string of this button."
return self.label.getText()

def activate(self):
"Sets this button to 'active'."
self.label.setFill('black')
self.rect.setWidth(2)
self.active = True

def deactivate(self):
"Sets this button to 'inactive'."
self.label.setFill('darkgrey')
self.rect.setWidth(1)
self.active = False

This article has been dead for over six months. Start a new discussion instead.