Rock, Paper, Scissors is a two-player game in which each player chooses one of t
ID: 3695752 • Letter: R
Question
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.
>>> rps('R', 'P') 1
>>> rps('R', 'S') -1
>>> rps('S', 'S') 0
Explanation / Answer
public int rps(char p1, char p2){ if(p1=='R' && p2=='S') return -1; else if(p1=='S' && p2=='R') return 1; else if(p1=='P' && p2=='S') return 1; else if(p1=='S' && p2=='P') return -1; else if(p1=='P' && p2=='R') return -1; else if(p1=='R' && p2=='P') return 1; else return 0; }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.