Question: PYTHON Take the complete Dice Poker program from class and add code to it so that it does error checking. Once you add error checking,
PYTHON
Take the complete Dice Poker program from class and add code to it so that it does error checking. Once you add error checking, no matter what the user types in, the program should never crash and always operate in a reasonable manner that follows all the rules of the game.
''' I intend this as a design document, even though I use Python syntax. It describes all the classes and functions using docstrings. '''
# UTILITY FUNCTIONS def strNumList(lt): 'Converts a list of strings (of integers) to a list of integers' # Right now, no error checking ... for i in range(len(lt)): lt[i] = int(lt[i])
from random import *
class Dice: ''' This class models 5 six-sided dice, which are are understood to be numbered die 1, die 2, die 3, die 4, and die 5. '''
def __init__(self): 'Initializes the 5 dice to 5 random values' self.dieFaces = [1,1,1,1,1] # initialize list self.roll([1,2,3,4,5])
def getFaces(self): 'Returns the current values of the 5 dice as list' return self.dieFaces
def roll(self, rList): ''' Input: A list whose length is between 0 and 5, inclusive. Each entry in the list is an integer between 1 and 5, inclusive. There are no repeated entries in the list, i.e. it is essentially a set. This method then randomly rolls the dice corresponding to this list, for example: if rList is [2,3,5] then die 2, die 3, and die 5 are re-rolled. ''' for i in rList: self.dieFaces[i-1] = randint(1,6)
def score(self): ''' Returns two values based on the current state of the dice. First value: A string describing the hand Second value: The numeric score of the hand ''' # COME BACK TO! return '3 of a kind', 12 # temporary return
def testDice():
D = Dice() D.roll() print(D.score())
class UserInterface:
def printIntro(self): 'Prints introduction to the game' print("Welcome to Dice Poker!") print("For the rules, google it ...")
def continuePlay(self): ''' Asks the user if they want to continue playing or quit. For continuing play it accepts the following strings: yes, Yes, y, Y For quitting play, it accepts the following strings: no, No, n, N The user may input illegal strings. This method continues to ask the user for input, till a valid input is given. It returns a boolean, True for continue playing, False for not. ''' while True: response = input("Continue play (Yes or No)? ") if response in ('Yes','yes','Y','y'): return True if response in ('No','no','N','n'): return False print("Give a valid response! ...")
def printMoney(self, money): ''' Input: A float indicating how much money the user currently has. Prints out this amount of money for the user to see ''' print("You have ${}".format(money))
def printHand(self, faceList): ''' Input: A list indicating the current state of the dice. Prints out the die values for the user to see ''' dieNum = 1 for face in faceList: print("Die {}: {}.".format(dieNum,face), end=' ') dieNum = dieNum + 1 print() print()
def roundStatus(self, handName, handValue, totalMoney): print("Your hand was a {}.".format(handName)) print("You earned ${}.".format(handValue)) print("You now have a total of ${}.".format(totalMoney))
def reRollQuestion(self): ''' Asks the user which dice they want to re-roll. Expects a comma separated list of integers (example: 2,3,5). This method repeatedly asks the user for such a list, till a valid list is given (see the roll method of Dice for what counts as a valid list). Note that an empty list is a valid input (i.e. the user just hits return) ''' response = input("Which dice to re-roll: ") responseList = response.split(',')
if response == '': return [] else: strNumList(responseList) # currently no error checking! return responseList
def printConclusion(self, money): ''' Input: A float indicating the amount of money the player has Prints out a conclusion to the player, including how much money they now have. ''' print("Thanks for playing!") print("You ended up with ${}...".format(money))
def testUserInterface():
U = UserInterface() U.printIntro() U.printConclusion(70) print(U.reRollQuestion()) U.roundStatus('Pair', 5, 50)
class DiceGame:
def __init__(self, startMoney = 100): self.money = startMoney self.dice = Dice() self.UI = UserInterface() self.roundCost = 10
def adjustMoney(self, x): self.money += x
def continueGame(self): 'Returns True if the player has enough money and wants to continue' return self.money >= self.roundCost and self.UI.continuePlay()
def playGame(self):
self.UI.printIntro() while self.continueGame(): self.playRound()
self.UI.printConclusion(self.money)
def rollEnhancement(self): dieList = self.UI.reRollQuestion() self.dice.roll(dieList) self.UI.printHand( self.dice.getFaces() )
def playRound(self): self.adjustMoney(-self.roundCost) self.dice.roll([1,2,3,4,5]) currentFaces = self.dice.getFaces() self.UI.printHand(currentFaces)
for i in range(2): self.rollEnhancement()
handName, value = self.dice.score() self.adjustMoney(value) self.UI.roundStatus(handName, value, self.money)
def main(): game = DiceGame(150) game.playGame()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
