F. Discussion

Preliminary tests of the program were successful. These early versions of the game did not include randomizing a word or “success” and “failure” messages. However, once this more complicated code was needed to complete the game, difficulties arose. Most of these problems were solved using a trial-and-error method and continually checking error messages in MATLAB.

Some obstacles in coding the game were selecting a random word from an initialized word list, working with different word lengths, the presence of double letters, and checking both the number of guesses remaining and if the word was completed. A word list was initialized as a vector called wordList, where each index was a different word. Research was done to find functions to select a random index and store the word properly as a variable. This was done using rand(), length(), and ceil(). In the code, a random decimal from 0 to 1 was first chosen using rand(1,1). This decimal was then multiplied by the length of the word list. Because this result was not an integer, ceil() was used to round the result up to the closest integer. This finalized the result to the value of a random index in wordList.

To address the issue of a letter appearing multiple times in a word, a for loop was written to iterate through each individual letter of the word, and each time determine if it was a match. Lastly, a central matter was deciding how to continually check both the status of remaining guesses and completion of the word. A single while loop with two parameters — an incomplete word and at least 1 guess still remaining — contains all of the code relating to having the player guess a letter.

The MATLAB code progresses in three sections: initialization of variables, a while loop prompting the player to guess letters and updating the word’s completion status, and displaying a final “success” or “failure” message. A more detailed progression of the code can be found under “Program Description for Developers.” In the first section, a random word from the vector wordList is selected, the status of the word’s completion is set to zero, and the players “health” (total number of guesses left) is set to six.

The second section is a while loop that runs when the word is not yet completed and health is not zero. In this while loop, the word is first displayed to the player with only correctly guessed letters shown. Then, the player is prompted to guess another letter, after which the code determines where that letter appears in the word. Health is deducted if the letter did not appear, and the loop starts over if the parameters still hold. If the parameters of an incomplete word and health greater than zero do not hold, the third section of code runs. This displays whether or not the game was won, and asks the player if they would like to play again.