KageNoOni

Rock, Paper, Scissors

Feb 28th, 2012
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. import random
  2. ROCK=0
  3. PAPER=1
  4. SCISSORS=2
  5.  
  6. TIE=0
  7. WIN=1
  8. LOSE=-1
  9.  
  10. def createRpsTable():
  11.     rpsTable=list()
  12.     rpsTable.append([None, None, None])
  13.     rpsTable.append([None, None, None])
  14.     rpsTable.append([None, None, None])
  15.     rpsTable[ROCK][ROCK]=TIE
  16.     rpsTable[ROCK][PAPER]=LOSE
  17.     rpsTable[ROCK][SCISSORS]=WIN
  18.     rpsTable[PAPER][ROCK]=WIN
  19.     rpsTable[PAPER][PAPER]=TIE
  20.     rpsTable[PAPER][SCISSORS]=LOSE
  21.     rpsTable[SCISSORS][ROCK]=LOSE
  22.     rpsTable[SCISSORS][PAPER]=WIN
  23.     rpsTable[SCISSORS][SCISSORS]=TIE
  24.     return rpsTable
  25.  
  26. def main():
  27.     rpsTable=createRpsTable()
  28.     while True:
  29.         wantsToContinue=runGame(rpsTable)
  30.         if not wantsToContinue:
  31.             break
  32.  
  33. def runGame(rpsTable)
  34.     playerChoice=raw_input("Rock, Paper, or Scissors? ").lower()
  35.     while True:
  36.         if playerChoice="rock":
  37.             playerChoice=ROCK
  38.         elif playerChoice="paper":
  39.             playerChoice=PAPER
  40.         elif playerChoice="scissors":
  41.             playerChoice=SCISSORS
  42.         else:
  43.             print "Invalid choice."
  44.             continue
  45.         break
  46.    
  47.     computerChoice=random.randint(0,2)
  48.     if computerChoice=ROCK:
  49.         print "The computer chose rock."
  50.     elif computerChoice=PAPER:
  51.         print "The computer chose paper."
  52.     else:
  53.         print "The computer chose scissors."
  54.    
  55.     result = rpsTable[playerChoice][computerChoice]
  56.     if result=WIN:
  57.         print "Congratulations, you win!"
  58.     elif result=LOSE:
  59.         print "Looks like the computer beat you this round."
  60.     else:
  61.         print "It was a tie this time."
  62.    
  63.     playAgain=raw_input("Do you want to play again? (yes, no)").lower()
  64.     if playAgain in ('y', 'yes'):
  65.         return True
  66.     return False
  67.  
  68. if __name__=='__main__':
  69.     main()
Advertisement
Add Comment
Please, Sign In to add comment