Advertisement
Guest User

objectoriented

a guest
Sep 8th, 2014
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.92 KB | None | 0 0
  1. import random, sys
  2.  
  3. class Player:#two methods in comun between user player and computer player
  4.     score = 0
  5.  
  6.     def update_score(self):
  7.         self.score += 1
  8.  
  9.     def get_score(self):
  10.         return self.score
  11.  
  12.  
  13.  
  14. class Computerp(Player): # The computer player class, it has in addition to the two Player method, it's own choose method.
  15.  
  16.     def choose(self):
  17.         computer_choice = random.choice("rps")
  18.         return computer_choice
  19.  
  20.  
  21. class Userp(Player): # Same as computer player class
  22.  
  23.     def choose(self):
  24.         user_ch = raw_input(" Your Choice ?   ")
  25.         if user_ch != "r" and user_ch != "p" and user_ch != "s":
  26.             print " Wrong input, Please try again.\n"
  27.             user_ch = self.choose()
  28.        
  29.         return user_ch
  30.  
  31. class Game: # This is a wrapper class for the "Game logic", it has: compare_choices method, play, and play_again methods.
  32.  
  33.     def compare_choices(self, computer_choice, user_choice):
  34.         if user_choice == "r" and computer_choice == "p":
  35.             return "computer"
  36.         elif user_choice == "r" and computer_choice == "s":
  37.             return "user"
  38.         elif user_choice == "r" and computer_choice == "r":
  39.             return "n" # n means nul
  40.         elif user_choice == "p" and computer_choice == "r":
  41.             return "user"
  42.         elif user_choice == "p" and computer_choice == "s":
  43.             return "computer"
  44.         elif user_choice == "p" and computer_choice == "p":
  45.             return "n"
  46.         elif user_choice == "s" and computer_choice == "r":
  47.             return "computer"
  48.         elif user_choice == "s" and computer_choice == "p":
  49.             return "user"
  50.         elif user_choice == "s" and computer_choice == "s":
  51.             return "n"
  52.  
  53.     def play(self):
  54.         user = Userp()
  55.         computer = Computerp()
  56.         i = 1
  57.         while i <= 3:
  58.             user_ch = user.choose()
  59.             computer_ch = computer.choose()
  60.             result = self.compare_choices(computer_ch, user_ch)
  61.             if result == 'user':
  62.                 user.update_score()
  63.                 print " Your choice was " + user_ch + ", the computer's choice was "+ computer_ch
  64.                 print " You WON ! \n"
  65.             elif result == 'computer':
  66.                 computer.update_score()
  67.                 print " Your choice was " + user_ch + ", the computer's choice was "+ computer_ch
  68.                 print " The computer won. \n"
  69.             else:
  70.                 print " Your choice was " + str(user_ch) + ", the computer's choice was "+ str(computer_ch)
  71.                 self.play_again()
  72.             i = i + 1
  73.         print "Your score was: "+str(user.get_score())+", The Computer's score was: "+str(computer.get_score())
  74.         if user.get_score() > computer.get_score():
  75.             print "You are the WINNER, CONGRATULATIONS! \n\n"
  76.         else:
  77.             print "The Computer is the WINNER. \n\n"
  78.         self.play_again()
  79.            
  80.  
  81.     def play_again(self):
  82.         print "Game Over"
  83.         choice = raw_input(" Would you like to play again? (Y/N) ")
  84.         if choice == "y" or choice == "Y":
  85.             self.play()
  86.         elif choice == "n" or choice == "N":
  87.             sys.exit()
  88.         else:
  89.             print " Wrong input, Please try again.\n"
  90.             self.play_again()
  91.  
  92.  
  93. ######################################################################
  94.  
  95. print """
  96.          +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  97.          +                          WECLOME TO ROCK, PAPER, SISSORS GAME.                         +
  98.          +                                                                                                                                          +
  99.          + To test you luck, please choose "r" for Rock, "s" Sissors and "p" for paper.+
  100.          +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  101.         """
  102.  
  103. game = Game()
  104.  
  105. game.play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement