Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C programming question The game of rock-paper-scissors was expanded so that it n

ID: 3586649 • Letter: C

Question

C programming question

The game of rock-paper-scissors was expanded so that it now includes rock-paperscissors-lizard-spock. The expansion was created by Sam Kass and Karen Bryla and made famous on the Big Bang Theory. See http://bigbangtheory.wikia.com/wiki/Rock_Paper_Scissors_Lizard_Spock for the game rules. 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 winner. The main program, shown below, counts the number of times each player won and then print the results.

You must use at least three functions in your program to accomplish the three tasks shown below

• int getNumGames(void); // get number of games to play

o Prompt the user for the number of games to play, read that number, return it

• int findWinner(int, int); // determine a single game winner

o Returns a 0 for a tie, 1 if player 1 won that game, and a 2 if player 2 won that game

o The two arguments passed to the function are player 1’s move (0-4) and player 2’s move (0-4)

• void printResults(int, int, int, int); // print the final results

o Prints the results after playing the specified number of games

o The four arguments passed are number of games, ties, player 1 wins, and player 2 wins

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>

int getNumGames(void);  
int findWinner(int, int);  
void printResults(int, int, int, int);  

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;
}

int getNumGames() {
printf("Enter number of games: ");
int num;
scanf("%d", &num);
return num;
}

int findWinner(int p1, int p2) {
if (p1 == p2) return 0;
  
  
switch(p1) {
// p1 rock
case 0:
// rock loses to paper and spock
if (p2 ==1 || p2 == 4) {
return 2;
} else {
return 1;
}
  
// p1 paper
case 1:
// paper loses to scissor and lizard
if (p2 == 2 || p2 == 3) {
return 2;
} else {
return 1;
}
// p1 scissor
case 2:
// paper loses to spock and rock
if (p2 == 4 || p2 == 0) {
return 2;
} else {
return 1;
}
// p1 lizard
case 3:
// paper loses to rock and scissor
if (p2 == 0 || p2 == 2) {
return 2;
} else {
return 1;
}
// p1 spock
case 4:
// paper loses to paper and lizard
if (p2 == 1 || p2 == 3) {
return 2;
} else {
return 1;
}
}
}

void printResults(int games, int ties, int p1, int p2) {
printf("Total games played: %d ", games);
printf("Total games tied: %d ", ties);
printf("Total games player 1 wins: %d ", p1);
printf("Total games player 2 wins: %d ", p2);
}