KageNoOni

Rock, Scissor, Python

Mar 1st, 2012
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2. ROCK=0
  3. PAPER=1
  4. SCISSORS=2
  5.  
  6. TIE=0
  7. WIN=1
  8. LOSS=2
  9.  
  10. def setup():
  11.     rpsDict=dict()
  12.     rpsDict['winLossTable']=createWinLossTable()
  13.     indexToString=[None, None, None]
  14.     indexToString[ROCK]='rock'
  15.     indexToString[PAPER]='paper'
  16.     indexToString[SCISSORS]='scissors'
  17.     rpsDict['indexToString']=indexToString
  18.     stringToIndex=dict()
  19.     stringToIndex['rock']=ROCK
  20.     stringToIndex['paper']=PAPER
  21.     stringToIndex['scissors']=SCISSORS
  22.     rpsDict['stringToIndex']=stringToIndex
  23.     results=[None, None, None]
  24.     results[TIE]='It was a tie this time.'
  25.     results[WIN]='Congratulations, you win!'
  26.     results[LOSS]='Looks like the computer beat you this round.'
  27.     rpsDict['resultStrings']=results
  28.     return rpsDict
  29.  
  30. def createWinLossTable():
  31.     winLossTable=list()
  32.     winLossTable.append([None, None, None])
  33.     winLossTable.append([None, None, None])
  34.     winLossTable.append([None, None, None])
  35.     winLossTable[ROCK][ROCK]=TIE
  36.     winLossTable[ROCK][PAPER]=LOSS
  37.     winLossTable[ROCK][SCISSORS]=WIN
  38.     winLossTable[PAPER][ROCK]=WIN
  39.     winLossTable[PAPER][PAPER]=TIE
  40.     winLossTable[PAPER][SCISSORS]=LOSS
  41.     winLossTable[SCISSORS][ROCK]=LOSS
  42.     winLossTable[SCISSORS][PAPER]=WIN
  43.     winLossTable[SCISSORS][SCISSORS]=TIE
  44.     return winLossTable
  45.  
  46. def main():
  47.     rpsDict=setup()
  48.     while True:
  49.         runGame(rpsDict)
  50.         playAgain=raw_input("Do you want to play again? (yes, no) ").lower()
  51.         if playAgain in ('y', 'yes'):
  52.             continue
  53.         exit()
  54.  
  55. def runGame(rpsDict):
  56.     while True:
  57.         playerChoice=raw_input("\nRock, Scissors, or Paper? ").lower()
  58.         try:
  59.             playerChoice=rpsDict['stringToIndex'][playerChoice]
  60.         except:
  61.             print 'That choice is invalid.'
  62.             continue
  63.         break
  64.    
  65.     computerChoice=random.randint(0,2)
  66.     print "The computer chose %s.\n" % rpsDict['indexToString'][computerChoice]
  67.    
  68.     result = rpsDict['winLossTable'][playerChoice][computerChoice]
  69.     print rpsDict['resultStrings'][result]
  70.  
  71. if __name__=='__main__':
  72.     main()
Advertisement
Add Comment
Please, Sign In to add comment