Ste1e

Simple C Hangman game

Sep 18th, 2025 (edited)
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.07 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5.  
  6. #define LENOF(A) (int)(sizeof(A)/sizeof(*A))
  7.  
  8. int main(){
  9.     char *word;
  10.     char *words[]= {"elephant", "cook", "balling", "monkey", "eggs", "pacifist", "sick", "cool", "dog", "city"};
  11.    
  12.     char *guessed;
  13.     char *guessed_before;
  14.  
  15.     int i;
  16.     int guesses_wrong = 0;
  17.  
  18.     int len;
  19.     int max_len = 0;
  20.     int word_len;
  21.  
  22.     srand(time(NULL));
  23.     word = words[rand() % (LENOF(words))];
  24.  
  25.     for(i = 0; i < LENOF(words); i++) {
  26.         len = strlen(words[i]);                         /* finds the longest word in the array */
  27.         if(len > max_len) max_len = len;
  28.     }
  29.  
  30.     guessed = malloc(sizeof(char) * max_len + 1);
  31.     if(!guessed) {perror("malloc"); return 1;}
  32.     guessed_before = malloc(sizeof(char) * max_len + 1);
  33.     if(!guessed) {perror("malloc"); return 1;}
  34.  
  35.     printf("This is a hangman game, even though there is no hanging men around here.\n");
  36.     printf("You only have 3 chances before the game ends.\n");
  37.     printf("Also, every duplicate counts as a wrong guess! Have fun!\n\n");
  38.  
  39.     word_len = strlen(word);
  40.  
  41.     for(i = 0; i < word_len; i++){                      /* fills guessed array with underscores */
  42.         guessed[i] = '_';
  43.     }
  44.     guessed[word_len] = '\0';
  45.  
  46.     printf("%s\n", guessed);
  47.    
  48.     while(strcmp(word,guessed) != 0){                   /* game over if over 3 fails */
  49.         char letter;
  50.  
  51.         if(guesses_wrong > 2){
  52.             printf("\nGame over!\nThe word was '%s'.\n", word);
  53.            
  54.             free(guessed);
  55.             free(guessed_before);
  56.             return 0;
  57.         }
  58.         printf("\nInput a letter:");
  59.         scanf(" %c", &letter); 
  60.  
  61.         strcpy(guessed_before, guessed);                /* generates before version of the array */
  62.         for(i = 0; i < word_len; i++){
  63.             if(word[i] == letter){
  64.                 guessed[i] = letter;                    /* if the letter is correct, replace underscore with it */
  65.             }  
  66.         }
  67.         printf("\n%s\n", guessed);
  68.  
  69.         if(strcmp(guessed_before, guessed) == 0){       /* if there was no change in guessed array, increase wrong count */
  70.             guesses_wrong++;
  71.             printf("\nYou have %d wrong guesses!\n", guesses_wrong);
  72.         }
  73.     }
  74.  
  75.     printf("\nCongratulations, you won!\n");
  76.  
  77.     free(guessed);
  78.     free(guessed_before);
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment