Solving Word Ladder Game - Step 3

◀ Solving Word Ladder Game - Step 2▶ Solving Word Ladder Game - Step 4
Amazon Let’s apply the 3rd step of the Four-Step Programming Model to solve the word ladder game!

Four Step Programming Model: Step 3
Interrelation: Elements of both bitmap and words can be accessed via [], so make sure their indices do not go out of their bounds. Also, pay attention to the condition of a while loop.

State: Let’s focus on the critical variables in this program, including bitmap and words. Obviously, bitmap is a critical variable because it keeps track of all valid words. Once it is assigned correct values, they should never be changed. Its use solely lies in checking if a given word is valid. Therefore, we need to make sure we do nothing to alter its contents.


Let’s look at words, the solution vector. It is supposed to store all words in the word ladder, including its beginning and destination words. Once a randomly selected beginning word does not work, it should be cleared of its contents for the next attempt.

Scope: bitmap definitely merits a global scope because several functions need it. On the other hand, ladder() needs access to words, so we can make words global, too.

If the chosen beginning word does not lead to a valid word ladder, we should randomly select another word.


But there is still a chance of selecting that word, so we should remove it from a vector that initially holds all words in the file, then randomly select a word in that vector as the beginning word. Where is this vector? It is not even in our skeleton! Therefore, we need one more string vector which assists us in making second attempts.

On the other hand, inside ladder(), when we know that an intermediate word in the word ladder leads nowhere, we need to remember to remove that word from words. Without doing these, words is likely to contain many words, which would add to the fun of debugging later.


Let's look at the next step!
◀ Solving Word Ladder Game - Step 2▶ Solving Word Ladder Game - Step 4

fShare
Questions? Let me know!