Skip to main content
1 of 3
Edward
  • 66.6k
  • 4
  • 118
  • 282

This code is not bad at all, and I like the new ASCII art. Here are some ideas on how to improve it further:

Don't confuse the reader

By grouping things together in an enum it's true that it eliminates "magic numbers" but it also tends to mislead the reader into thinking that these items are related. In this case, there are really three independent constants whose only relation is that they are all used in this game. I'd use const for this and use the appropriate types for each, since ALPHABET_SIZE should probably be size_t, HIDDEN_LETTER a char, etc.

Consider reworking the interface

Right now, there is not much separation of concerns. The main program knows everything about every piece of the program. That works, but it might be nicer to separate things a little more. I'd rename config.h to dictionary.h and have a function named get_random_word:

const char *get_random_word() {
    static const char *words[] = {"racing", "magic", "bow", "racecar"};
    static const size_t word_count = sizeof(words)/sizeof(words[0]);
    return words[rng_to(word_count)];
}

That eliminates the need for main to have the macro, and removes the need for it to know anything about the random number generation code.

Consider different data structures

The code relies on a number of related data structures, letters, words and word_to_guess. I would alter their use a bit. First, I'd hide words completely, as shoown above. Right now, the actual underlying alphabet is implicit rather than explicit. It assumes that the alphabet consists of ALPHABET_SIZE contiguous characters beginning from 'a'. This works for English and an ASCII encoding, but not for EBCDIC encodings and not for other languages such as Spanish, French or German. Instead, I'd suggest that there could be an explicit alphabet string associated with the previously mentioned dictionary.h. It could be const. Second, one could employ a bool array of the same length to keep track of which letters of the alphabet had been guessed. This would then be the only data structure that would need to be modified during game play.

Edward
  • 66.6k
  • 4
  • 118
  • 282