In C, write a program to score the paper-rock-scissors game. Each of two players
ID: 670859 • Letter: I
Question
In C, write a program to score the paper-rock-scissors game. Each of two players enters either P, R, or S. The program then announces the winner as well as the basis for determining the winner: “Paper covers rock”, “Rock breaks scissors”, “Scissors cut paper”, or “Draw, nobody wins”. You may use nested if statement, switch statement, or the mixture of them to do the homework. In this assignment, you don’t have to validate the inputs, just assume that p, r, s or P, R, S will be entered to test your program. We’ll implement the validations in another assignment later this week.
Output:
"Player-1 it is your turn!
Please enter your choice (p)aper. (r)ock, or (s)cissors: p
Player-2 it is your turn!
Please enter your choice (p)aper. (r)ock, or (s)cissors: r
Player1 wins! paper covers rock!"
Explanation / Answer
#include <stdio.h>
int main()
{
char p1,p2,p3;
printf("Player-1 it is your turn! ");
printf("Please enter your choice (p)aper. (r)ock, or (s)cissors: ");
p1=getchar();
scanf("%c",&p3);//to avoid the buffer
printf("Player-2 it is your turn! ");
printf("Please enter your choice (p)aper. (r)ock, or (s)cissors: ");
scanf("%c",&p2);
if(p1==p2){
printf("Draw, nobody wins ");
}
else{
if(p1=='p'){
if(p2=='r'){
printf("Player1 wins! paper covers rock! ");
}else if(p2=='s'){
printf("Player2 wins! Scissors cut paper! ");
}
}else if(p1=='r'){
if(p2=='s'){
printf("Player1 wins! Rock breaks scissors! ");
}else if(p2=='p'){
printf("Player2 wins! Paper covers rock! ");
}
}else if(p1=='s'){
if(p2=='r'){
printf("Player2 wins! Rock breaks scissors! ");
}else if(p2=='p'){
printf("Player1 wins! Scissors cut paper! ");
}
}
}
return 0;
}