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!