Menu

Lab 1 Deal Deal Python Basics Lists Problems 1 Code 100pts Write Python Application Allows Q43883333

Lab 1 – Deal or No Deal (Python Basics & Lists)
Problems
1. [CODE] [100pts] Write a Python application that allows you toplay a simplified “Deal or
No Deal” game (the classic gameshow staring Howie Mandel). Clickhere for a youtube
video if you’ve never seen it. A “compiled” Java JAR file has beenincluded so you can
play the game to get a feel for what your program should bedoing.
NOTE: The JAR file is an executable file created with Javacode…but we will be writing
our code in Python.
Before you do anything, play the game a few times using theincluded JAR file to get a
feel for what your program should do. Your program will begin bygenerating a random
board (essentially a 1D array) of prizes. You will continuallyguess a door which has a
prize behind it. When you select a door, that prize is removed fromthe game. You win
whatever is left behind the last door, and thus, the point of thegame is to hopefully be
left with the last door having $1,000,000 (or some really bigamount) behind it.
However, instead of continuing to guess, the “banker” will make youan offer
(somewhere in between the min and max prizes remaining on theboard) in an effort to
prevent himself from having to pay you the max prize value.
At the end of the game, your program will print out the offerhistory and how you did
relative to the banker’s best offer.

Specific requirements must be followed as found in the code. Youwill need to insert
code at 9 “TODO” locations. The TODO labels are numbered from 1 to9 and are
intended to be written in that order. Use the TODO statements, aswell as any method
and block comments in the code to make sure you are meeting therequirements.
Be sure to do incremental testing of your code where possible. Youdon’t want to write
code for all 9 TODO statements and then hit “play”. Bad things willhappen.

“””
   CS 125 – Intro to Computer Science
   File Name: CS125_Lab1.py
   Python Programming
   Lab 1
   Instructor: Dr. Dan

   Name 1: FirstName1 LastName1
   Name 2: FirstName2 LastName2
   Description: This file contains the Python source codefor deal or no deal.
“””

class CS125_Lab1():
def __init__(self):
# Welcome user to game
print(“Let’s play DEAL OR NO DEAL”)
  
# Define instance variables and init board
self.cashPrizes = [.01, .50, 1, 5, 10, 50, 100, 250, 500, 1000,5000, 10000, 100000, 500000, 1000000]
self.remainingPrizesBoard = []
self.gameOver = False
self.offerHistory = []
self.initializeRandomPrizeBoard()

“””—————————————————————————-
Prints the locations available to choose from
(0 through numRemainingPrizes-1)
—————————————————————————-“””
def printRemainingPrizeBoard(self):
# Copies remaining prizes ArrayList into prizes ArrayList (a tempArrayList)
prizes = []
for prize in self.remainingPrizesBoard:
prizes.append(prize)
  
prizes.sort()
  
# TODO 1: Print the prizes in the prize ArrayList. All valuesshould be on
# a single line (put a few spaces after each prize) and add a newline
# at the end (simple for loop);
  
# NOTE: ‘${:,.2f}’.format(num) is the python
# equivalent to the Java df.format(num) DecimalFormat class, whichallows
# you to print a decimal num like 5.6 as $5.60.

  
“””—————————————————————————-
Generates the banks offer. The banker algorithm computes theaverage
value of the remaining prizes and then offers 85% of theaverage.
—————————————————————————-“””
def getBankerOffer(self):
pass
# TODO 2: Write code which returns the banker’s offer as adouble,
# according to the description in this method’s comment above.

#—————————————————————————-
# Takes in the selected door number and processes the result
#—————————————————————————-
def chooseDoor(self, door):
# TODO 6: Add the current bank offer (remember, we have amethod
# for to obtain the current bank offer – callself.getBankerOffer())
# to the our offerHistory.
if door == -1:
pass
# This block is executed when the player accepts the banker’soffer. Thus the game is over.
  
# TODO 3: Set the gameOver variable to true
# Inform the user that the game is over and how much money theyaccepted from the banker.
# Print the offer history (there is a method to call forthis).
else:
pass
# This block is executed when the player selects one of theremaining doors.
  
