Advertisement
Guest User

Untitled

a guest
Nov 5th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAXLINE 20          /* pre cursor definitions and c libraries included */
  5.     /* My attempt at creating a hangman game in c language */
  6.  
  7.  
  8.  
  9.     int main(){
  10.     FILE *fpin;
  11.     char line[MAXLINE];                 /* actual word thats read in from unknown1.txt */
  12.     char guess[MAXLINE];                    /* line of asterixs with current letters known to be displayed to user */
  13.     char character;                     /* character used for checking whether user wants to guess letter or word */
  14.     char current_guess;                 /* character for guessing a letter */
  15.     char word_guess[MAXLINE];               /* string for guessing the word all at once */
  16.     int lifes_remaining=10;                 /* amount of lives remaining */
  17.     int y,q,i;                      /* ints used for loop counters etc */
  18.     int string_compare;                 /* initial variable declaration */
  19.  
  20.    
  21.  
  22.     if((fpin=fopen("unknown1.txt","r"))== NULL)
  23.         {printf("Cannot open file\n");              /* open unknown1.txt as a read only file */
  24.         exit(EXIT_FAILURE);}
  25.    
  26.     fscanf(fpin,"%s",line);                 /* taking the text from unknown1.txt and copying it to line */
  27.  
  28.         {printf("Ready to start!, the word is");}
  29.    
  30.     int x=strlen(line);
  31.     for(y=0;y<x;y++)
  32.         {guess[y]='*';}
  33.                
  34.         guess[y]='\0';      /* letting c know when the string is ended */          
  35.         printf("%s\n",guess);  
  36.                     /* creating our guess line, which displays how many letters to be guessed/how many they have                                    already guessed*/
  37.  
  38.    
  39.     printf("the number of guesses remaining is %d\n", lifes_remaining);  /* telling the user how many lives they have left*/
  40.  
  41.     printf("would you like to guess the word [w] or guess a letter [l]\n:");  /* asking the user for word or letter */
  42.         scanf("%c", &character);
  43.        
  44.  
  45.     if(character == 'l' || character == 'L')                   /* if user selects l or L, guessing letter */
  46.         {printf("please choose a letter to guess in the word:\n");    
  47.         do
  48.             {current_guess = getchar();}                /* reading in a value for the guess from the user*/
  49.             while( !isalpha(current_guess));
  50.                
  51.                 for( i = 0 ; line[i] != '\0' ; i++)
  52.                     {if(line[i] == current_guess)       /* looping through word being guessed, checking entriesagainst guess*/                          {guess[i] = current_guess;                  /* assigning the value to the string of astrericks */
  53.                     }   }
  54.                    
  55.     printf("\n%s\n",guess);
  56.  
  57.     if(character == 'w' || character == 'W')                            /* user wants to guess word */
  58.         {printf("You have chosen to guess the word\n");
  59.         printf("\nplease enter your guess for the word:\n");
  60.     char word_guess =getchar();                         /* function to read in the word being guessed */
  61.         while (getchar() != '\n'){ current_guess = getchar();  };
  62.             string_compare = strcmp(word_guess, line);                  /* comparing the word to what we have already */
  63.  
  64.          if(string_compare==0)                              /* if ==0, they're equal, if not, incorrect guess */
  65.             {printf("congratulations, you have guessed the word");}
  66.        
  67.          else
  68.             {printf(" incorrect guess:");}
  69. }
  70. fclose(fpin);
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement