Concept explainers
Three Button Monte
Program Plan:
- Import the required packages.
- Declare a gameplay function. Inside the function,
- Create the application window using the “button.py” module.
- Activate the doors.
- Get the random number.
- Assign the values to “false”.
- Check the condition.
- Assign “point1” to “true”.
- Check the condition.
- Assign the “point2” to “true”.
- Otherwise, Assign the “point3” to “true”
- Activate all the three doors.
- Get the action mouse clicked.
- Check the condition using “while” loop.
- Check the condition for selecting the door 1 to be clicked.
- Check the condition for selecting the door 2 to be clicked.
- Check the condition for selecting the door 3 to be clicked.
- Definition of “printSummary()”.
- Print the result.
- Definition of main function.
- Creating the application window by setting title, cords and background.
- Define “Quit” button and set to active.
- Assign the text to the interface.
- Call the method draw.
- Initialize variables to enter loop
- Check the condition for “Quit” not clicked.
- Call the method “gameplay()”
- Call the method “printSummary()”
- Call the main function.
Explanation of Solution
Program:
Refer the program “button.py” given in the “Chapter 10” from the text book. Add the method “update()” along with the given code.
#Define the method update
def update(self, win, label):
#Call the method undraw()
self.label.undraw()
#Assign the position to centre
center = self.center
#Assign the label
self.label = Text(center, label)
#Set active to false
self.active = False
#Call the method draw()
self.label.draw(win)
Main.py:
#Import required packages
from button import Button
from graphics import GraphWin, Point, Text
from random import random
from time import sleep
#Definition of gameplay method
def gameplay(click, win, wins, losses):
#use button.py module to create doors and activate them
door1 = Button(win, Point(-7.5, -3), 5, 6, "Door 1")
door2 = Button(win, Point(0, -3), 5, 6, "Door 2")
door3 = Button(win, Point(7.5, -3), 5, 6, "Door 3")
#Activate the doors
door1.activate()
door2.activate()
door3.activate()
#Get the random number
x = random() * 3
#Assign the values to false
point1 = point2 = point3 = False
#check the condition
if 0 <= x <1:
#Assign point1 to True
point1 = True
#Check the condition
elif 1 <= x < 2:
#Assign the point2 to True
point2 = True
#Otherwise
else:
#Assign the point2 to True
point3 = True
#Get the action mouse click
click = win.getMouse()
#Check the condition using "while" loop
while door1.clicked(click) or door2.clicked(click) or door3.clicked(click):
#If door1 is clicked
if door1.clicked(click):
#Assign point1 to true
if point1 == True:
#Update the interface
door1.update(win,"Victory!")
#Call the method sleep
sleep(1)
#Increment the value of wins
wins = wins + 1
#Otherwise
else:
#Update the interface
door1.update(win,"Try Again")
#Call the method sleep
sleep(1)
#Increment the losses
losses = losses + 1
break
#Check the door2 is clicked
elif door2.clicked(click):
#Assign point2 to true
if point2 == True:
#Update the interface
door2.update(win,"Victory!")
#Call the method sleep
sleep(1)
#Increment the value of wins
wins = wins + 1
#Otherwise
else:
#Update the interface
door2.update(win,"Try Again")
#Call the method sleep
sleep(1)
#Increment the value of losses
losses = losses + 1
break
else:
if point3 == True:
door3.update(win,"Victory!")
#Call the method sleep
sleep(1)
#Increment the value of wins
wins = wins + 1
#Otherwise
else:
#Update the interface
door3.update(win,"Try Again")
#Call the method sleep
sleep(1)
#Increment the value of losses
losses = losses + 1
break
#Return the values
return click, wins, losses
#Definition of printSummary
def printSummary(wins, losses):
#Print the result
print("Wins: {0:5} Losses: {1:5}".format(wins, losses))
#Definition of main
def main():
#Creating the application window by setting title, cords and background
win = GraphWin("Three Button Monte", 500, 300)
win.setCoords(-12, -12, 12, 12)
win.setBackground("green3")
#define Quit button and set to active
gameover = Button(win, Point(9, 10), 3, 3, "Quit")
gameover.activate()
#Assign the text to the interface
direction = Text(Point(0, 10), "Pick a Door")
#Call the method draw
direction.draw(win)
#initialize variables to enter loop
click = Point(0,0)
wins = losses = 0
#Check the condition
while not gameover.clicked(click):
#Call the method
click, wins, losses = gameplay(click, win, wins, losses)
#Call the method printSummary
printSummary(wins, losses)
#Call the main function
main()
Output:
Screenshot of output
Clicking the “Exit” button:
Clicking the “Door1”, “Door2”, “Door3” will displays the opened door leads to “Victory!” or “Try Again”. After clicking the “Exit” button it prints the count of the corresponding result.
Wins: 1 Losses: 2
Want to see more full solutions like this?
Chapter 10 Solutions
Python Programming: An Introduction to Computer Science, 3rd Ed.
- You have a board divided into N xN squares and two robots, R1 and R2. Each square on the board has a number written on it R1 starts at the top left corner square of the board and R2 starts at the top right corner square of the board. Your aim is to move the robots so that R1 reaches the bottom right corner square and R2 reaches the bottom left corner square. You have a remote control with two buttons, blue and yellow, that control the robots as follows. • Whenever you press the blue button, R1 moves one square to the right and R2 moves one square down. • Whenever you press the yellow button, R1 moves one square down and R2 moves one square left. The robots are not allowed to move off the board-if pushing a button would force either one of the robots to move off the board, the button push has no effect and both robots stay where they are. For each robot, you compute a score which is the sum of the numbers on the squares that it visits along the path that it follows from its starting…arrow_forwardCreate a method that randomly select a letter in the alphabetarrow_forwardTrue or False: Adding more control variables will always increase the R 2 value. True Falsearrow_forward
- Please create a tic tac toe game using any programming language. Please include graphics, a score board. And ai so players can verse the computer. Include a graphic that show up when player wins (e.g. Tie, X wins, O wins)arrow_forwardprovide your own code, pleasearrow_forwardMake this as a python game. In each level it consists of 30 seconds to finish the task and needs to aim 40 points to proceed to the next level. The user should enter or type the incorrect word that doesn't match the color name.arrow_forward
- Write a Python file to create a GUI for Tic-Tac-Toe game as shown in the following figure (create the same display and same dimensions and forms): Tic-Tac-Toe game X's turn Play Again Tic-Tac-Toe game O's turn Play Againarrow_forwardAlert dont submit AI generated answer.arrow_forwardHelp I am making a elevator simulation. Can you please help me improve my code. Any help is appreciated. There are 4 types of elevators in the system:StandardElevator: This is the most common type of elevator and has a request percentage of 70%. Standard elevators have a maximum capacity of 10 passengers.ExpressElevator: This type of elevator has a request percentage of 20%. Express elevators have a maximum capacity of 8 passengers and are faster than standard elevators.FreightElevator: This type of elevator has a request percentage of 5%. Freight elevators have a maximum capacity of 5 passengers and are designed to transport heavy items.GlassElevator: This type of elevator has a request percentage of 5%. Glass elevators have a maximum capacity of 6 passengers and are designed to transport fragile item import java.util.Random; public abstract class Passenger { public static int passengerCounter = 0; private String passengerID; protected int startFloor; protected int…arrow_forward
- Write a MessageBox.Show method that displays “You win!” as the message, “Game Over” in the title bar, an OK button, and an Information icon.arrow_forwardPlease add reset button or a button that will clear all things on textfield i cant do it i am receiving an errorarrow_forwardBrowse the web to locate the lyrics to the song “The Twelve Days of Christmas.” The song contains a list of gifts received for the holiday. The list is collective so that as each “day” passes, a new verse contains all the words of the previous verse, plus a new item. Write an application that displays the words to the song starting with any day the user enters. (Hint: Use a switch statement with cases in descending day order and without any break statements so that the lyrics for any day repeat all the lyrics for previous days.)arrow_forward
- Programming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage