Need help with coding Hangman in Matlab

5 views (last 30 days)
Amanda Wojcik
Amanda Wojcik on 14 Nov 2017
Answered: Aidan Mackey on 7 Mar 2019
Hi! I am trying to code the game Hangman in Matlab and I am struggling to make the program recognize when a letter has already been guessed as well as display a message informing the user the letter has already been tried. I attempted to so this with a for loop, arrays and by adjusting some of the variables but nothing seems to be working for me. I have attached my code below with comments but I would really appreciate a general explanation so I can understand the best way to do this without messing up what I have coded thus far. Thanks in advance!
%%importing the word list
words=importdata('word_list.txt');
%%Creating the Figure
% making the Hangman figure by creating lines for the figures to graph
% in order to form the gallows axis off %turn the axis off because we
% need a figure, not a graph
%line 1
a=[15,0];b=[0,0];
%line 2
c=[12,12];d=[0,18];
%line 3
e=[5,12];f=[18,18];
%line 4
g=[5,5];h=[12,18];
%plot the figure
figure(1)
line(a,b);line(c,d);line(e,f);line(g,h);
hold on %use Hold on function to hold figure 1
%%generating the word
%generating the spaces and randomly selecting the words
spaces=length(words);
wpos=randi(spaces);
guess=char(words(wpos));
L=length(guess);
i=1:L;
listword(i)='-';
listword;
%%Guessing the Letters
A=0;%The number of attempts
guess;
listword
while A<6 %setting a while loop for the not exceeding the amount of wrong guesses the user has
fprintf('\nThis is attempt %d you get up to 6 incorrect guesses',A)
G=input('\nGuess a letter: ','s')
N=0;
for z=1:L; %creating a for loop to inspect the length of the word to determine if the letter is present
if G==guess(z); %if it is then the letter is displayed
listword(z)=G
N=N+1;
else
N==0;
end
end
if N~=0; %the right letter is guessed and there is no penalty
disp('You guessed the right letter!')
A; %the number of tries remains the same
elseif N==0; %This if statement displays whether the letter guessed is correct. If N equals 0 this displays
disp('Sorry you did not guess the right letter')
if A==0
m=[5,6];n=[3,2];
left_leg=line(m,n,'color','r');
left_leg;
elseif A==1
q=[5,4];r=[3,2];
right_leg=line(q,r,'color','r');
right_leg;
elseif A==2
o=[5,4];p=[8,9];
right_arm=line(o,p, 'color','r');
right_arm;
elseif A==3
k=[5,6];l=[8,9];
left_arm=line(k,l,'color','r');
left_arm;
elseif A==4
i=[5,5];j=[10,3];
torso=line(i,j,'color','r');
torso;
elseif A==5
center=[5,11];
radii=1;
viscircles(center,radii);
disp('***YOU HAVE LOST PLEASE PLAY AGAIN***')
hold on
end
A=A+1;
end
Q=strcmp(listword,guess);
if Q==1;
disp('*****CONGRATULATIONS YOU HAVE WON*****')
A=A+10
end
end %end the while loop
  2 Comments
Guillaume
Guillaume on 14 Nov 2017
To format the code properly, select all the code, then press the {}Code button.
I've done this for you this time.
Amanda Wojcik
Amanda Wojcik on 14 Nov 2017
Thank you! I was having some trouble with that but now I know for next time!

Sign in to comment.

Answers (1)

Aidan Mackey
Aidan Mackey on 7 Mar 2019
Hi!
I wrote and published a hangman game recently and I encountered this problem earlier. What you can do is save the letters that have been guessed correctly in a variable, and before you analyze to see if the guess is in the code, check to see if the letter is found in this variable. If it is, use "continue" to reset the loop for guessing. I did it in my code if you want to check it out. :)

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!