Hey all,
Hope all is going great!
I'm experimenting with a jumble game from a book (Beginning C++ Game Programming) I'm reading and I need alittle help with an explaination of how a particular part of the code is working. I'm going to break it down the best I can as how I
think it's working and I'd appreciate it if someone could steer me in the right direction please.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
enum fields {WORD, HINT, NUM_FIELDS};
const int NUM_WORDS = 5;
const string WORDS[NUM_WORDS][NUM_FIELDS] =
{
{ "wall", "Do you feel you're banging your head against something? "},
{ "glasses", "These might help you see the answer. "},
{ "labored", "Going slowly, is it? "},
{ "persistent", "Keep at it. "},
{ "jumble", "Its what the game is all about. "}
};
srand(time(0));
int choice = (rand() % NUM_WORDS);
string theWord = WORDS[choice][WORD]; // word to guess
string theHint = WORDS[choice][HINT]; // hint for word
| |
The part of that code I need the help on is this part:
1 2 3
|
enum fields {WORD, HINT, NUM_FIELDS};
const int NUM_WORDS = 5;
const string WORDS[NUM_WORDS][NUM_FIELDS]
| |
I just included the rest to help provide you with additional info so you got a better idea of what's going on. Now, from what I know, enumerators start at 0 and increment with each element. So enum fields is indexed as so: {0, 1, 2}. The NUM_WORDS is initialized to 5 which in turn is the number used to determine how many "storage" elements are created in the WORDS array under NUM_WORDS and NUM_FIELDS (which is index 2), creates that many storage elements within the NUM_FIELDS section of WORDS array. Right? Or am I way off base here?
Now, how does the program "know" to store the WORD and HINT elements in the NUM_FIELDS section of the array and store those with the actual words and hints used in the game and not place them within the NUM_WORDS section or does it? For example, is this how it looks if drawn out on paper?
1 2 3
|
Array Storage Element Storage Element
WORDS NUM_WORDS NUM_FIELDS
[0][1][2][3][4] [WORD][HINT]
| |
Sorry if this is simple, but I'm having difficulty wrapping my head around the process here.
Thanks for all the help.