In: Computer Science
Rock, Paper, Scissors is a two-player game in which each player chooses one of three items. If both players choose the same item, the game is tied. Otherwise, the rules that determine the winner are: (a) Rock always beats Scissors (Rock crushes Scissors) (b) Scissors always beats Paper (Scissors cut Paper) (c) Paper always beats Rock (Paper covers Rock) Implement function rps() that takes the choice ('R', 'P', or 'S') of player 1 and the choice of player 2, and returns −1 if player 1 wins, 1 if player 2 wins, or 0 if there is a tie. 1
>>> rps('R', 'P') 1
>>> rps('R', 'S') -1
>>> rps('S', 'S') 0
def rps(choice_1, choice_2):
#if both player enter same choice then its a tie and return 0
if(choice_1 == choice_2 ):
return 0
#if player 1 choose rock and player 2 choose paper then player 2 is winner and return 1
#if player 1 choose rock and player 2 choose scissor then player 1 is winner and return -1
elif(choice_1 == 'R'):
if(choice_2 == 'P'):
return 1
elif(choice_2 == 'S'):
return -1
#if player 1 choose scissor and player 2 choose rock then player 2 is winner and return 1
#if player 1 choose scissor and player 2 choose paper then player 1 is winner and return -1
elif(choice_1 == 'S'):
if(choice_2 == 'R'):
return 1
elif(choice_2 == 'P'):
return -1
#if player 1 choose paper and player 2 choose scissor then player 2 is winner and return 1
#if player 1 choose paper and player 2 choose rock then player 1 is winner and return -1
elif(choice_1 == 'P'):
if(choice_2 == 'S'):
return 1
elif(choice_2 == 'R'):
return -1
#print the output
print("rps('R','R'): ", rps('R','R'))
print("rps('R','P'): ",rps('R','P'))
print("rps('R','S'): ",rps('R','S'))
print("rps('P','R'): ",rps('P','R'))
print("rps('P','P'): ",rps('P','P'))
print("rps('P','S'): ",rps('P','S'))
print("rps('S','R'): ",rps('S','R'))
print("rps('S','P'): ",rps('S','P'))
print("rps('S','S'): ",rps('S','S'))
OUTPUT: