Thread: Broken code - Hangman game.

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    26

    Broken code - Hangman game.

    I tried to post this earlier, but it seems like my post was not properly submitted.
    I'm trying to build a simple hangman game in C, but part of my code is breaking everything else i've done.

    Here is the code, (i commented out the block that is breaking the program):

    Code:
     #include <stdio.h>
     #include <string.h>
     #include <stdlib.h>
     #include <sys/stat.h>
     #include <time.h>
     #define MAX_WORDS 32
     #define MAX_GUESSES 15
     #define dir "/root/Desktop/WORDS"
      
      
     int main() {
         srand((unsigned int) time(NULL));
         FILE *fp;
         int charz;
         int i;
         int x = 0;
         int guesses[MAX_GUESSES];
         int lives_left = MAX_GUESSES;
         int count = 0;
         char ans[8];
         char guess[5];
         char buff[8];
         char data_full[4096];
         char *words[MAX_WORDS];
         char *random_word[1];
         struct stat stz;
         stat(dir, &stz);
         int r = rand();
         if (stz.st_size > 0) {
             fp = fopen(dir, "r");
             fseek(fp, 0, SEEK_SET);
             for (i = 0; i < stz.st_size; i++) {
                 charz = fgetc(fp);
                 if (charz == 10) {
                     strcat(data_full, " ");
      
                 } else {
                     sprintf(buff, "%c", charz);
                     strcat(data_full, buff);
                 }
             }
             fclose(fp);
             words[0] = strtok(data_full, " ");
             for (i = 0; i < MAX_WORDS; i++) {
                 if (!words[i]) {
                     break;
                 }
                 x++;
                 words[i+1] = strtok(NULL, " ");
             }
             random_word[0] = words[r%x];
             printf("Random word: %s\n", random_word[0]);
             printf("Would you like to play hangman? (Y/N): ");
             fgets(ans, sizeof(ans), stdin);
             strtok(ans, "\n");
             if (strcmp(ans, "Y") == 0) {
                 for (i = 0; i < strlen(random_word[0]); i++) {
                     guesses[i] = 0;
                 }
                 printf("\nOkay, max guesses: %d | Word length is %zu\n", MAX_GUESSES, strlen(random_word[0]));
                 while (count != strlen(random_word[0])) {
                     printf("Guess: ");
                     fgets(guess, sizeof(guess), stdin);
                     strtok(guess, "\n");
      
     //              for (i = 0; i < strlen(random_word[0]); i++) {
     //                  if (strcmp(guess, (char *) random_word[0][i]) == 0) {
     //                      printf("Guessed one!");
     //                      guesses[i] = 1;
     //                      count++;
     //                  } else {
     //                      lives_left--; // Adding this line below to decrement counter.
     //                  }
     //              }
      
                     lives_left--;
                     printf("\nLetters Guessed: ");
                     for (i = 0; i < strlen(random_word[0]); i++) {
                         printf("[%d]", guesses[i]);
                     }
                     printf("| Lives left: %d\n", lives_left);
      
                     if (lives_left == 0) {
                         printf("GAME OVER!");
                         break;
                     }
                 }
             }
         } else {
             printf("No word list found");
         }
         return 0;
     }

    This is the output when i allow the commented code to run:

    Code:
    Random word: Banana 
     Would you like to play hangman? (Y/N): Y 
      
     Okay, max guesses: 15 | Word length is 6 
     Guess: n 
      
     Process finished with exit code 11
    This is the output as is, with the code commented out:

    Code:
    Random word: Australia 
     Would you like to play hangman? (Y/N): Y 
      
     Okay, max guesses: 15 | Word length is 9 
     Guess: l 
      
     Letters Guessed: [0][0][0][0][0][0][0][0][0]| Lives left: 14 
     Guess:
    The program seems to break as soon as it hits that block, and without it, the game just loops and does not check if a guess was correct.
    Where might i have gone wrong here?
    Last edited by Strobez; 04-02-2017 at 11:54 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You should try using
    if (guess[0] == random_word[0][i] )

    That you had a cast in there was a sign of trouble.

    > char data_full[4096];
    You don't initialise this buffer before strcat-ing to it.

    I hope your word files are always smaller than the buffer size.

    > #define dir "/root/Desktop/WORDS"
    You're just asking for trouble if you're surfing the web and developing programs whilst logged in as root.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    26
    That 100% did what i was looking for. Thanks heaps.
    And i have been told that already but in not so nice words, i just figured my system is never facing the Internet directly, i have no ports forwarded on my router, and i never run any code that i'm not familiar with. I would never do it this way on a production server. Is it really that serious of an issue to be logged in as root on my own LAN? I mean that seriously, because i've been roasted over the fact a few times.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It doesn't matter whether it's internet facing or not.

    All it takes is one silly mistake at the console because you mis-typed a command, or forgot what directory you were in, and all of a sudden, you're wondering where your system rescue / re-install disks are.

    Adopting a "best practice" when it doesn't matter ensures you'll do the right thing when it does.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman game
    By GaitBait in forum C Programming
    Replies: 3
    Last Post: 12-06-2013, 01:32 PM
  2. Hangman Game
    By Mcdom34 in forum C# Programming
    Replies: 3
    Last Post: 10-13-2012, 11:21 AM
  3. Help with hangman game.
    By astarialexi in forum C Programming
    Replies: 8
    Last Post: 03-13-2011, 04:04 AM
  4. New game: Hangman!
    By abrege in forum Game Programming
    Replies: 6
    Last Post: 12-05-2002, 02:05 PM

Tags for this Thread