Advertisement
DrunkenToad

Untitled

Oct 23rd, 2018
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.96 KB | None | 0 0
  1. import random
  2.  
  3. moves = ['rock', 'paper', 'scissors']
  4.  
  5. class Player:
  6.     def __init__(self):
  7.         self.score = 0
  8.     def move(self):
  9.         return moves[0]
  10.     def learn(self, my_move, their_move):
  11.         pass
  12.  
  13. class Random(Player):
  14.     def move(self):
  15.         return(random.choice(moves))
  16.  
  17.     def learn(self, my_move, their_move):
  18.         pass
  19.  
  20. class Human(Player):
  21.     def move(self):
  22.         self.Humanmove = []
  23.         while True:
  24.             self.Humanmove = str.lower(input("rock, paper, scissors quit: "))
  25.             if self.Humanmove == "quit":
  26.                 quit()
  27.             elif self.Humanmove not in moves:
  28.                 print("Invalid move, try again")
  29.                 self.Humanmove = []
  30.  
  31.             else:
  32.                 return self.Humanmove
  33.  
  34.  
  35. class Reflect(Player):
  36.     def __init__(self):
  37.         self.Refmove = []
  38.         self.score = 0
  39.     def move(self):
  40.         if self.Refmove == []:
  41.             return moves[0]
  42.         else:
  43.             return self.Refmove
  44.     def learn(self, my_move, their_move):
  45.         self.Refmove = their_move
  46.  
  47.  
  48.  
  49. class Cycle(Player):
  50.     def __init__(self):
  51.         self.score = 0
  52.         self.Cmove = []
  53.     def move(self):
  54.         if self.Cmove == []:
  55.             return moves[0]
  56.         else:
  57.             return self.Cmove
  58.     def learn(self, my_move, their_move):
  59.         if my_move == moves[0]:
  60.             self.Cmove = moves[1]
  61.         elif my_move == moves[1]:
  62.             self.Cmove = moves[2]
  63.         else:
  64.             self.Cmove = moves[0]
  65.  
  66. def beats(one, two):
  67.     return ((one == 'rock' and two == 'scissors') or
  68.     (one == 'scissors' and two == 'paper') or
  69.     (one == 'paper' and two == 'rock'))
  70.  
  71.  
  72.  
  73. class Game:
  74.     def __init__(self, p1, p2):
  75.         self.p1 = p1
  76.         self.p2 = p2
  77.         move1 = self.p1.move()
  78.         move2 = self.p2.move()
  79.  
  80.  
  81.     def play_round(self):
  82.         move1 = self.p1.move()
  83.         move2 = self.p2.move()
  84.         print(f"\n******Player 1: {move1}  Computer: {move2}******")
  85.         if beats(move1, move2):
  86.             self.p1.score += 1
  87.             print("\n           Player 1 wins!")
  88.  
  89.         elif beats(move2, move1):
  90.             self.p2.score += 1
  91.             print("\n           Computer wins!")
  92.  
  93.         else:
  94.             print("\n           It's a tie!")
  95.  
  96.         self.p1.learn(move1, move2)
  97.         self.p2.learn(move2, move1)
  98.  
  99.  
  100.  
  101.     def play_game(self):
  102.         print("Game start!")
  103.         while True:
  104.             for round in range(1, 6):
  105.                 print(f"\nRound {round}:")
  106.                 self.play_round()
  107.                 print(f"\n*** Your Score: {self.p1.score} *** Computer Score: {self.p2.score} ***")
  108.             else:
  109.                 print("\n")
  110.                 print("******  New Game ******")
  111.                 self.play_game()
  112.  
  113.  
  114.  
  115.  
  116. players = [Random(), Player(), Reflect(), Cycle()]
  117.  
  118. if __name__ == '__main__':
  119.     game = Game(Human(), random.choice(players))
  120.     game.play_game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement