Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.62 KB | None | 0 0
  1. /*FileGenie.c
  2. Author: Sam Dunne
  3. Date: 18/02/2011
  4. Description: Command-line film guessing game.*/
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <time.h>
  9. #include <ctype.h>
  10.  
  11. //Declare function prototypes
  12. void intro();
  13. void filmCall();
  14. void printout();
  15. void guessing();
  16. void cGuess();
  17. void tGuess();
  18.  
  19. //Define global variables
  20. char norm_title[80];
  21. char lower_title[80];
  22. char used_char[80];
  23. char dummy;
  24. int guesses=0;
  25. int guess=0;
  26.  
  27. void main()
  28. {
  29.     intro(); //Calling the intro() function
  30.     guessing(); //Calling the guessing() function
  31. }
  32.  
  33. //This function provides the introduction text
  34. void intro()
  35. {
  36.     printf("\n\n-----------------Welcome to the Film Genie-------------------\n");
  37.     printf("-------------------Coded by Sam Dunne 2011-------------------\n");
  38.     printf("----------------Software Engineering Project-----------------\n");
  39.     printf("------------------------Assignment Two-----------------------\n\n");
  40. }
  41.  
  42. //This function checks to see if the guess is right
  43. void guessing()
  44. {
  45.     char choice;
  46.  
  47.     filmCall(); //Calling the filmCall() function
  48.  
  49.     printf("---------Main Menu---------\n");
  50.     printf("1. Guess a character....(c)\n");
  51.     printf("2. Guess full title.....(f)\n");
  52.     printf("Choice (c/f): ");
  53.     scanf("%c%c", &choice, &dummy);
  54.  
  55.     printf("\n"); //For neatness
  56.  
  57.     switch(choice)
  58.         {
  59.         case 'c': cGuess(); break;
  60.         case 'f': tGuess(); break;
  61.         default:  printf("Invalid choice.\n"); guessing();
  62.         }
  63. }
  64.  
  65. //This function grabs the film for the guessing game
  66. void filmCall()
  67. {
  68.     int show, len, i=0;
  69.  
  70.     //Random function ensures a random film title is chose each time
  71.     srand(time(NULL)); //Seed the random function with the time
  72.     show=rand()%45; //A random number between 0 and 44
  73.  
  74.     //Open the file for reading only
  75.     FILE *fp;
  76.     fp=fopen("filmtitles.txt", "r");
  77.  
  78.     /*Repeat until i is less than the random number generated
  79.     This means the last title grabbed by fgets is the line assigned by the random number */
  80.     for(i=0; i<show; ++i)
  81.         {
  82.         fgets(norm_title, sizeof norm_title, fp);
  83.         }
  84.  
  85.     //Here we replace the newline character at the end of the string with '0'
  86.     len = strlen(norm_title);
  87.     if(norm_title[len-1]=='\n')
  88.         {
  89.         norm_title[len-1]=0;
  90.         }
  91.  
  92.     strcpy(lower_title, norm_title);
  93.  
  94.     printf("%s\n", lower_title); //Testing line REMOVE
  95.  
  96.     //This changes the string to all lower case for easier checking of characters in cGuess()
  97.     for(i=0; lower_title[i]; ++i)
  98.         {
  99.         lower_title[i]=tolower(lower_title[i]);
  100.         }
  101.  
  102.     fclose(fp);
  103. }
  104.  
  105. //This function prints out the appropriate characters for the guess
  106. void printout()
  107. {
  108.     int x=0, i=0;
  109.  
  110.     for(i=0; i<strlen(lower_title); ++i)
  111.         {
  112.         for(x=0; x<sizeof used_char; ++x)
  113.                 {
  114.                 if(lower_title[i]==used_char[x])
  115.                     {
  116.                     printf("%c", used_char[x]);
  117.                     x = 1000;
  118.                     }//end if
  119.                 }//end while
  120.  
  121.         if(lower_title[i]!='\0' && lower_title[i]!='\n' && lower_title[i]!=' ')
  122.             {
  123.             printf("*");
  124.             }
  125.         else if(lower_title[i]==' ')
  126.             {
  127.             printf(" ");
  128.             }
  129.         }
  130.     printf("\n");
  131. }
  132.  
  133. //This function allows guessing of single characters
  134. void cGuess()
  135. {
  136.     int value, i=0;
  137.     char charGuess;
  138.  
  139.     printf("The movie title is: \n");
  140.     printout(); //Calling the printout() function
  141.  
  142.     printf("\nPlease enter a character: ");
  143.     scanf("%c%c", &charGuess, &dummy);
  144.  
  145.     for (i=0; i<80; ++i)
  146.         {
  147.             if(used_char[i]==charGuess)
  148.             {
  149.             printf("\n***|Already guessed '%c'. Try again|***\n\n", charGuess);
  150.             cGuess();
  151.             }
  152.         }
  153.  
  154.     charGuess=tolower(charGuess); //Make the input character lower-case
  155.  
  156.     for(i=0; i<strlen(lower_title); ++i)
  157.             {
  158.             if(lower_title[i]==charGuess)
  159.                 {
  160.                 value=1;
  161.                 }
  162.             }
  163.  
  164.  
  165.     if(value==1)
  166.         {
  167.         printf("\n***|'%c' is correct!|***\n\n", charGuess);
  168.         used_char[guess]=charGuess; ++guess;
  169.         cGuess();
  170.         }
  171.     else
  172.         {
  173.         printf("\n***|'%c' is incorrect|***\n\n", charGuess);
  174.         used_char[guess]=charGuess; ++guess; ++guesses;
  175.         switch(guesses)
  176.             {
  177.             case 4: printf("***|One guess remaining|***\n"); break;
  178.             case 5: printf("***|No more guesses, the answer was '%s'.|***\n\n\n", norm_title); exit(1);
  179.             }
  180.         cGuess();
  181.         }
  182. }
  183.  
  184. //This function allows guessing of full titles
  185. void tGuess()
  186. {
  187.     char repeat, titleGuess[80];
  188.     int i=0;
  189.  
  190.     printf("Guess the title: ");
  191.     scanf("%s%c", titleGuess, &dummy);
  192.  
  193.     //Change guess to lower case to make checking easier
  194.     for(i=0; titleGuess[i]; ++i)
  195.         {
  196.         titleGuess[i]=tolower(titleGuess[i]);
  197.         }
  198.  
  199.     if(strcmp(lower_title,titleGuess) != 0)
  200.         {
  201.         printf("%s is correct!\n\n", titleGuess);
  202.         printf("Play again (Y/N)? ");
  203.         scanf("%c%c", &repeat, &dummy);
  204.         }
  205.     else
  206.         {
  207.         printf("%s is incorrect.\n", titleGuess);
  208.         tGuess(); //Start to guess again
  209.         }
  210.  
  211.     //This repeats the program starting with the guessing() function and thus skipping the intro etc.
  212.     if(repeat=='y' || repeat=='Y')
  213.         {
  214.         guessing();
  215.         }
  216.     else
  217.         {
  218.         printf("Goodbye.\n");
  219.         exit(1);
  220.         }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement