D10d3

rockpaperscissors.py

Sep 10th, 2014
363
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Rock Paper Scissors by D10d3
  2. #Goal:
  3. #    Ask the player if they pick rock paper or scissors
  4. #    Have the computer chose its move
  5. #    Compare the choices and decide who wins
  6. #    Print the results
  7. #Sub goals:
  8. #    Let the player play again
  9. #    Keep a record of the score e.g. (Player: 3 / Computer: 6)
  10. #---------------------------------------------------------------------------------------------
  11.  
  12. #adds the ability to create random choices. example:
  13. #foo = ['a', 'b', 'c', 'd', 'e']
  14. #print(random.choice(foo))
  15. import random
  16.  
  17. def p_shoot(): #Player chooses move
  18.     valid = False
  19.     while not valid:
  20.         print """
  21.     What move would you like to play?
  22.     Type your selection:
  23.     (R) Rock
  24.     (P) Paper
  25.     (S) Scissors"""
  26.         p_choice = raw_input('>')
  27.         p_choice = p_choice.lower()
  28.         if p_choice == 'r':
  29.             p_move = "Rock"
  30.             valid = True
  31.         elif p_choice == 'p':
  32.             p_move = "Paper"
  33.             valid = True
  34.         elif p_choice == 's':
  35.             p_move = "Scissors"
  36.             valid = True
  37.     print "You played %s" % p_move
  38.     return p_move
  39.  
  40. def c_shoot(): #generate computer move
  41.     c_hand = ['Rock', 'Paper', 'Scissors']
  42.     throw = random.choice(c_hand)
  43.     return throw
  44.  
  45. def compair(p,c): # my sorry excuse for "logic"
  46.     if p == c:
  47.         winner = "Tie"
  48.     elif p == "Rock":
  49.         if c == "Paper":
  50.             winner = "Computer"
  51.         else:
  52.             winner = "Player"
  53.     elif p == "Paper":
  54.         if c == "Scissors":
  55.             winner = "Computer"
  56.         else:
  57.             winner = "Player"
  58.     elif p == "Scissors":
  59.         if c == "Rock":
  60.             winner = "Computer"
  61.         else:
  62.             winner = "Player"
  63.     return winner      
  64.  
  65. #Begin Game and set up variables
  66. print "Hi there, let's play Rock Paper Scissors!"  
  67. playing = True
  68. c_wins = 0
  69. p_wins = 0
  70. ties = 0
  71.  
  72. #Begin game Loop
  73. while playing:
  74.     p_move = p_shoot()
  75.     c_move = c_shoot()
  76.     winner = compair(p_move,c_move) #declare winner
  77.     print "Computer played %s" % c_move
  78.     if winner == "Tie":
  79.         print "The match is tied!"
  80.         ties = ties + 1
  81.     elif winner == "Player":
  82.         print "The Player is the winner!"
  83.         p_wins = p_wins + 1
  84.     elif winner == "Computer":
  85.         print "The Computer is the winner!"
  86.         c_wins = c_wins + 1
  87.     #print scores
  88.     print "Scores:"
  89.     print "Player  : %d" % p_wins
  90.     print "Computer: %d" % c_wins
  91.     print "Ties    : %d" % ties
  92.     #ask player if they'd like to play again
  93.     valid = False
  94.     while not valid:
  95.         print "Would you like to play again? (Y/N)"
  96.         again = raw_input('>')
  97.         again = again.lower()
  98.         if again == "n":
  99.             print "Thank you for playing"
  100.             quit()
  101.         elif again =="y":
  102.             valid = True
RAW Paste Data