Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*FileGenie.c
- Author: Sam Dunne
- Date: 18/02/2011
- Description: Command-line film guessing game.*/
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <time.h>
- #include <ctype.h>
- //Declare function prototypes
- void intro();
- void filmCall();
- void printout();
- void guessing();
- void cGuess();
- void tGuess();
- //Define global variables
- char norm_title[80];
- char lower_title[80];
- char used_char[80];
- char dummy;
- int guesses=0;
- int guess=0;
- void main()
- {
- intro(); //Calling the intro() function
- guessing(); //Calling the guessing() function
- }
- //This function provides the introduction text
- void intro()
- {
- printf("\n\n-----------------Welcome to the Film Genie-------------------\n");
- printf("-------------------Coded by Sam Dunne 2011-------------------\n");
- printf("----------------Software Engineering Project-----------------\n");
- printf("------------------------Assignment Two-----------------------\n\n");
- }
- //This function checks to see if the guess is right
- void guessing()
- {
- char choice;
- filmCall(); //Calling the filmCall() function
- printf("---------Main Menu---------\n");
- printf("1. Guess a character....(c)\n");
- printf("2. Guess full title.....(f)\n");
- printf("Choice (c/f): ");
- scanf("%c%c", &choice, &dummy);
- printf("\n"); //For neatness
- switch(choice)
- {
- case 'c': cGuess(); break;
- case 'f': tGuess(); break;
- default: printf("Invalid choice.\n"); guessing();
- }
- }
- //This function grabs the film for the guessing game
- void filmCall()
- {
- int show, len, i=0;
- //Random function ensures a random film title is chose each time
- srand(time(NULL)); //Seed the random function with the time
- show=rand()%45; //A random number between 0 and 44
- //Open the file for reading only
- FILE *fp;
- fp=fopen("filmtitles.txt", "r");
- /*Repeat until i is less than the random number generated
- This means the last title grabbed by fgets is the line assigned by the random number */
- for(i=0; i<show; ++i)
- {
- fgets(norm_title, sizeof norm_title, fp);
- }
- //Here we replace the newline character at the end of the string with '0'
- len = strlen(norm_title);
- if(norm_title[len-1]=='\n')
- {
- norm_title[len-1]=0;
- }
- strcpy(lower_title, norm_title);
- printf("%s\n", lower_title); //Testing line REMOVE
- //This changes the string to all lower case for easier checking of characters in cGuess()
- for(i=0; lower_title[i]; ++i)
- {
- lower_title[i]=tolower(lower_title[i]);
- }
- fclose(fp);
- }
- //This function prints out the appropriate characters for the guess
- void printout()
- {
- int x=0, i=0;
- for(i=0; i<strlen(lower_title); ++i)
- {
- for(x=0; x<sizeof used_char; ++x)
- {
- if(lower_title[i]==used_char[x])
- {
- printf("%c", used_char[x]);
- x = 1000;
- }//end if
- }//end while
- if(lower_title[i]!='\0' && lower_title[i]!='\n' && lower_title[i]!=' ')
- {
- printf("*");
- }
- else if(lower_title[i]==' ')
- {
- printf(" ");
- }
- }
- printf("\n");
- }
- //This function allows guessing of single characters
- void cGuess()
- {
- int value, i=0;
- char charGuess;
- printf("The movie title is: \n");
- printout(); //Calling the printout() function
- printf("\nPlease enter a character: ");
- scanf("%c%c", &charGuess, &dummy);
- for (i=0; i<80; ++i)
- {
- if(used_char[i]==charGuess)
- {
- printf("\n***|Already guessed '%c'. Try again|***\n\n", charGuess);
- cGuess();
- }
- }
- charGuess=tolower(charGuess); //Make the input character lower-case
- for(i=0; i<strlen(lower_title); ++i)
- {
- if(lower_title[i]==charGuess)
- {
- value=1;
- }
- }
- if(value==1)
- {
- printf("\n***|'%c' is correct!|***\n\n", charGuess);
- used_char[guess]=charGuess; ++guess;
- cGuess();
- }
- else
- {
- printf("\n***|'%c' is incorrect|***\n\n", charGuess);
- used_char[guess]=charGuess; ++guess; ++guesses;
- switch(guesses)
- {
- case 4: printf("***|One guess remaining|***\n"); break;
- case 5: printf("***|No more guesses, the answer was '%s'.|***\n\n\n", norm_title); exit(1);
- }
- cGuess();
- }
- }
- //This function allows guessing of full titles
- void tGuess()
- {
- char repeat, titleGuess[80];
- int i=0;
- printf("Guess the title: ");
- scanf("%s%c", titleGuess, &dummy);
- //Change guess to lower case to make checking easier
- for(i=0; titleGuess[i]; ++i)
- {
- titleGuess[i]=tolower(titleGuess[i]);
- }
- if(strcmp(lower_title,titleGuess) != 0)
- {
- printf("%s is correct!\n\n", titleGuess);
- printf("Play again (Y/N)? ");
- scanf("%c%c", &repeat, &dummy);
- }
- else
- {
- printf("%s is incorrect.\n", titleGuess);
- tGuess(); //Start to guess again
- }
- //This repeats the program starting with the guessing() function and thus skipping the intro etc.
- if(repeat=='y' || repeat=='Y')
- {
- guessing();
- }
- else
- {
- printf("Goodbye.\n");
- exit(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement