tags:

views:

81

answers:

1

I'm trying to write a little CLI Hangman game, just to create something in C using my very limited set of language features, making the information so far stick.

I haven't got to strings in the book yet, so I've created a so-called dictionary, which is a multi-dimensional array of characters, each row representing a word, and each column a letter. Therefore, if I want to put today's Dictionary.com word of the day ("prognosticate") in the dictionary, I write something like

dictionary[MAX_WORDS][MAX_CHARS] = {
     ...
     {'p', 'r', 'o', 'g', 'n', 'o', 's', 't', 'i', 'c', 'a', 't', 'e'},
     ...
}

Next, I represent the word to be printed on screen as an array of characters, initially made of underscores.

The way I thought of it, when the player enter a letter, I check to see if it exists within the current word. If it does, I replace the underscores in the word[] array with the actual letter. I consider the word to be guessed when there are no underscores left in the word[] array.

The first thought was to write a function int in_array(char array[], char letter) which would return 0 or 1 based on whether the letter is found in the array. But then I figured out I couldn't pass it dictionary[][] as the first argument. I haven't figured out a solution so far, so I'll have to either use two functions, one for each array, or... rethink the whole idea.

So to sum this up, I need a function to check whether an element exists within either a one-dimensional or multi-dimensional array. Any solutions? Thanks in advance!

A: 

You can pass the entire dictionary via the somewhat janky

void foo(char dict[][MAX_CHARS], int whichWord);

i.e. by telling the compiler exactly how big each row of the 2D array is in this case, and which row you want as a separate parameter. Then you use your usual 2-subscript notation to access elements.

To pass one row of this array is easy too. Let's say you're doing guesses on the second word:

void foo(char[] word, int numChars);

foo(&(dict[1][0]), MAX_CHARS);

which might be little ahead of where you're at, but says "take the address in memory of the first character in the row I want, the second row (remember: 0-based indexing!), and treat it as the start of a one-dimensional array."

The problem you need to solve with this approach is telling the routine how many characters are in your word. If you use MAX_CHARS you have a problem, because presumably not every word is that length, and the characters you don't assign are uninitialized; i.e. you may have a bunch of garbage chars at the end of your row, one of which might even be the character you're looking for! The customary solution for this is to put a 0-valued character, written '\0', at the end of each word, so the function knows to stop when it sees it. You'd need to add this to the end of each word you're initializing, and you might need to bump MAX_CHARS up too to make room for it, because it is a character like everything else in your word.

A far better solution is to turn your dict into a one-dimensional array of strings, i.e.

char* dict[MAX_WORDS];

but that requires you be comfortable with pointers and strings, so come back to it when you're ready.

Owen S.
I have already taken care of the word's length. I've a separate function for this :-) I understand that I need some more knowledge of the language in order to accomplish what I want, so for now, I'll stick with two different pointers, and, as you suggest, I'll come back to it when I'm ready.
XLR3204S
@Owen, you're wrong about uninitialized positions. There is no such thing as a partially-initialized object in C; if it has any initializer at all, then the parts not explicitly initialized are initialized to zero. Thus `{0}` is a universal zero-initializer which can be used for any object.
R..
caf