Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <stdbool.h>
- int rockPaperScissors();
- char playerInput[20], computerOutput[20];
- char *inputPossibilities[6]={"", "rock", "paper", "scissors", "q", "Q"};
- int main()
- {
- int playerInputInInt, computerOutputInInt;
- bool game=1;
- printf("Welcome to Rock, Paper, Scissors\n");
- while (game==1)
- {
- printf("Choose \"rock\", \"paper\" or \"scissors\" or \"q\" to exit\n");
- scanf("%s", playerInput);
- if (!strcmp(playerInput, "rock")&&!strcmp(playerInput, "paper")&&
- !strcmp(playerInput, "scissors")&&!strcmp(playerInput, "q")&&
- !strcmp(playerInput, "Q")==false)
- {
- printf("Please input again\n");
- }
- else if (!strcmp(playerInput, inputPossibilities[4])==true)//exit
- {
- game=0;
- break;
- }
- computerOutputInInt=rockPaperScissors();
- if (!strcmp(playerInput, "rock")==true)
- playerInputInInt=1;
- if (!strcmp(playerInput, "paper")==true)
- playerInputInInt=2;
- if (!strcmp(playerInput, "scissors")==true)
- playerInputInInt=3;
- if (playerInputInInt==computerOutputInInt)//tie
- {
- printf("The game was a tie because the computer chose %s\n", computerOutput);
- }
- else if (playerInputInInt==computerOutputInInt+1||computerOutputInInt-2)//win
- {
- printf("You won because the computer chose %s!\n", computerOutput);
- }
- else if (computerOutputInInt==playerInputInInt+1||playerInputInInt-2)//lose
- {
- printf("You lost because the computer chose %s!\n", computerOutput);
- }
- }
- return 0;
- }
- int rockPaperScissors()
- {
- srand(time(NULL));
- int output=(rand()%3+1);
- strcpy(computerOutput, inputPossibilities[output]);
- return output;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement