Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- moves = ['rock', 'paper', 'scissors']
- class Player:
- def __init__(self):
- self.score = 0
- def move(self):
- return moves[0]
- def learn(self, my_move, their_move):
- pass
- class Random(Player):
- def move(self):
- return(random.choice(moves))
- def learn(self, my_move, their_move):
- pass
- class Human(Player):
- def move(self):
- self.Humanmove = []
- while True:
- self.Humanmove = str.lower(input("rock, paper, scissors quit: "))
- if self.Humanmove == "quit":
- quit()
- elif self.Humanmove not in moves:
- print("Invalid move, try again")
- self.Humanmove = []
- else:
- return self.Humanmove
- class Reflect(Player):
- def __init__(self):
- self.Refmove = []
- self.score = 0
- def move(self):
- if self.Refmove == []:
- return moves[0]
- else:
- return self.Refmove
- def learn(self, my_move, their_move):
- self.Refmove = their_move
- class Cycle(Player):
- def __init__(self):
- self.score = 0
- self.Cmove = []
- def move(self):
- if self.Cmove == []:
- return moves[0]
- else:
- return self.Cmove
- def learn(self, my_move, their_move):
- if my_move == moves[0]:
- self.Cmove = moves[1]
- elif my_move == moves[1]:
- self.Cmove = moves[2]
- else:
- self.Cmove = moves[0]
- def beats(one, two):
- return ((one == 'rock' and two == 'scissors') or
- (one == 'scissors' and two == 'paper') or
- (one == 'paper' and two == 'rock'))
- class Game:
- def __init__(self, p1, p2):
- self.p1 = p1
- self.p2 = p2
- move1 = self.p1.move()
- move2 = self.p2.move()
- def play_round(self):
- move1 = self.p1.move()
- move2 = self.p2.move()
- print(f"\n******Player 1: {move1} Computer: {move2}******")
- if beats(move1, move2):
- self.p1.score += 1
- print("\n Player 1 wins!")
- elif beats(move2, move1):
- self.p2.score += 1
- print("\n Computer wins!")
- else:
- print("\n It's a tie!")
- self.p1.learn(move1, move2)
- self.p2.learn(move2, move1)
- def play_game(self):
- print("Game start!")
- while True:
- for round in range(1, 6):
- print(f"\nRound {round}:")
- self.play_round()
- print(f"\n*** Your Score: {self.p1.score} *** Computer Score: {self.p2.score} ***")
- else:
- print("\n")
- print("****** New Game ******")
- self.play_game()
- players = [Random(), Player(), Reflect(), Cycle()]
- if __name__ == '__main__':
- game = Game(Human(), random.choice(players))
- game.play_game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement