how to replace '_' with a guessed word?

Hi,
I am making a hangman game for a school project and needed help with how to replace hidden words with the right word if guessed.
This part of the code prints "__" for each letter in the word.
word = char(word);
wordlength = strlength(word); % To find length of word
for i = 1:wordlength
fprintf(" __ ");
end
I have given the user 6 incorrect guesses until they lose the game. I was planning to use a while loop for this step:
incorrect = 0;
left = 7;
while (incorrect < 7)
guess = input("Please guess a letter (lowercase): ", 's');
if guess ~= word(i) % unsure about this
incorrect = incorrect + 1;
left = incorrect - 1
fprintf("That is incorrect, you have %.0f incorrect guesses left.", left)
end
for i = 1:wordlength
if word(i) == guess % unsure about this
= % unsure about this
end
end
end
Can someone please tell me how i could replace __ with the right word if guessed?
Thanks

3 Comments

This could be done very easily using logical indexing. It would let you simplify your code (get rid of some loops).
Hi Stephen,
I tried playing with it and got this, however, something is wrong in between the if and else of the code, the correct letters are not being picked up and replaced. The code is below:
while (incorrect < 7)
guess = input("Please guess a letter (lowercase): ", 's');
if guess == word(i)
fprintf("This is right!");
disp(word(i));
else
incorrect = incorrect + 1;
left = left - 1;
fprintf("Sorry this is incorrect, you have %.0f guesses left.", left);
end
end
Thanks
Try strrep

Sign in to comment.

 Accepted Answer

Adam Danz
Adam Danz on 22 Apr 2020
Here are some tips. Rename the generic variable names I created.
  • Instead of the wordlength loop, s = repmat(' _ ', 1, wordlength); fprintf('%s\n',s); That's 1 underscore per letter.
  • Instead of if guess ~= word(i), use idx = strfind(word, guess); if ~isempty(idx)
  • To insert a correctly guessed letter, dashidx = strfind(s, '_'), s(dashidx(idx)) = guess;
  • You'll also need to add something that stops the game when the correct word was chosen.

3 Comments

Hi,
Thank you for your response, I will try to implement the tips you have provided into my code. However, I will not be able to use all of it because some of the things you have mentioned have not been taught and could be considered plagerism.
The first tip, actually prints:
-
-
-
-
-
-
I think this is because of the \n, i have removed that to make all the dashes come in one line.
Thank you for the help :)
You must be implementing it incorrectly.
wordlength = 5;
s = repmat(' _ ', 1, wordlength);
fprintf('%s\n',s)
% Result:
_ _ _ _ _
Kunal Kumar
Kunal Kumar on 23 Apr 2020
Edited: Kunal Kumar on 26 Apr 2020
Hi,
Thanks, I see the problem. I was using " instead of ' , problem solved now.
Thank you.

Sign in to comment.

More Answers (0)

Categories

Find more on Number games in Help Center and File Exchange

Asked:

on 22 Apr 2020

Edited:

on 26 Apr 2020

Community Treasure Hunt

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

Start Hunting!