# TODO 4: Obtain the prize behind the proper door and remove theprize from the board
# Print out which door the user selected and what prize was behindit (this prize is now gone)

         
# If only one prize remaining, game is over!!!
if len(self.remainingPrizesBoard) == 1:
pass
# This block is executed when there is only one more prizeremaining…so it is what they win!
  
# TODO 5: Set the gameIsOver variable to true
# Let the user know what prize they won behind the finaldoor!
# Print the offer history (there is a method to call for this).

“””—————————————————————————-
This method is called when the game is over, and thus takes asa
parameter the prize that was accepted (either from the banker’soffer
or the final door).
  
Prints out the offers made by the banker in chronologicalorder.
Then, prints out the money left on the table (for example, ifthe
largest offer from the banker was $250,000, and you ended upwinning
$1,000, whether from a later banker offer or from the lastdoor,
then you “left $249,000 on the table).
—————————————————————————-“””
def printOfferHistory(self, acceptedPrize):
# Print out the history of offers from the banker
  
# TODO 7: Print out the banker offer history (will need to loopthrough
# your offerHistory member variable) and find the max offermade.
# Print one offer per line, like:
# Offer 1: $10.00
# Offer 2: $5.00
# …..
maxOffer = 0
print(“nnn***BANKER HISTORY***”)
  
# TODO 8: If the max offer was greater than the accepted prize,then print out
# the $$$ left out on the table (see the example in this method’sheader above).
# Otherwise, congratulate the user that they won more than thebanker’s max
# offer and display the banker’s max offer.

“””
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////DO NOT EDIT ANY PORTIONS OF METHODSBELOW///////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
“””
  
“””—————————————————————————-
Processes all the code needed to execute a single turn of thegame
—————————————————————————-“””
def playNextTurn(self):
print(“———————————————————————–“)
  
# Print out remaining prizes
print(“There are ” + str(len(self.remainingPrizesBoard)) + ” prizesremaining, listed in ascending order: “)
self.printRemainingPrizeBoard()
  
# Display all prize doors
print(“nThe prizes have been dispersed randomly behind thefollowing doors:”)
for i in range(0, len(self.remainingPrizesBoard)):
print(str(i), end=” “)
print(“”)
  
# Print out banker’s offer and ask user what to do
print(“nThe banker would like to make you an offerof………………..” +’${:,.2f}’.format(self.getBankerOffer()))
print(“”)      
  
# Get selection from user and choose door
promptStr = “What would you like to do? Enter ‘-1’ to accept thebanker’s offer, ” + “or select one of the doors above (0-” +(str(len(self.remainingPrizesBoard)-1)) + “): “
selectedDoorNum = int(input(promptStr))
if selectedDoorNum >= -1 and selectedDoorNum <len(self.remainingPrizesBoard): # Make sure valid sel.
self.chooseDoor(selectedDoorNum)
else:
print(str(selectedDoorNum) + ” is not a valid selection.”)
print(“”)
print(“”)

”’—————————————————————————-
Basically, a getter method for the gameIsOver method. Theclient
will continually call playNextTurn() until gameIsOver()evaluates
to true.
—————————————————————————-”’
def gameIsOver(self):
return self.gameOver

”’—————————————————————————-
Copies the constant prizes (from an array) into a temporaryarray
and uses that array to populate the initial board into themember
variable ‘remainingPrizesBoard’.
—————————————————————————-”’
def initializeRandomPrizeBoard(self):
# Start with a fresh board with nothing in it
self.remainingPrizesBoard = []
  
# Copies cashPrizes array into prizes ArrayList (a tempArrayList)
prizes = []
  
for prize in self.cashPrizes:
prizes.append(prize)
  
# Randomizes prizes into remainingPrizesBoard
while len(prizes) > 0:
from random import randint
i = randint(0, len(prizes)-1)
self.remainingPrizesBoard.append(prizes[i]) # Copy into our”board”
del prizes[i]
          
# Debug print which will show the random board contents
#for p in self.remainingPrizesBoard:
# print(‘${:,.2f}’.format(p), end=” — “)
#print(“”)

Expert Answer


Answer to Lab 1 – Deal or No Deal (Python Basics & Lists) Problems 1. [CODE] [100pts] Write a Python application that allows you…

OR