Advertisement
theelitenoob

Rock-Paper-Scissors

Mar 4th, 2012
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.41 KB | None | 0 0
  1. #include <stdio.h> // Standard IO
  2. #include <stdlib.h> // other stuff
  3. #include <time.h>
  4. #include <string.h>
  5. int main(int argc, const char *argv[]){
  6.     printf("Hello, Welcome to rock-paper-scissors\nBy The Elite Noob\n");
  7.     while(1){ // infinite loop :)
  8.         printf("\n\nPlease type in 1 for Rock, 2 For Paper, 3 for Scissors, 4 to quit\n");
  9.         srand(time(NULL));
  10.         int user, comp = (rand()%3)+1;
  11.         char line[255];
  12.         fgets(line, sizeof(line), stdin);  
  13.         while(sscanf(line, "%d", &user) != 1) { //1 match of defined specifier on input line
  14.             printf("You have not entered an integer.\n");
  15.             fgets(line, sizeof(line), stdin);
  16.         }              
  17.         if(user != 1 && user != 2 && user != 3  && user != 4){printf("Please enter a valid number!\n");continue;}
  18.         char umove[10], cmove[10];
  19.         if(comp == 1){strcpy(cmove, "Rock");}else if(comp == 2){strcpy(cmove, "Paper");}else{strcpy(cmove, "Scissors");}
  20.         if(user == 1){strcpy(umove, "Rock");}else if(user == 2){strcpy(umove, "Paper");}else{strcpy(umove, "Scissors");}   
  21.         if(user == 4){printf("Goodbye! Thanks for playing!\n");break;}
  22.         if((comp == 1 && user == 3)||(comp == 2 && user == 1)||(comp == 3 && user == 2)){
  23.             printf("Comp Played: %s\nYou Played: %s\nSorry, You Lost!\n", cmove, umove);
  24.         }else if(comp == user){
  25.                 printf("Comp Played: %s\nYou Played: %s\nYou Tied\n", cmove, umove);}
  26.         else{
  27.             printf("Comp Played: %s\nYou Played: %s\nYay, You Won!\n", cmove, umove);
  28.     }}return 1;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement