MATLAB HANGMAN HELP plz
Afficher commentaires plus anciens
im trying to make a matlab code to produce a hangman game but without the picture of the stickman, just the words. this is my code so far and i need to use if statements and loops etc but i dont know how to make if statements work when the guess is a single letter, hence why i made a vector which every letter so i could try comparing that but the if statements do not work.
words = ["water" "fire" "earth" "wind"];
num = randi([1 4],1,1); %creates random number between 1-4 which corresponds to a word in the vector
num = ceil(num); %ensures only integers are picked
answer = words(num); %assigns the number to the word in the vector
answer = char(answer); %puts the word as a character
letters = ["a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"];
for l = 1:26
letters(l) = l;
end
hiddenword = []; %initialises the hidden word
%this for loop is used to show the hidden word as blank dashes with the
%count of the dashes being the same as the word length.
for k = 1:length(answer)
hiddenword = [hiddenword, '-'];
end
%prompt the user to guess a letter in the hidden word
guess = input('Enter your guess (1 letter): ','s');
wrongguess = [];
correct = 0;
incorrect = 0;
%while guess ==
%Tests to see that a letter is inputted
if guess ~= (letters) % If not
fprintf('Please enter an actual letter: \n') % Politely asks for ACTUAL letter
if guess > letters
fprintf('Please only enter one letter at a time.\n') % Politely ask them to guess only one letter
1 commentaire
Steven Lord
le 13 Avr 2020
Just a couple suggestions on a few pieces of your code:
words = ["water" "fire" "earth" "wind"];
num = randi([1 4],1,1); %creates random number between 1-4 which corresponds to a word in the vector
num = ceil(num); %ensures only integers are picked
answer = words(num); %assigns the number to the word in the vector
To generalize this, in your second line specify numel(words) instead of hard-coding 4.
Also you don't need the third line. randi generates integer values; if it doesn't that's a bug.
Moving down a few lines of your code:
hiddenword = []; %initialises the hidden word
%this for loop is used to show the hidden word as blank dashes with the
%count of the dashes being the same as the word length.
for k = 1:length(answer)
hiddenword = [hiddenword, '-'];
end
This is ineffective, growing hiddenword each iteration through the loop. Instead use repmat to replicate the character '-' a number of times equal to the strlength of the secret word.
Réponses (1)
Geoff Hayes
le 13 Avr 2020
Benjamin - I don't understand this code
letters = ['a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' 'x' 'y' 'z'];
for l = 1:26
letters(l) = l;
end
. Why overwrite the characters in the letters array with numbers? (In fact, it looks like the numbers are treated as ASCII codes and so ltetters becomes an array of special characters. I would remove the for loop altogether. If you want the user to enter a single character (a-z) character guess, then consider the following code where we use ismember to check if the character is part of the array
%while guess ==
guess = '';
while true
%prompt the user to guess a letter in the hidden word
guess = lower(input('Enter your guess (1 letter): ','s'));
if length(guess) > 1
fprintf('Please only enter one letter at a time.\n') % Politely
elseif ~ismember(guess, letters)
fprintf('Please enter an actual letter: \n') % Politely asks for ACTUAL letter
else
% valid character is entered so break out of loop
break;
end
end
Note how the query for the character is put inside the while loop. We continue prompting the user for a single character until they enter it in (at which point we break out of the loop).
Catégories
En savoir plus sur Word games dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!