Important Notes DO NOT modify the provided function declarations DO NOT create a
ID: 3861987 • Letter: I
Question
Important Notes
DO NOT modify the provided function declarations
DO NOT create any additional functions
Include the string header (#include )
Format the output using the sample output in the Example Output section as a guide
Review the Command-Line Arguments page for instructions on using command-line arguments in Visual Studio
Throughout the project, the term shape refers to any one of the legal moves: rock, paper, scissors, lizard, or Spock
Project Requirements
The program MUST accept command-line arguments (see the appropriate note above)
Display your name and the project title on separate lines, followed by a blank line
Test whether the correct number of command-line arguments were provided
The program requires 3 values, but 4 values are present in the command-line arguments
If the correct number of command-line arguments are not provided
Display an appropriate error message
Exit to the operating system WITHOUT executing the remainder of the program
If, and only if, the correct number of command-line arguments are providedCreate six (6) variables (integer) to store the following information
Leonard’s and Sheldon’s initial shapes (2)
The number of rounds to play (1)
Counters to track Leonard’s and Sheldon’s wins (2) and the number of ties (1)
Convert Leonard’s and Sheldon’s initial shapes using the convertShape function
The initial shapes are located at indexes 1 and 2 of the command-line arguments
Use the strlwr function in to convert the initial shape string to all lowercase characters
Convert the number of rounds to a numeric value
The number of rounds is located at index 3 of the command-line arguments
Use the atoi function in to convert the string to a number
Call the playGame function passing the appropriate values and references
Call the displayResults function passing the appropriate values
Function Descriptions
convertShapeConverts a shape name to its corresponding numeric value
Refer to the constants provided in the Recommended Constants section for more details
Use the return value of the strcmp function in to compare two strings
0 - the two strings are the same (including case)
Negative # - the first string exists (alphabetically) before the second string
Negative # - the first string exists (alphabetically) after the second string
determineLeonardsNextShape
Use Leonard's strategy to determine his next shape
Refer to the Player Strategies section for more details
determineSheldonsNextShapeUse Sheldon's strategy to determine his next shape
Refer to the Player Strategies section for more details
determineWinner
Determine which player won the most recent round or if the round ended in a tie
displayResults (see Example Output)
Displays Sheldon’s and Leonard’s initial shapes
Display the final result of the game (i.e., Sheldon Wins!, Leonard Wins! or Tie Game!)
Display the win count for Sheldon and Leonard, as well as the number of ties
getBetterShapeHelps Leonard choose his next shape if he lost or tied the most recent round
Refer to the Player Strategies section for more details
playGame
Play the specified number of rounds
During each round:
Determine the winner using each player’s current shape
Update the scores
Determine Leonard's next shape
It's important that you determine Leonard's next shape before determining Sheldon's next shape
Determine Sheldon's next shape
updateScore
Update the win count for the appropriate player or the tie count after each round
Game Rules (refer to Figure 1)
"Scissors cuts Paper, Paper covers Rock, Rock crushes Lizard, Lizard poisons Spock, Spock smashes Scissors, Scissors decapitates Lizard, Lizard eats Paper, Paper disproves Spock, Spock vaporizes Rock, and as it always has, Rock crushes Scissors!" - Sheldon Cooper, The Big Bang Theory
Player Strategies
Sheldon's Strategy
He selects Spock as his next shape every other round
For rounds in which Spock is his current shape
If he wins, he selects Rock as his next shape
If he ties, he selects Lizard as his next shape
If he loses, he selects Paper as his next shape
Leonard's Strategy
If he wins, he uses the same shape for the next round
If he ties, he selects the better of the two shapes that defeat his current shape
If he loses, he selects the better of the two shapes that defeat Sheldon’s current shape
Additional Notes
Play the game with a friend a couple of times to better understand the logic of the game
Use the constants defined in the Recommended Constants section to simplify the code
Ensure your source code conforms to the programming and commenting standards for the class
Submit the design document in a common file format (i.e., *.doc, *.docx, *.pdf, *.png, *.jpg)
Submit the project source code as a single *.c file
Submit a text capture of the compiler output as a *.txt file
Submit a text capture of the program output as a *.txt file
Submit the after-action report as a *.doc/*.docx or *.txt file
Example Output
Note 1: The text, "// Command-line Arguments:", provides the command-line arguments for each example; therefore, the text should not appear in the output of the program.
Note 2: Your program only needs to produce a single output.
Note 3: Your program should be able to reproduce the results of each example.
Explanation / Answer
Answer:
#include<stdio.h>
#include<string.h>
//Since the recommended constants file is not available in the question
//Assuming following constants for each of the Elements
#define ROCK 1
#define PAPER 2
#define SCISSOR 3
#define LIZARD 4
#define SPOCK 5
//Constants for game outcome
#define SHELDON_WINS 1
#define LEONARD_WINS 2
#define TIE 3
int convertShape(char* shape);
void displayResults(char* sheldonsInitialShape, int sheldonWins, char* leonardsInitialShape, int leonardWins
, int ties);
int determineLeonardsNextShape(int sheldonsCurrentShape, int leonardsCurrentShape, int gameOutcome);
int determineSheldonsNextShape(int sheldonsCurrentShape, int leonardsCurrentShape, int gameOutcome);
int determineWinner(int sheldonsShape, int leonardsShape);
int getBetterShape(int shape);
void playGame(int sheldonsInitialShape, int leonardsInitialShape, int numberGames, int* pSheldonsWinCount,
int* pLeonardsWinCount, int* pTieCount);
void updateScores(int gameOutcome, int* pSheldonsWinCount, int* pLeonardsWinCount, int* pTieCount);
int main(int argc, char* argv[]) {
if(argc != 4){
//Exiting if correct number of command line arguments aren't given
printf("Please enter correct number of inputs");
exit(0);
}
int leonardsInitialShape = 0, sheldonsInitialShape = 0, roundsToPlay = 0, sheldonWins = 0
, leonardWins = 0, ties = 0;
sheldonsInitialShape = convertShape((argv[1]));
leonardsInitialShape = convertShape((argv[2]));
roundsToPlay = atoi(argv[3]);
playGame(sheldonsInitialShape, leonardsInitialShape, roundsToPlay, &sheldonWins, &leonardWins, &ties);
displayResults(argv[1], sheldonWins, argv[2], leonardWins, ties);
}
//Function to convert each shape to its numerical value
int convertShape(char* shape){
if(strcmp(shape, "rock") == 0){
return ROCK;
}
else if(strcmp(shape, "paper") == 0){
return PAPER;
}
else if(strcmp(shape, "scissor") == 0){
return SCISSOR;
}
else if(strcmp(shape, "lizard") == 0){
return LIZARD;
}
else if(strcmp(shape, "spock") == 0){
return SPOCK;
}
}
//function to determine Leonard's Next Shape
int determineLeonardsNextShape(int sheldonsCurrentShape, int leonardsCurrentShape, int gameOutcome){
if(gameOutcome == LEONARD_WINS){
return leonardsCurrentShape;
}
if(gameOutcome == TIE){
return getBetterShape(leonardsCurrentShape);
}
if(gameOutcome == SHELDON_WINS){
return getBetterShape(sheldonsCurrentShape);
}
}
//function to determine Sheldon's Next Shape
int determineSheldonsNextShape(int sheldonsCurrentShape, int leonardsCurrentShape, int gameOutcome){
if(sheldonsCurrentShape != SPOCK){
return SPOCK;
}
else{
if(gameOutcome == SHELDON_WINS){
return ROCK;
}
if(gameOutcome == TIE){
return LIZARD;
}
if(gameOutcome == LEONARD_WINS){
return PAPER;
}
}
}
//function that returns the better of the two shapes that defeat the current shape
int getBetterShape(int shape){
if(shape == ROCK){
return PAPER;
}
if(shape == PAPER){
return SCISSOR;
}
if(shape == SCISSOR){
return SPOCK;
}
if(shape == SPOCK){
return LIZARD;
}
if(shape == LIZARD){
return ROCK;
}
}
//function to determine winner among the two players
int determineWinner(int sheldonsShape, int leonardsShape){
if(sheldonsShape == leonardsShape){
return TIE;
}
else{
switch(sheldonsShape){
case ROCK:
{
if(leonardsShape == PAPER || leonardsShape == SPOCK){
return LEONARD_WINS;
}
else{
return SHELDON_WINS;
}
break;
}
case PAPER:
{
if(leonardsShape == SCISSOR || leonardsShape == LIZARD){
return LEONARD_WINS;
}
else{
return SHELDON_WINS;
}
break;
}
case SCISSOR:
{
if(leonardsShape == SPOCK || leonardsShape == ROCK){
return LEONARD_WINS;
}
else{
return SHELDON_WINS;
}
break;
}
case SPOCK:
{
if(leonardsShape == PAPER || leonardsShape == LIZARD){
return LEONARD_WINS;
}
else{
return SHELDON_WINS;
}
break;
}
case LIZARD:
{
if(leonardsShape == ROCK || leonardsShape == SCISSOR){
return LEONARD_WINS;
}
else{
return SHELDON_WINS;
}
break;
}
}
}
}
//function to update the scores after each round of gameplay
void updateScores(int gameOutcome, int* pSheldonsWinCount, int* pLeonardsWinCount, int* pTieCount){
if(gameOutcome == TIE){
(*pTieCount)++;
}
else if(gameOutcome == SHELDON_WINS){
(*pSheldonsWinCount)++;
}
else{
(*pLeonardsWinCount)++;
}
}
//function to drive the actual gameplay using the above functions
void playGame(int sheldonsInitialShape, int leonardsInitialShape, int numberGames, int* pSheldonsWinCount,
int* pLeonardsWinCount, int* pTieCount)
{
int leonardsShape = leonardsInitialShape;
int sheldonsShape = sheldonsInitialShape;
int i;
for(i=0; i<numberGames; i++){
int gameOutcome = determineWinner(sheldonsShape, leonardsShape);
updateScores(gameOutcome, pSheldonsWinCount, pLeonardsWinCount, pTieCount);
leonardsShape = determineLeonardsNextShape(sheldonsShape, leonardsShape, gameOutcome);
sheldonsShape = determineSheldonsNextShape(sheldonsShape, leonardsShape, gameOutcome);
}
}
//function to display the final results
void displayResults(char* sheldonsInitialShape, int sheldonWins, char* leonardsInitialShape, int leonardWins
, int ties)
{
printf("Im A C Programmer");
printf(" Rock-Paper-Scissors-Lizard-Spock");
printf(" Sheldon's Initial Shape: %s", sheldonsInitialShape);
printf(" Leonard's Initial Shape: %s", leonardsInitialShape);
if(sheldonWins > leonardWins){
printf(" Sheldon Wins!");
}
else if(leonardWins > sheldonWins){
printf(" Leonard Wins!");
}
else{
printf(" Both Tie!");
}
printf(" Sheldon won %d game(s), Leonard won %d game(s) and they tied %d game(s)", sheldonWins
, leonardWins, ties);
}
------------------------------------------------------------------------------------------------------------------
OUTPUT: command line args : spock rock 3
Output:
Im A C Programmer
Rock-Paper-Scissors-Lizard-Spock
Sheldon's Initial Shape: spock
Leonard's Initial Shape: rock
Sheldon Wins!
Sheldon won 2 game(s), Leonard won 1 game(s) and they tied 0 game(s)