Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tim Gleaves
- # Final Project
- import random
- # Intro
- print "\t\t***Welcome to Roll the Dice***\n\n"
- class DiceRoller:
- def __init__(self):
- self.dice = []
- numberOfDice = 6
- numberOfSides = 6
- def getTotal(self):
- total = 0
- for d in self.dice:
- total += d
- return total
- def rollSingleDie(self):
- return random.randrange(1, self.numberOfSides)
- def rollDice(self):
- dice = []
- for d in range(0,self.numberOfDice):
- self.dice.append(self.rollSingleDie())
- def replaceDice(self,positions):
- for pos in positions:
- self.dice[pos] = self.rollSingleDie()
- def getReplacements():
- replacements = []
- while (True):
- try:
- repdie = raw_input("Dice to replace (ENTER to end): ").strip()
- if (repdie == ""):
- break
- pos = int(repdie) - 1
- if pos < 0 or pos > DiceRoller.numberOfSides - 1:
- raise ValueError
- if pos not in replacements:
- replacements.append(pos)
- except ValueError:
- print "Please input a number between 1 and 6."
- return replacements
- def bet(totalMoney):
- playerBet = 0
- try:
- repdie = raw_input("Enter bet (max $%s): " % totalMoney)
- playerBet = int(repdie)
- if (playerBet > totalMoney):
- raise ValueError
- except ValueError:
- print ("Invalid value.")
- playerBet = bet(totalMoney)
- return playerBet
- def computerReplace(roller):
- positions = []
- for i in range(0,len(roller.dice)-1):
- d = roller.dice[i]
- if d < 3:
- positions.append(i)
- return positions
- def main():
- p1Money = 500
- while (True):
- print "Current Player Total: $%s" % p1Money
- p1Roller = DiceRoller()
- cpRoller = DiceRoller()
- print "\nPlayer 1st roll"
- p1Roller.rollDice()
- print "%s (%s)\n" % (p1Roller.dice, p1Roller.getTotal())
- print "\nComputer 1st roll"
- cpRoller.rollDice()
- print "%s (%s)\n" % (cpRoller.dice, cpRoller.getTotal())
- p1Rep = getReplacements()
- cpRep = computerReplace(cpRoller)
- p1Bet = bet(p1Money)
- print "\nPlayer 2nd roll"
- p1Roller.replaceDice(p1Rep)
- print "%s (%s)\n" % (p1Roller.dice, p1Roller.getTotal())
- print "\nComputer 2nd roll"
- cpRoller.replaceDice(cpRep)
- print "%s (%s)\n" % (cpRoller.dice, cpRoller.getTotal())
- if p1Roller.getTotal() > cpRoller.getTotal():
- winAmt = p1Bet * 2
- p1Money += p1Bet
- print "You WON $%s!\n" % winAmt
- print "*****************************"
- else:
- p1Money = p1Money - p1Bet
- print "You LOST $%s\n" % p1Bet
- print "*****************************"
- if p1Money <= 0:
- print "You're out of money. Game over."
- break
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement