1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

anyone for hangman?

Discussion in 'C' started by hobbyist, Feb 12, 2012.

  1. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    Here's another project I've been playing around with. As with the other program, if anyone sees anything bad, please let me know.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <time.h>
    
    const char *wordlist[] = { "almost", "recovery", "tempest", "whirlwind", "zebra" };
    /* any number of words can be added, or perhaps loaded from an external file */
    
    int main(void) {
    
       char input[3] = { 0 },
            *p, *tmpstr = NULL, *cpystr = NULL, *cmpstr = NULL;
       int word, misses;
       unsigned maxlen = 0;
            
       srand((unsigned)time(NULL));
       
       for(word=0; word < sizeof(wordlist)/sizeof(wordlist[0]); ++word)     
            if(strlen(wordlist[word]) > maxlen)
               maxlen = strlen(wordlist[word]);
               
       tmpstr = malloc(maxlen + 1); /* create a few working strings */
       cpystr = malloc(maxlen + 1);  
       cmpstr = malloc(maxlen + 1);      
               
       if(!tmpstr || !cpystr || !cmpstr) { /* bail on malloc failure */
          puts("program error - couldn't allocate memory");
          return 1;
       }
    
       for(;;) {
       
          char noose[] =  {'_', '_', '_', '_', ' ', '\n',
                           '|', ' ', ' ', ' ', ' ', '\n',
                           '|', ' ', ' ', ' ', ' ', '\n',
                           '|', ' ', ' ', ' ', ' ', '\n',
                           '|', ' ', ' ', ' ', ' ', '\n',
                           '|', ' ', ' ', ' ', ' ', '\n',
                           '|', ' ', ' ', ' ', ' ', '\n',
                           '_', '_', '_', '\n', '\n', '\0' };
       
          printf("start game (y/n): ");
          fgets(input, sizeof input, stdin);
          
          if((p = strchr(input, '\n')) == NULL)
             while(getchar() != '\n'){}         
             
          if(tolower(input[0]) != 'y') {      
             break;         
          } else { 
          
             memset(tmpstr, 0, maxlen);  /* clear out any junk from game to game */
             memset(cpystr, 0, maxlen);
             memset(cmpstr, 0, maxlen);
             
             word = rand() % (sizeof(wordlist)/sizeof(wordlist[0])); /* pick a word */
             misses = 0;
             
             strncpy(cpystr, wordlist[word], strlen(wordlist[word]));
             memset(tmpstr, '-', strlen(cpystr));
             memset(cmpstr, '!', strlen(cpystr)); /* initialize the copies */
             
             do {
                
                puts(noose);
                puts(tmpstr);
                
                printf("enter letter: ");
                fgets(input, sizeof input, stdin);
                if((p = strchr(input, '\n')) == NULL)
                   while(getchar() != '\n'){}
                   
                if((p = strchr(cpystr, input[0])) != NULL) { /* user pick a letter */
                   *(tmpstr + (int)(p - cpystr)) = *p;
                   *p = '!';
                } else {
                   switch(misses) {
                   
                      case 0 : noose[9] = '|';   break;
                      case 1 : noose[15] = 'O';  break;
                      case 2 : noose[21] = '|';  break;
                      case 3 : noose[20] = '/';  break;
                      case 4 : noose[22] = '\\'; break;
                      case 5 : noose[26] = '/';  break;
                      case 6 : noose[28] = '\\'; break;
                   }
                      
                   ++misses;
                   fflush(stdout); /* force the output buffer */
                }            
                
             }while((strcmp(cpystr, cmpstr) != 0) && (misses < 7)); 
             
             puts(noose);
             
             if(strcmp(cpystr, cmpstr) == 0) {
                puts("\n\nyou win");
                puts(wordlist[word]);
                puts("\n");
             } else {         
                puts("\n\nhanged");
                puts(wordlist[word]);
                puts("\n");
             }  
          }
       }
       
       free(cmpstr);  /* release the memory */
       free(cpystr);
       free(tmpstr);      
       
       return 0;
    }
     

Share This Page