Rock Paper Scissors

 

At our last meeting on April 5, 2017, we continued to create our rock, paper, scissors game.  The following code is the current version of the functional game.

#include <stdio.h>
#include <stdlib.h>


//The computer choice function is where we generate a value to use as the computers
//move in the game
int ComputerChoice(int Max){

    int rc=0;
    time_t t;
    //Initialize the random number generator
    srand((unsigned) time(&amp;amp;t));
    //Generate a Random Number with a limit less than Max 
    rc=rand()%Max;
    //Increment the generated value by 1.  This will make the value from 1 to Max.
    rc++;
    //Return the generated value.
    return(rc);
}


void main(void){
 char a[5];
 int uc=0;
 int cc=9;

//We are going to wrap our game in an infinite loop so that it runs until the user quits
do{ 
     //Prompt user to enter a value
     printf("Please enter R for Rock, P for Paper, S for Scissors. Type X to quit\n");
     //Read the value the user input
     scanf("%s",&amp;amp;a);

//ifdef debug tells the compiler to online include the code between
//the ifdef and endif if DEBUG is defined at compile time
//we define DEBUG by compiling with the -D flag as in:
//cc -DDEBUG rps.c
#ifdef DEBUG
 printf("User entered %s\n",a);
#endif

//Check if the user entered a valid choice
if(!(!strcmp(a,"R")||!strcmp(a,"P")||!strcmp(a,"S")||!strcmp(a,"X"))){
     printf("Error please make a choice\n");
}

//Convert the users Letter to a numeric value for the computer
//Let R=1, P=2, S=3, X means Exit so we just return.  
switch (a[0]){
     case 'R':
              uc=1;
              break;
     case 'P':
              uc=2;
              break;
     case 'S':
              uc=3;
              break;
     case 'X':
              printf("Goodbye");
              return;
              break;
}

#ifdef DEBUG
     printf("uc=%d\n",uc);
#endif
 
 //Generate the Computers Choice.  See the function above
 //Note that we pass in 3 to get a value from 1 to 3
 cc=ComputerChoice(3);

#ifdef DEBUG
     printf("Computer Choice=%d\n",cc);
#endif
//Check for a tie game
 if(uc==cc){
      
      printf("Tie Game!\n");
 }else{  //uc==cc
      if(uc==1){
          switch(cc){
                     case 2:
                           printf("Sorry the computer won\n");
                           break;
                     case 3:
                           printf("You Win!\n");
                           break;
           }
       }
      if(uc==2){
          switch(cc){
                     case 1:
                           printf("You Win!\n");
                           break;
                     case 3:
                           printf("Sorry the computer won\n");
                           break;
           }
     }
     if(uc==3){
         switch(cc){
                     case 1:
                          printf("Sorry the computer won\n");
                          break;
                     case 2:
                          printf("You Win!\n");
                          break;
         }
      }
   }//uc==cc
//End of the loop that keeps the game going forever
}while(1);

 return;
}