Thread: Help with hangman game: how to approach random selecting word from file

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    3

    Help with hangman game: how to approach random selecting word from file

    Hello

    A total newbie to C programming. Trying to understand where to start with this code.

    First part of the assignment is to select a word at random from the words stored in a textfile.

    I'm really struggling with how to approach this. I've been reading about ways to do so.

    So I have pseudocode for finding longest word in a file and also counting words in the file. I'm trying to figure how to pass this information into an array from the file so that then I can display the information.

    I've read about ways of allocating memory to the array of strings to read the file into. Or using pointers or randomly picking a number that would cover the number of words in the file. I haven't used the malloc function really though have experience with pointers and arrays.

    What I'm wondering is how do I get the contents of the file allocated to an array in the program, to then display it to the screen to be guessed.

    Would welcome thoughts.

  2. #2
    Registered User
    Join Date
    Feb 2017
    Posts
    2
    You don't have problems with reading the file? What I would do is something like this: - Read the file - Count the words in the file (= word_count) - Find the longest word and it's size (= max_size) - Store this in an array of strings, which in turn are arrays of char. And for that last part:
    Code:
     char **word_list = (char**)malloc(word_count*sizeof(char*))
    That initializes an array with length word_count. Each cell of that array is prepared to store a char array:
    Code:
     for(i = 0; i < word_count; i++) {     word_list[i] = (char*)malloc(max_size*sizeof(char));  }
    Each cell is now storing an array with length max_size. Lets say the first word is "Hello". You're now supposed to get something like this:
    Code:
     word_list[0][0] = "H";  word_list[0][1] = "e";  word_list[0][2] = "l";  word_list[0][3] = "l";  word_list[0][4] = "o";
    To select a random word:
    Code:
     int randKey = rand()%word_count;
    This generates a random integer between 0 and word_count-1. And to print the random word:
    Code:
     for(i = 0; i < max_size; i++) {     printf("%c", word_list[randKey][i];  }
    I may have the char identifier wrong. Or to skip the null cells:
    Code:
     i = 0;  do {     printf("%c", word_list[randKey][i];    i++; }while(word_list[randKey][i] != NULL);

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    3
    1
    Code:
    char **word_list = (char**)malloc(word_count*sizeof(char*))
    Can I ask - so I count the words in the file and also figure out the longest word.

    This function essentially means that I pass the result of say word list to the memory allocation? So that the results of the word list can be stored in memory?
    And most of all, many thanks for your reply.
    Last edited by adidas; 02-25-2017 at 06:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error when trying to read a random word from a file
    By MuckingFedic in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2015, 07:12 PM
  2. need a random word from a txt file
    By abbylennon in forum C# Programming
    Replies: 18
    Last Post: 04-29-2013, 08:47 AM
  3. Replies: 28
    Last Post: 10-23-2011, 07:17 PM
  4. not selecting random number again
    By diego in forum C++ Programming
    Replies: 5
    Last Post: 05-26-2010, 03:45 PM
  5. Hangman Game - Need Help!!
    By krobort in forum C++ Programming
    Replies: 3
    Last Post: 10-12-2006, 04:15 PM

Tags for this Thread