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

adding a dictionary to hangman

Discussion in 'C' started by maltech, Dec 16, 2010.

  1. maltech

    maltech New Member

    Joined:
    Dec 16, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I would like to add a dictionary to the hangman code below. The dictionary must give the meaning of the word, origin, and pronounciation of the word. Please advise me how to go about doing this.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <ctype.h>
    #include <conio.h>
    #include <time.h>
    #include <string.h>
    
    
    char fileLoc[500]; /* The backup file location*/
    
    void showLogo() 
    {
         printf("|==============================================|\n");
         printf("|========== CSI 211 FINAL PROJECT =============|\n");
         printf("|==============================================|\n");
         printf("|===============HANGING PUZZLE GAME============|\n");
         printf("|============ CSI 211 FINAL PROJECT ===========|\n");
         printf("|==============================================|\n");   
    }
    
    void prn_galg(int i) 
    {
         switch (i) 
         {
                case 0 :
                     printf("Amount of wrong letters: %d\n\n", i);
                     printf("\n");
                     printf("\n");
                     printf("\n");
                     printf("\n");
                     printf("\n");
                     printf("\n");
                     printf("____________\n\n");
                break;
                     
                case 1 :
                     printf("Amount of wrong letters: %d\n\n", i);
                     printf("\n");
                     printf("  |\n");
                     printf("  |\n");
                     printf("  |\n");
                     printf("  |\n");
                     printf("  |\n");
                     printf("__|_________\n\n");
                break;
                     
                case 2 :
                     printf("Amount of wrong letters: %d\n\n", i);
                     printf("  _______\n");
                     printf("  |\n");
                     printf("  |\n");
                     printf("  |\n");
                     printf("  |\n");
                     printf("  |\n");
                     printf("__|_________\n\n");
                break;
                     
                case 3 :
                     printf("Amount of wrong letters: %d\n\n", i);
                     printf("  _______\n");
                     printf("  |/\n");
                     printf("  |\n");
                     printf("  |\n");
                     printf("  |\n");
                     printf("  |\n");
                     printf("__|_________\n\n");
                break;
                     
                case 4 :
                     printf("Amount of wrong letters: %d\n\n", i);
                     printf("  _______\n");
                     printf("  |/   | \n");
                     printf("  |    O \n");
                     printf("  |\n");
                     printf("  |\n");
                     printf("  |\n");
                     printf("__|_________\n\n");
                break;
                
                case 5 :
                     printf("Amount of wrong letters: %d\n\n", i);
                     printf("  _______\n");
                     printf("  |/   | \n");
                     printf("  |    O \n");
                     printf("  |    |\n");
                     printf("  |    |\n");
                     printf("  |\n");
                     printf("__|_________\n\n");
                break;
                
                case 6 :
                     printf("Amount of wrong letters: %d\n\n", i);
                     printf("  _______\n");
                     printf("  |/   | \n");
                     printf("  |    O \n");
                     printf("  |   \\|\n");
                     printf("  |    | \n");
                     printf("  |\n");
                     printf("__|_________\n\n");
                break;
                
                case 7 :
                     printf("Amount of wrong letters: %d\n\n", i);
                     printf("  _______\n");
                     printf("  |/   | \n");
                     printf("  |    O \n");
                     printf("  |   \\|/\n");
                     printf("  |    | \n");
                     printf("  |\n");
                     printf("__|_________\n\n");
                break;
                
                case 8 :
                     printf("Amount of wrong letters: %d\n\n", i);
                     printf("  _______\n");
                     printf("  |/   | \n");
                     printf("  |    O \n");
                     printf("  |   \\|/\n");
                     printf("  |    | \n");
                     printf("  |   /\n");
                     printf("__|_________\n\n");
                break;
                
                case 9 :
                     printf("Amount of wrong letters: %d\n\n", i);
                     printf("  _______\n");
                     printf("  |/   | \n");
                     printf("  |    O \n");
                     printf("  |   \\|/\n");
                     printf("  |    | \n");
                     printf("  |   / \\\n");
                     printf("__|_________\n\n");
                break;
                
                case 10 :
                     printf("Amount of wrong letters: %d\n\n", i);
                     printf("  _______\n");
                     printf("  |/   | \n");
                     printf("  |    X \n");
                     printf("  |   \\|/\n");
                     printf("  |    | \n");
                     printf("  |   / \\\n");
                     printf("__|_________\n\n");
                break;
     }
    }
    
    char randomNumber(int max_number) 
    {
         srand(time(NULL));
         int g = (rand() % (max_number + 1));
         return g;
    }
    
    char *getWord() 
    {
         char c[50000];  /* declare a char array */
         int n;
         FILE *file;  /* declare a FILE pointer  */
         /* Opening words file */
         if (strcmp(fileLoc, "") != 1) 
         {
                             /* Here is the default words file, you can change this into whatever words file you'd like.*/
                             file = fopen("words.txt", "r");
         } 
         else 
         {
              file = fopen(fileLoc, "r");
         }
         
         /* Incase the file cant be openend */
         
         if(file==NULL) 
         {
                     printf("An error has occured: can't open words file.\nPlease type the location of the words file:\n");
                     scanf("%s", fileLoc);
                     printf("Reading file '%s'.....\n\n", fileLoc);
                     file = fopen(fileLoc, "r");
                     if (file == NULL) 
                     {
                              while (file==NULL) 
                              {
                                    printf("That file doesn't exist. Enter the location of the words file:\n");
                                    scanf("%s", fileLoc);
                                    printf("Reading file '%s'.....\n\n", fileLoc);
                                    file = fopen(fileLoc, "r");
                              }
        }
        printf("File has been read.\n\n");
        n = fread(c, 1, 50000, file);
        c[n] = '\0';
        
        }  
        else 
        {
           /* Reading the contents of the file */
           n = fread(c, 1, 50000, file);
           c[n] = '\0';
        }
        /* Separating the contents, divided by | and declaring variables */
        char *token = strtok(c, "|");
        char *words[200] = {0};
        int f = 0;
        while(token != NULL)
        {
                    /* Allocating memory for the pointer */
                    
                    words[f] = malloc(strlen(token)+1);
                    
                    /* Copying entire string to pointer */
                    
                    strcpy(words[f],token);
                    
                    /* Resetting pointer */
                    
                    token = strtok(NULL, "|");
                    
                    f++;
                    
        }
        /* Closing the file */
    
        fclose(file);
    
        /* Retrieving a random number */
    
        int wordN = randomNumber(f);
    
        /* Freeing all the memory allocated for the strings */
    
        int q;
        for(q  = 0; q < 200; q++)
        {
            if( q != wordN) 
            {
                free(words[q]);
            }
        }
    
        /* Returning string */
    
        return words[wordN];
    
    }
    
    int main(void) 
    {
        char udi[] = "EMPTY";
        while ((strcmp(udi, "END") != 0) && ((strcmp(udi, "AGAIN") == 0) || (strcmp(udi, "EMPTY") == 0))) 
        {
              strcpy(udi, "EMPTY");
              
              /* Declaring variables */
              /* Random deciding which word is chosen to be guessed:
              guessWord is the word that needs to be guessed
              currentWord is the word that is filled with dots */
              
              /* Retrieving the word that matches with the wordNumber */
              /* Check which number was chosen: printf("%d", wordNumber); */
    
              char *tempWord = getWord();
              
              /* Declaring the guessWord with the length */
              
              char guessWord[strlen(tempWord)+1];
              
              /* Copying the string into guessWord */
              
              strcpy(guessWord, tempWord);
              
              /* Freeing the pointer */
              
              free(tempWord);
              
              /* Calculate the length of the guessWord */
              
              int wordlength = strlen(guessWord);
              
              /* Creating the dotword (name: currentWord) */
              
              char* currentWord = malloc( wordlength );
              
              int t;
              
              for (t = 0; t <= wordlength; t++) 
              
              {
                  if (t == wordlength) 
                  
                  {
                        currentWord[t] = '\0';
                  } 
                  
                  else 
                  
                  {
                       currentWord[t] =  '.';
                  }
                  
              }
              
              /* Currentword check: printf("Currentword: \"%s\"", currentWord); */
              
              /* Declaring variables */
              
              int errors = 0; /* Error amount, if its higher than 10 the loop stops */
              
              int guessedLetter = 0; /* Boolean Integer used to check whether the player entered a correct letter 0 = false, 1 = true */
              
              int i,n = 1;
              
              char c;
              
              /* Printing logo */
              
              showLogo();
              
              /* Printing introduction message */
              
              printf("%s\n\n%s\n%s\n%s\n%s\n\n%s%s\n\n",
              "Welcome to Hanging!",
              "Hanging is created to stimulate your thinking",
              "by using letters to solve the puzzle and avoid being hang",
              "To avoid being hang you enter the correct upper or lower case letters .",
              "Entering an incorrect letter would lead to you being hung gradually.",
              "What do you think this word is: ",
              currentWord);
              printf("%d.     %s", n, "Please enter the letter(s) that you think will complete the word: ");
              
              /* As long as the word hasn't been guessed or the errors are lower than 10: */
              
              while( (strcmp(currentWord, guessWord) != 0) && (errors < 10) )
              
              {
                     scanf("%c", &c); /* Retrieving the user entry */
                     c = tolower(c); /* Removing caps */
                     if (c != '\n') 
                     
                     {
                           if (isalpha(c)) 
                           { 
                                           /* Making sure that the letter is alphanumeric */
                                           /* Checking whether the letter that has been entered (c) occurs in the guessWord */
                                           
                                           for (i = 0; i < wordlength; i++) 
                                           {
                                               if (guessWord[i] == c) 
                                               {
                                                                currentWord[i] = c;
                                                                guessedLetter = 1;
                                               }
                                               
                                           }
                                           
                                           /* Actions taken if the letter c doesn't occur in the guessWord and when it does */
                                           
                                           if (guessedLetter == 0) 
                                           {
                                                             errors++;
                                                             printf("\nYou have entered an incorrect letter.\n\n");
                                           } 
                                           
                                           else 
                                           
                                           {
                                                guessedLetter = 0;
                                                printf("\nGreat! You've entered the correct letter.\n\n");
                                           }
                                           /* Showing the word and the amount of errors */
                                           printf("%s%s\n\n", "The word including the letters you guessed: ", currentWord);
                                           prn_galg(errors);
                                           n++; /* Increasing attempt amount */
                                           /* Showing header if the word has not been guessed and the errors are lower than 10 */
                                           if ( (strcmp(currentWord, guessWord) != 0) && (errors < 10) ) 
                                           {
                                                printf("%d.     %s", n, "Please enter the letter(s) that you think will complete the word: ");
                                           }
                                           
                                           /* If the letter isn't alphanumeric (isalpha()) */
                                           
                           }
                           
                           else 
                           
                           {
                                printf("Only alphanumeric symbols are allowed (a-z, A-Z), try again:\n");
                           }
                     }
                     
              }
              
              /* Showing the results, whether the player won or not  */
              
              printf("|===============|\n");
              printf("|=== Results ===|\n");
              printf("|===============|\n\n");
              if (errors < 10) 
              {
                         if (strcmp(currentWord, guessWord) == 0)
                         {
                                                 printf("Congratulations! You Won!\n\n");
                         } 
                         
                         else 
                         {
                              printf("You have the wrong word!\n\n");
                         }
                         
              } 
              
              else 
              
              {
                   printf("Better luck next time!\n\n");
              }
              
              printf("\n| Incorrect Letters: %d\n| The correct word is: %s\n| The word you entered is: %s\n", errors, guessWord, currentWord);
              printf("\nType 'end' to exit the game or type 'again' to proceed to the next level:\n");
              
              /* Making sure that the user doesn't enter strange things*/
              
              while ((strcmp(udi, "END") != 0) && (strcmp(udi, "AGAIN") != 0)) 
              
              {
                    if (strcmp(udi, "EMPTY") != 0) 
                    {
                                    printf("\n\nYou are not permitted to type any word other than 'again' or 'end', please try again:\n");
                    }
                    
                    /* Retrieving the udi (udi = user determined input)*/
                    
                    scanf("%s", udi);
                    
                    /* Converting the udi to uppercase */
                    
                    int x;
                    for (x = 0; x < 5; x++) 
                    {
                        udi[x] = toupper(udi[x]);
                    }
              }
              printf("\n|**********************************************|\n");
        }
        
        getch();
        return 0;
    }
     

Share This Page