thorax232

Rock Paper Scissors Lizard Spock

Oct 8th, 2013
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.60 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. char play = 'y';
  4. char userChoice = 'r';
  5. char comChoice = 'r';
  6. int random;
  7. int winCount = 0;
  8. int lossCount = 0;
  9. int tieCount = 0;
  10.  
  11. char comSet(int n) {
  12.     // Set computer choice to character
  13.     if(n == 1) {
  14.         return 'r';
  15.     } else if(n == 2) {
  16.         return 'p';
  17.     } else if(n == 3){
  18.         return 's';
  19.     } else if(n == 4){
  20.         return 'l';
  21.     }else {
  22.         return 'S';
  23.     }
  24. }
  25.  
  26. void compare(char u, char c) {
  27.  
  28.     // Tie
  29.     if(u == 'r' && c == 'r') {
  30.         printf("\nRock ties rock! Tie Game!");
  31.         tieCount++;
  32.     } else if(u == 'p' && c == 'p') {
  33.         printf("\nPaper ties paper! Tie Game!");
  34.         tieCount++;
  35.     } else if(u == 's' && c == 's') {
  36.         printf("\nScissors ties scissors! Tie Game!");
  37.         tieCount++;
  38.     } else if(u == 'l' && c == 'l') {
  39.         printf("\nLizard ties lizard! Tie Game!");
  40.         tieCount++;
  41.     } else if(u == 'S' && c == 'S') {
  42.         printf("\nSpock ties Spock! Tie Game!");
  43.         tieCount++;
  44.     }
  45.     // Wins
  46.     else if(u == 'r' && c == 's') {
  47.         printf("\nRock crushes scissors! You win!");
  48.         winCount++;
  49.     } else if(u == 'p' && c == 'r') {
  50.         printf("\nPaper covers rock! You win!");
  51.         winCount++;
  52.     } else if(u == 's' && c == 'p') {
  53.         printf("\nScissors cuts paper! You win!");
  54.         winCount++;
  55.     } else if(u == 'r' && c == 'l') {
  56.         printf("\nRock crushes lizard! You win!");
  57.         winCount++;
  58.     } else if(u == 'l' && c == 'S') {
  59.         printf("\nLizard poisons Spock! You win!");
  60.         winCount++;
  61.     } else if(u == 'S' && c == 's') {
  62.         printf("\nSpock melts scissors! You win!");
  63.         winCount++;
  64.     } else if(u == 's' && c == 'l') {
  65.         printf("\nScissors decapitates lizard! You win!");
  66.         winCount++;
  67.     } else if(u == 'l' && c == 'p') {
  68.         printf("\nLizard eats paper! You win!");
  69.         winCount++;
  70.     } else if(u == 'p' && c == 'S') {
  71.         printf("\nPaper disproves Spock! You win!");
  72.         winCount++;
  73.     } else if(u == 'S' && c == 'r') {
  74.         printf("\nSpock vaporizes rock! You win!");
  75.         winCount++;
  76.     }
  77.     // Losses
  78.     else if(u == 'r' && c == 'p') {
  79.         printf("\nPaper covers rock! Computer wins!");
  80.         lossCount++;
  81.     } else if(u == 'p' && c == 's') {
  82.         printf("\nScissors cuts paper! Computer wins!");
  83.         lossCount++;
  84.     } else if(u == 's' && c == 'r') {
  85.         printf("\nRock crushes scissors! Computer wins!");
  86.         lossCount++;
  87.     } else if(u == 'l' && c == 'r') {
  88.         printf("\nRock crushes lizard! Computer wins!");
  89.         lossCount++;
  90.     } else if(u == 'S' && c == 'l') {
  91.         printf("\nLizard poisons Spock! Computer wins!");
  92.         lossCount++;
  93.     } else if(u == 's' && c == 'S') {
  94.         printf("\nSpock melts scissors! Computer wins!");
  95.         lossCount++;
  96.     } else if(u == 'l' && c == 's') {
  97.         printf("\nScissors decapitates lizard! Computer wins!");
  98.         lossCount++;
  99.     } else if(u == 'p' && c == 'l') {
  100.         printf("\nLizard eats paper! Computer wins!");
  101.         lossCount++;
  102.     } else if(u == 'S' && c == 'p') {
  103.         printf("\nPaper disproves Spock! Computer wins!");
  104.         lossCount++;
  105.     } else{
  106.         printf("\nSpock vaporizes rock! Computer wins!");
  107.         lossCount++;
  108.     }
  109. }
  110.  
  111. int main() {
  112.  
  113.     printf("Welcome to Rock-Paper-Scissors-Lizard-Spock!");
  114.     printf("\nHuman Wins: %i\tComputer Wins: %i", winCount, lossCount);
  115.     printf("\nDo you want to play Rock-Paper-Scissors? (y/n) ");
  116.     scanf("%c%*c", &play);
  117.    
  118.     do {   
  119.             printf("Enter your choice (r,p,s,l,S): ");
  120.             scanf(" %c", &userChoice);
  121.    
  122.             random = (rand() % 5) + 1;
  123.             comChoice = comSet(random);
  124.             printf("Computer plays: %c", comChoice);
  125.        
  126.             compare(userChoice, comChoice);
  127.            
  128.             printf("\nHuman Wins: %i\tComputer Wins: %i\tTies: %i", winCount, lossCount, tieCount);
  129.             printf("\n************************************");
  130.             printf("\nDo you want to play Rock-Paper-Scissors? (y/n) ");
  131.             scanf(" %c", &play);
  132.     } while(play == 'y');
  133. }
Advertisement
Add Comment
Please, Sign In to add comment