4. Name this program rpsls.c – The game of rock-paper-scissors was expanded so t
ID: 3671305 • Letter: 4
Question
4. Name this program rpsls.c – The game of rock-paper-scissors was expanded so that it now includes rockpaper-scissors-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 complete details. Your program should read the two player moves from the command line, as shown below, and print a message indicating the winner. You can assume that the first move (lizard in the case below) is for Player One, and the second move (paper in the case below) is for Player Two. ./a.out lizard paper You can also assume that the user will always enter a valid input for each player. That is, the word will be lowercase and will be one of “rock” or “paper” or “scissors” or “lizard” or “spock”.
Explanation / Answer
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
int main(int argc, char const *argv[])
{
int endGame = 0;
char reponse[9];
int comPrecedent;
int me, com;
int scoreMe=0, scoreCom=0;
//int lastFourA[] = {0,0,0,0};
int nbCoups = 0;
int i;
weapon type[5];
type[0].name = "rock";
type[0].winTable[0] = 2, type[0].winTable[1] = 0, type[0].winTable[2] = 1, type[0].winTable[3] = 1, type[0].winTable[4] = 0;
type[0].verbs[0] = "crushes";
type[0].verbs[1] = "crushes";
type[1].name = "paper";
type[1].winTable[0] = 1, type[1].winTable[1] = 2, type[1].winTable[2] = 0, type[1].winTable[3] = 0, type[1].winTable[4] = 1;
type[1].verbs[0] = "covers";
type[1].verbs[1] = "disproves";
type[2].name = "scissors";
type[2].winTable[0] = 0, type[2].winTable[1] = 1, type[2].winTable[2] = 2, type[2].winTable[3] = 1, type[2].winTable[4] = 0;
type[2].verbs[0] = "cut";
type[2].verbs[1] = "decapitate";
type[3].name = "lizard";
type[3].winTable[0] = 0, type[3].winTable[1] = 1, type[3].winTable[2] = 0, type[3].winTable[3] = 2, type[3].winTable[4] = 1;
type[3].verbs[0] = "eats";
type[3].verbs[1] = "poisons";
type[4].name = "spock";
type[4].winTable[0] = 1, type[4].winTable[1] = 0, type[4].winTable[2] = 1, type[4].winTable[3] = 0, type[4].winTable[4] = 2;
type[4].verbs[0] = "smashes";
type[4].verbs[1] = "vaporizes";
system("clear");
printf("==================================== ");
printf("| ROCK PAPER SCISSORS LIZARD SPOCK | ");
printf("==================================== ");
printf(" Scissors cut paper. Paper covers rock. Rock crushes lizard. Lizard poisons Spock. Spock smashes scissors. Scissors decapitate lizard. Lizard eats paper. Paper disproves Spock. Spock vaporizes rock. Rock crushes scissors. ");
while(!endGame)
{
do
{
printf(" Your choice : ");
scanf("%s", reponse);
for(i=0;i<strlen(reponse);i++)
reponse[i] = tolower(reponse[i]);
} while(!checkAnswer(reponse, type));
system("clear");
me = checkAnswer(reponse, type) - 1;
do
{
com = randomInt(0, 5);
}while(com==comPrecedent);
comPrecedent = com;
nbCoups++;
printBattle(me, com, type, &scoreCom, &scoreMe);
printf(" > COM : %d You : %d ", scoreCom, scoreMe);
}
return 0;
}
int randomInt(int from, int to)
{
to = to - from;
srand(time(NULL));
return rand()% to + from;
}
int checkAnswer(char *answer, weapon *type)
{
int i;
for(i=0;i<5;i++)
{
if(strcmp(answer, type[i].name) == 0)
return i+1;
}
return 0;
}
void generateVerb(int winner, int looser, weapon *type)
{
int i;
int rangWin1;
for(i=0;i<5;i++)
{
if(type[winner].winTable[i] == 1)
{
rangWin1 = i;
}
}
i=0;
if(rangWin1 == looser)
{
printf("%s",type[winner].verbs[0]);
}
else
{
printf("%s",type[winner].verbs[1]);
}
}
void printBattle(int me, int com, weapon *type, int *scoreCom, int *scoreMe)
{
int battle;
printf("> You chose %s. Computer chose %s ", type[me].name, type[com].name);
battle = type[me].winTable[com];
if(battle == 1)
{
printf(" > %s ", type[me].name);
generateVerb(me, com, type);
printf(" %s. ", type[com].name);
printf(" > You win this round.");
*scoreMe += 1;
}
else if(battle == 0)
{
printf(" > %s ", type[com].name);
generateVerb(com, me, type);
printf(" %s. ", type[me].name);
printf(" > You lost this round.");
*scoreCom += 1;
}
else if(battle == 2)
{
printf(" > It's a tie. ");
}
}
main.h
typedef struct weapon weapon;
struct weapon
{
char *name;
int winTable[5];
char *verbs[2];
};
int randomInt(int from, int to);
void generateVerb(int winner, int looser, weapon *type);
int checkAnswer(char *answer, weapon *type);
int tolower(int c);
time_t time(time_t *t);
void printBattle(int me, int com, weapon *type, int *scoreMe, int *scoreCom);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.