Advertisement
DrunkenToad

Untitled

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