C. Program Description for Developers

List of Relevant Variables:

  • x ~determines whether the game will be run or quit
  • e ~list of words for easy difficulty
  • m ~list of words for medium difficulty
  • h ~list of words for hard difficulty
  • d ~difficulty the user chooses
  • w ~the word that will be guesses
  • W ~the same word but split into cells and uppercase
  • V ~W in the correct orientation
  • a ~V with all characters in V are now underscores
  • pic ~the appropriate figure loaded in
  • p ~pic showed to the user docked into the window
  • g ~the users typed in guess
  • Guess ~the users guess made uppercase if not already
  • B ~vector of the user’s wrong guesses

List of Relevant Commands:

  • input
  • fprintf
  • (randi(numel( )))
  • split
  • regexprep
  • num2cell
  • figure
  • imread
  • imshow
  • any(strcmp(
  • disp(

To start the code, the first two lines clear the command window and workspace with the commands “clear” and “clc”. The user is welcomed, and the game begins. The user is prompted to enter how many players there are, depending on whether the user inputs a 0, 1, or any other number determines the course of action. If a 0 is entered the game ends and a fprintf message thanks the user for playing.

If a 1 is entered, the user is directed to the single player route.

Single Player-

The user is prompted to select a difficulty: easy, medium, or hard. The selection of difficulty determines what word is randomly chosen from the respective list for that difficulty. The lists are words, with each letter separated with a space. The easy difficulty is being used in this example, however the process is the same for each difficulty. A random word from the list is pulled with the command “w = e(randi(numel(e)));”. The word is then split at the whitespaces with the command “W = split(w);”.  The variable W is then transposed to the vector V in order to align the letters in a traditional left to right manner. W is then turned into a character array (important during guessing phase). Next each character in the vector V is re-expressed to show as an underscore, the command used to do this is “a = regexprep(V,’.’,’_’)”.

If any other number is entered, the user is directed to the multiplayer setup.

Multiplayer-

To make the game multiplayer compatible, the users are prompted to input a word to be guessed. That word is then converted to uppercase and then turned into a cell array with the command “V = num2cell(W);”. This is to make the array compatible with the “a = regexprep(V,’.’,’_’)” which replaces all characters within the array to underscores.

Next the pole is loaded in and docked into the window with these three lines of code: figure(‘WindowStyle’,’docked’)

pic=imread(‘pole.png’);

p = imshow(pic);

Now the code transitions into the operation that allows the user to guess. While there are any underscores remaining in the word, the user is moved through the following lines:

g = input(‘Guess a letter: ‘ , ‘s’);

Guess = upper(g);

B{1,1} = [‘Wrong Letters’];

for k = 1:length(V)

if Guess == W(k)

a(k) = V(k);

end

end

The variable g is assigned to be the guess that the user inputs. Its is then converted into uppercase. The vector B is created with the first cell assigned as ‘Wrong Letters’, this is the list of letters that will pop up when a guess is wrong. For each letter of the word, there is an iteration in which if the Guess is equal to any letter in the vector W of that iteration, the cell in vector “a” of that iteration is then replaced with the letter which is signaled by the vector V of that iteration. If the Guess does not equal to any letter in W, then that Guess is added to the vector B. So when the length of B increases the figure is replaced with one that features the next body part.

if Guess ~= W

B{end+1} = Guess;

if length(B) == 2

pic2 = imread(‘head.png’);

p = imshow(pic2);

end

if length(B) == 3

pic3 = imread(‘torso.png’);

p = imshow(pic3);

end

if length(B) == 4

pic4 = imread(‘rightleg.png’);

p = imshow(pic4);

end

if length(B) == 5

pic5 = imread(‘leftleg.png’);

p = imshow(pic5);

end

if length(B) == 6

pic6 = imread(‘rightarm.png’);

p = imshow(pic6);

end

if length(B) == 7

pic7 = imread(‘leftarm.png’);

p = imshow(pic7);

end

end

Next are the breaks to the while loops. First if the vector B reaches the length of 7 then the user is presented with the message that they lost and the word is then revealed. The loop is then broke.

if length(B) == 7

fprintf(‘\nYou lose!’)

Word = V

Break

end

The user wins and the while loop breaks when all the letters are converted from underscores to the letters that belong in each underscores spot, so there is no need to include a “break” when this happens.

Next the vector B is only displayed when there has been a wrong letter guessed. Then the vector “a” is displayed after each guess to update the guesser on how many letters still need to be guessed for the word to be complete.

if length(B) >= 2

disp(B)

end

disp(a)

Finally, once there are no underscores left the user is presented with the message “Congratulations! You guessed the word!”. The user then must indicate how many players there are, or whether they would like to quit or not, and the program loops so long as the user does not quit.