Rps

rps.c

Click on the "GetCode" link in the lower right corner of the page to get a version of the code without line numbers.

  1. /* File: rps.c */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>      
  5. #include <string.h>
  6.  
  7. char* getUserChoice() {
  8.     /* Prompt the user "Enter rock, paper, or scissors: " and return
  9.        the string they enter */
  10. }
  11.  
  12. char* getComputerChoice() {
  13.     srand (time(NULL));
  14.     /* get a pseudo-random integer between 0 and 2 (inclusive) */
  15.     int randChoice = rand() % 3;
  16.  
  17.     /* If randChoice is 0, return "rock"; if randChoice is 1,
  18.      return "paper", and if randChoice is 2, return "scissors". */
  19.  
  20. }
  21.  
  22. char* compare(char* choice1, char* choice2)
  23. {
  24.     /* Implement the logic of the game here. If choice1 and choice2
  25.      are equal, the result should be "This game is a tie."
  26.  
  27.      Make sure to use strcmp for string comparison.
  28.      */
  29. }
  30.  
  31. int main(int argc, char** argv)
  32. {
  33.     char *userChoice = NULL, *computerChoice = NULL, *outcome = NULL;
  34.  
  35.     userChoice = getUserChoice();
  36.     computerChoice = getComputerChoice();
  37.  
  38.     outcome = compare(userChoice, computerChoice);
  39.  
  40.     printf("You picked %s.\n", userChoice);
  41.     printf("Computer picked %s.\n", computerChoice);
  42.     printf("%s\n", outcome);
  43.  
  44.     return 0;
  45. }

Green Marinee theme adapted by David Gilbert, powered by PmWiki