Hangman right word replacement
Show older comments
Hi,
I am making a simple hangman game for a project. I am currently stuck on replacing a "_" and printing the correct letter in place. Can someone please help, parts of my code are below:
wordlength = strlength(word);
hide = repmat(' _ ', 1, wordlength);
fprintf('%s\n', hide);
fprintf("\nThere are %d letters in this word.\n", wordlength);
incorrect = 0;
left = 7;
while (incorrect < 7)
letter = input("Please guess a letter (1 lowercase letter): ", 's');
consists = ismember(word, letter);
if ~any(consists)
incorrect = incorrect + 1;
left = left - 1;
fprintf("\nSorry this is incorrect, you have %.0f guesses left.\n", left);
else
fprintf("This is right!");
hide(consists) = letter
end
end
if (left == 0)
fprintf("\nGAME OVER! YOU LOSE\n");
end
Example:
Lets say there are 6 letters in the word, and the word is 'wizard'. It will print " _ _ _ _ _ _ ".
It then asks for a letter, lets say i type 'w'. It says "This is right!" and shows: hide = 'w_ _ _ _ _ ' . However, It should replace the first '_'.
when it asks for another letter, lets say i type in 'i'. It says "This is right!" and shows: hide = 'wi _ _ _ _ _ ' .
then the third time i type 'd', it says "This is right!" and shows: hide = 'wi _d _ _ _ _ ' .
Thanks.
Answers (1)
Mehmed Saad
on 30 Apr 2020
Edited: Mehmed Saad
on 30 Apr 2020
because
' _ '
are three characters and not 1. first index is space, 2nd is dash and 3rd is space again
what you need is replace the dashes and not the space
for example
if i have
' _ _ _ _ '
The first dash occurs at 2nd index and 2nd dash occurs at 5th index and goes on with 3 index difference
Currently you ve logical index (consists) change them to array index (using find)
For example
A = [1 0 0 1]
B = find(A)
B =
1 4
Or you can replace ismember with strfind
Once you have the index multiply them with three to access every third index and you ve to subtract 1 from them because dash is at 2,5,7,..
PS: Please add a condition for correct answer in your code i.e. if a person guess all the letters stop the game
11 Comments
Kunal Kumar
on 30 Apr 2020
Mehmed Saad
on 30 Apr 2020
I told you all the things you ve to do to make it work. Read the answer again and try to implement it in your code.
Kunal Kumar
on 30 Apr 2020
Edited: Kunal Kumar
on 30 Apr 2020
Kunal Kumar
on 30 Apr 2020
Mehmed Saad
on 30 Apr 2020
they can
x=' _ ';
x(1)
ans =
' '
x(2)
ans =
'_'
x(3)
ans =
' '
You can use an array index just fine:
txt='foo';
txt(1)
Because you insert spaces in the dashed version those probably don't line up, but that is a simple bit of math: what function will map [1 2 3] to [2 4 6]? (assuming 1 space between each letter in the dashed version)
%observe the results:
txt='foo';
clc
guess=' _ _ _ '
guess([2 4 6])=txt([1 2 3])
Kunal Kumar
on 30 Apr 2020
"...how i could do it for different lengths of words in a simple way."
The simplest way would be to NOT store the space characters. Just create a character vector of underlines with exactly the same size as the word has, then you can use logical indexing to trivially update the character vector in the loop. The space characters you can easily add when displaying the character vector (either fprintf or sprintf makes this easy with ' %c ' format).
Basically If you separate the data storage from the data displaying your code will be much simpler.
Currently you are storing superfluous formatting information in hide, but this just complicates your code and serves no real purpose as 1) the space characters never change 2) they are trivially added when displaying.
Kunal Kumar
on 30 Apr 2020
Rik
on 30 Apr 2020
You don't need to change that aspect, just how you store your data. "If you separate the data storage from the data displaying your code will be much simpler." Take that advice to heart.
Kunal Kumar
on 1 May 2020
Categories
Find more on Word games 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!