2. Name this program rpsls.c- The game of rock-paper-scissors was expanded so th
ID: 3873389 • Letter: 2
Question
2. Name this program rpsls.c- The game of rock-paper-scissors was expanded so that it now includes rock-paper- scissors-lizard-spock. The expansion was created by Sam Kass and Karen Bryla and made famous on the Big Bang wik This program asks the user for the number of games to play, then simulates playing that number of games by repeatedly generating two random numbers (0-rock, 1-paper, 2=scissors, 3=lizard, and 4-spock) and then determining the winne The main program, shown below, counts the number of times each player won and then prints the results. int main (void) srand (0) int games, pl, p2, ans, ties 0, plwins-o, p2wins 0 games = getNumGames() ; for (int a=0 ; aExplanation / Answer
#include<stdio.h>
#include<stdlib.h>
int getNumGames(void){
int inp;
printf("Please enter the number of games to play:");
scanf("%d",&inp);
return inp;
}
int findWinner(int p1, int p2){
switch(p1){
case 0:
if(p2==0)
return 0;
else if(p2==1)
return 2;
else if(p2==2)
return 1;
else if(p2==3)
return 1;
else if(p2==4)
return 2;
break;
case 1:
if(p2==0)
return 1;
else if(p2==1)
return 0;
else if(p2==2)
return 2;
else if(p2==3)
return 2;
else if(p2==4)
return 1;
break;
case 2:
if(p2==0)
return 2;
else if(p2==1)
return 1;
else if(p2==2)
return 0;
else if(p2==3)
return 1;
else if(p2==4)
return 2;
break;
case 3:
if(p2==0)
return 2;
else if(p2==1)
return 1;
else if(p2==2)
return 1;
else if(p2==3)
return 0;
else if(p2==4)
return 1;
break;
case 4:
if(p2==0)
return 1;
else if(p2==1)
return 2;
else if(p2==2)
return 2;
else if(p2==3)
return 2;
else if(p2==4)
return 0;
break;
}
return 0;
}
void printResults(int games, int ties, int p1wins, int p2wins){
if(games==ties+p1wins+p2wins){
if(p1wins>p2wins)
printf("Player1 has won the game");
else if(p1wins<p2wins)
printf("Player2 has won the game");
else
printf("Tie between Player1 and Player2);
}
}
int main(void){
srand(0);
int games,p1,p2,ans,ties=0,p1wins=0,p2wins=0;
games=getNumGames();
for(int a=0;a<games;a++){
p1=rand() % 5;
p2=rand() % 5;
ans=findWinner(p1,p2);
if(ans==0) ties++;
if(ans==1) p1wins++;
if(ans==2) p2wins++;
}
printResults(games,ties,p1wins,p2wins);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.