Hangman right word replacement

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.

Respuestas (1)

Mehmed Saad
Mehmed Saad el 30 de Abr. de 2020
Editada: Mehmed Saad el 30 de Abr. de 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 comentarios

Kunal Kumar
Kunal Kumar el 30 de Abr. de 2020
Hi,
Thank you for the response, could you please explain how I'd do this? And yes im working on stopping the code if the correct answer is reached.
Thanks
Mehmed Saad
Mehmed Saad el 30 de Abr. de 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
Kunal Kumar el 30 de Abr. de 2020
Editada: Kunal Kumar el 30 de Abr. de 2020
I will try again.
thanks
Kunal Kumar
Kunal Kumar el 30 de Abr. de 2020
my words are in char format, i dont think they can use the array index.
Mehmed Saad
Mehmed Saad el 30 de Abr. de 2020
they can
x=' _ ';
x(1)
ans =
' '
x(2)
ans =
'_'
x(3)
ans =
' '
Rik
Rik el 30 de Abr. de 2020
Editada: Rik el 30 de Abr. de 2020
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
Kunal Kumar el 30 de Abr. de 2020
Hi Rik,
Thanks for the response, i understand the example but the words in my game are not the same length therefore the code would vary and im not sure how i could do it for different lengths of words in a simple way.
Stephen23
Stephen23 el 30 de Abr. de 2020
Editada: Stephen23 el 30 de Abr. de 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
Kunal Kumar el 30 de Abr. de 2020
Hi Stephen,
Thank you, I guess that is easier to have no spacing, therefore I will use some other symbol instead so spaces do not need to be filled.
I'll chage it to something like this.
hide = repmat('*', 1, wordlength);
thanks
Rik
Rik el 30 de Abr. de 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
Kunal Kumar el 1 de Mayo de 2020
I am not sure how to do what you have said above, because I don't have a lot of experience with Matlab, but i think what I have right now is good enough because it works and i need to finish this soon. If i had more time, i certainly would have tried to do what you have said but due to time limitations i'll stick to this.
Thanks for the help.

Iniciar sesión para comentar.

Categorías

Más información sobre Word games en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 30 de Abr. de 2020

Comentada:

el 1 de Mayo de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by