Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- typedef struct{
- char name[33];
- }Film;
- FILE *titles;
- Film film;
- char film_name[33];
- char *str1 = film_name;
- int guess_count = 0;
- void play_game();
- void random_film();
- void guess_character();
- void guess_film();
- char* lower_char(char*);
- int main() {
- srand(time(NULL));
- play_game();
- return 0;
- }
- void play_game(){
- char play_ans = 'y';
- char film_ans;
- printf("\t*********Welcome to the Film Genie!*********\n");
- fflush(stdout);
- do{
- random_film();
- printf("\nYour Film Title to guess is:\n");
- fflush(stdout);
- int length;
- length = strlen(film.name) - 1;
- for(int i = 0; i < length; i++){
- if (film.name[i] != ' ' && film.name[i] != '\0'){
- printf("*");
- fflush(stdout);
- }
- else{
- printf(" ");
- fflush(stdout);
- }
- }
- strcpy(film_name, film.name);
- str1 = lower_char(str1);
- do{
- printf("\nWould you like to guess a character? (Enter c);\nOR guess the film? (Enter f); ");
- fflush(stdout);
- scanf("%c", &film_ans);
- fflush(stdin);
- if(film_ans == 'c'){
- guess_character();
- }
- else if(film_ans == 'f'){
- guess_film();
- }
- else{
- printf("Invalid answer,\nPlease try again: ");
- fflush(stdout);
- scanf("%c", &film_ans);
- fflush(stdin);
- }
- } while(guess_count < 5);
- printf("\nDo you want to play again? ");
- fflush(stdout);
- scanf("%c", &play_ans);
- fflush(stdin);
- } while (play_ans == 'y' || play_ans == 'Y');
- printf("Thank you for playing!");
- fflush(stdout);
- }
- void random_film(){
- titles = fopen("C:\\Documents and Settings\\Owner\\Desktop\\FilmGenie\\filmtitles.txt", "rt");
- int count = 0;
- char line[33];
- while (fgets(line, 33, titles) != NULL)
- {
- count++;
- if (((rand() % 45) * count) / 45 == 0)
- strcpy(film.name, line);
- }
- fclose(titles);
- }
- void guess_character(){
- char guess;
- printf("Please Enter a Character: ");
- fflush(stdout);
- scanf("%c", &guess);
- fflush(stdin);
- if(strchr(film.name, guess) != NULL){
- printf("Your character exists! Well done. Please continue playing.");
- fflush(stdout);
- }
- else{
- printf("Your character does not exist! Please continue playing.");
- fflush(stdout);
- }
- }
- void guess_film(){
- printf("\nPlease Enter your Guess: ");
- fflush(stdout);
- char guess[33];
- gets(guess);
- char *str2 = guess;
- guess_count++;
- str2 = lower_char(str2);
- if (strcmp(film_name, guess) == 1){
- switch(guess_count){
- case 1:
- printf("\n1st guess! You got it right!");
- fflush(stdout);
- guess_count = 5;
- break;
- case 2:
- printf("\n2nd guess! You got it right!");
- fflush(stdout);
- guess_count = 5;
- break;
- case 3:
- printf("\n3rd guess! You got it right!");
- fflush(stdout);
- guess_count = 5;
- break;
- case 4:
- printf("\n4th guess! You got it right!");
- fflush(stdout);
- guess_count = 5;
- break;
- case 5:
- printf("\n5th guess! You got it right!");
- fflush(stdout);
- break;
- }
- }
- else{
- switch(guess_count){
- case 1:
- printf("\n1st guess! You got it wrong!");
- fflush(stdout);
- break;
- case 2:
- printf("\n2nd guess! You got it wrong!");
- fflush(stdout);
- break;
- case 3:
- printf("\n3rd guess! You got it wrong!");
- fflush(stdout);
- break;
- case 4:
- printf("\n4th guess! You got it wrong!");
- fflush(stdout);
- break;
- case 5:
- printf("\n5th guess! You got it wrong!");
- fflush(stdout);
- break;
- }
- }
- }
- char* lower_char(char *str){
- for(int i = 0; i < 33; i++){
- if(*(str + i) >= 'A' && *(str + i) <= 'Z'){
- *(str + i) = *(str + i) -'A' + 'a';
- }
- }
- return str;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement