In: Computer Science
"""
CS 125 - Intro to Computer Science
File Name: CS125_Lab1.py
Python Programming
Lab 1
Name 1: FirstName1 LastName1
Name 2: FirstName2 LastName2
Description: This file contains the Python source code
for 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 temp
ArrayList)
prizes = []
for prize in self.remainingPrizesBoard:
prizes.append(prize)
prizes.sort()
# TODO 1: Print the prizes in the prize ArrayList. All values
should be on
# a single line (put a few spaces after each prize) and add a new
line
# at the end (simple for loop);
# NOTE: '${:,.2f}'.format(num) is the python
# equivalent to the Java df.format(num) DecimalFormat class, which
allows
# you to print a decimal num like 5.6 as $5.60.
"""----------------------------------------------------------------------------
Generates the banks offer. The banker algorithm computes the
average
value of the remaining prizes and then offers 85% of the
average.
----------------------------------------------------------------------------"""
def getBankerOffer(self):
pass
# TODO 2: Write code which returns the banker's offer as a
double,
# 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 a
method
# for to obtain the current bank offer - call
self.getBankerOffer())
# to the our offerHistory.
if door == -1:
pass
# This block is executed when the player accepts the banker's
offer. 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 they
accepted from the banker.
# Print the offer history (there is a method to call for
this).
else:
pass
# This block is executed when the player selects one of the
remaining doors.
# TODO 4: Obtain the prize behind the proper door and remove the
prize from the board
# Print out which door the user selected and what prize was behind
it (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 prize
remaining...so it is what they win!
# TODO 5: Set the gameIsOver variable to true
# Let the user know what prize they won behind the final
door!
# Print the offer history (there is a method to call for this).
"""----------------------------------------------------------------------------
This method is called when the game is over, and thus takes as
a
parameter the prize that was accepted (either from the banker's
offer
or the final door).
Prints out the offers made by the banker in chronological
order.
Then, prints out the money left on the table (for example, if
the
largest offer from the banker was $250,000, and you ended up
winning
$1,000, whether from a later banker offer or from the last
door,
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 loop
through
# your offerHistory member variable) and find the max offer
made.
# Print one offer per line, like:
# Offer 1: $10.00
# Offer 2: $5.00
# .....
maxOffer = 0
print("\n\n\n***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's
header above).
# Otherwise, congratulate the user that they won more than the
banker's max
# offer and display the banker's max offer.
"""
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////DO NOT EDIT ANY PORTIONS OF METHODS
BELOW///////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
"""
"""----------------------------------------------------------------------------
Processes all the code needed to execute a single turn of the
game
----------------------------------------------------------------------------"""
def playNextTurn(self):
print("-----------------------------------------------------------------------")
# Print out remaining prizes
print("There are " + str(len(self.remainingPrizesBoard)) + " prizes
remaining, listed in ascending order: ")
self.printRemainingPrizeBoard()
# Display all prize doors
print("\nThe prizes have been dispersed randomly behind the
following 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 offer
of...................." +
'${:,.2f}'.format(self.getBankerOffer()))
print("")
# Get selection from user and choose door
promptStr = "What would you like to do? Enter '-1' to accept the
banker'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. The
client
will continually call playNextTurn() until gameIsOver()
evaluates
to true.
----------------------------------------------------------------------------'''
def gameIsOver(self):
return self.gameOver
'''----------------------------------------------------------------------------
Copies the constant prizes (from an array) into a temporary
array
and uses that array to populate the initial board into the
member
variable 'remainingPrizesBoard'.
----------------------------------------------------------------------------'''
def initializeRandomPrizeBoard(self):
# Start with a fresh board with nothing in it
self.remainingPrizesBoard = []
# Copies cashPrizes array into prizes ArrayList (a temp
ArrayList)
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("")
The code will be (without the TODO comments and only the code which can be edited rest can be copy-pasted below this code)
class CS125_Lab1():
def __init__(self):
# Welcome user to the 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 temp
ArrayList)
prizes = []
for prize in self.remainingPrizesBoard:
prizes.append(prize)
prizes.sort()
for num in prizes:
print('${:,.2f}'.format(num))
"""----------------------------------------------------------------------------
Generates the banks offer. The banker algorithm computes the
average
value of the remaining prizes and then offers 85% of the
average.
----------------------------------------------------------------------------"""
def getBankerOffer(self):
ans=0
for num in self.remainingPrizesBoard:
ans+=num
if len(self.remainingPrizesBoard)>0:
ans = ans/len(self.remainingPrizesBoard)
return 0.85*ans
def chooseDoor(self, door):
self.offerHistory.append(self.getBankerOffer())
if door == -1:
self.gameOver=True
print('The game is over and the banker\'s offer is
',self.getBankerOffer())
self.printOfferHistory(self.getBankerOffer())
else:
print('You selected door '+str(door)+' and the prize behind it was
'+str(self.remainingPrizesBoard[door]))
del self.remainingPrizesBoard[door]
if len(self.remainingPrizesBoard) == 1:
self.gameOver=True
print('The prize money is
'+str(self.remainingPrizesBoard[0]))
self.printOfferHistory(self.remainingPrizesBoard[0])
def printOfferHistory(self, acceptedPrize):
maxOffer = 0
print("\n\n\n***BANKER HISTORY***")
for i in range(len(self.offerHistory)):
maxOffer=max(maxOffer,self.offerHistory[i])
print('Offer ',i+1,'${:,.2f}'.format(self.offerHistory[i]))
if maxOffer-acceptedPrize>1e-5:
print('left $'+str(maxOffer-acceptedPrize)+' on the table.')
else:
print('Congratulations you won more than the banker\'s max offer -
',maxOffer)
The screenshots are for indentations
The output is
----------------------------------------------------------------------- There are 15 prizes remaining, listed in ascending order: $0.01 $0.50 $1.00 $5.00 $10.00 $50.00 $100.00 $250.00 $500.00 $1,000.00 $5,000.00 $10,000.00 $100,000.00 $500,000.00 $1,000,000.00 The prizes have been dispersed randomly behind the following doors: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 The banker would like to make you an offer of....................$91,625.27 What would you like to do? Enter '-1' to accept the banker's offer, or select one of the doors above (0-14): 5 You selected door 5 and the prize behind it was 5000 ----------------------------------------------------------------------- There are 14 prizes remaining, listed in ascending order: $0.01 $0.50 $1.00 $5.00 $10.00 $50.00 $100.00 $250.00 $500.00 $1,000.00 $10,000.00 $100,000.00 $500,000.00 $1,000,000.00 The prizes have been dispersed randomly behind the following doors: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 The banker would like to make you an offer of....................$97,866.36 What would you like to do? Enter '-1' to accept the banker's offer, or select one of the doors above (0-13): 6 You selected door 6 and the prize behind it was 1000 ----------------------------------------------------------------------- There are 13 prizes remaining, listed in ascending order: $0.01 $0.50 $1.00 $5.00 $10.00 $50.00 $100.00 $250.00 $500.00 $10,000.00 $100,000.00 $500,000.00 $1,000,000.00 The prizes have been dispersed randomly behind the following doors: 0 1 2 3 4 5 6 7 8 9 10 11 12 The banker would like to make you an offer of....................$105,329.16 What would you like to do? Enter '-1' to accept the banker's offer, or select one of the doors above (0-12): 3 You selected door 3 and the prize behind it was 0.5 ----------------------------------------------------------------------- There are 12 prizes remaining, listed in ascending order: $0.01 $1.00 $5.00 $10.00 $50.00 $100.00 $250.00 $500.00 $10,000.00 $100,000.00 $500,000.00 $1,000,000.00 The prizes have been dispersed randomly behind the following doors: 0 1 2 3 4 5 6 7 8 9 10 11 The banker would like to make you an offer of....................$114,106.55 What would you like to do? Enter '-1' to accept the banker's offer, or select one of the doors above (0-11): 1 You selected door 1 and the prize behind it was 5 ----------------------------------------------------------------------- There are 11 prizes remaining, listed in ascending order: $0.01 $1.00 $10.00 $50.00 $100.00 $250.00 $500.00 $10,000.00 $100,000.00 $500,000.00 $1,000,000.00 The prizes have been dispersed randomly behind the following doors: 0 1 2 3 4 5 6 7 8 9 10 The banker would like to make you an offer of....................$124,479.49 What would you like to do? Enter '-1' to accept the banker's offer, or select one of the doors above (0-10): 7 You selected door 7 and the prize behind it was 10000 ----------------------------------------------------------------------- There are 10 prizes remaining, listed in ascending order: $0.01 $1.00 $10.00 $50.00 $100.00 $250.00 $500.00 $100,000.00 $500,000.00 $1,000,000.00 The prizes have been dispersed randomly behind the following doors: 0 1 2 3 4 5 6 7 8 9 The banker would like to make you an offer of....................$136,077.44 What would you like to do? Enter '-1' to accept the banker's offer, or select one of the doors above (0-9): -1 The game is over and the banker's offer is 136077.43584999998 ***BANKER HISTORY*** Offer 1 $91,625.27 Offer 2 $97,866.36 Offer 3 $105,329.16 Offer 4 $114,106.55 Offer 5 $124,479.49 Offer 6 $136,077.44 Congratulations you won more than the banker's max offer - 136077.43584999998