Hangman, outputting unknowns while maintaining correct guesses in a string vector

5 views (last 30 days)
I am writing a code for hangman and i am trying to make it that it outputs my selected word in a similar for to ( _ _ _ ). for example if the correct word was graph and the player had guessed "a" than the code would output ( _ _ a _ _) on its seccond iteration.
Here is my code so far:
clc, clear
%word bank and choosing a random word
word_bank = ["aisle","cope","light","bucket","tail","eavesdrop"; "case","action","cruel","charge","crystal","acceptable"; "photography","discuss","marine","dog","cheese","matlab"];
r = randi(numel(word_bank),1); %getting random number to pull random word from word bank
selected_wrd = word_bank{r}; %naming the randomly chosen word
length_of_selected_wrd=length(selected_wrd); %finding length of chosen word to display to player
%naming images
first_life=imread('firstlife.PNG'); %first image
second_life=imread('seccondlife.PNG'); %second image
third_life=imread('thirdlife.PNG'); %third image
fourth_life=imread('fourthlife.PNG'); %fourth image
fifth_life=imread('fifthlife.PNG'); %sixth image
sixth_life=imread('sixthlife.PNG'); %sevanth image
fail_constant=0; %creating fail condition
win_constant=0; %creating win condition
game_constant=0;
picture_condition=0
%win and fail condition are created to determine weather to continue
%looping the code
%game iteration(s) - looped code
while game_constant==0 && win_constant==0 %while loop creates a condition to keep looping the code if a correct letter is chosen
for i=20 %a maximum of 15 guesses are given per stage (max word is length 11) leaving 9 posible attempts to guess the correct word
fprintf("the length of the cosen word is %d\n)", length_of_selected_wrd); % length of chosen word is displayed
guess_x=input('would you like to guess a letter or a word (letter = type(0)) (word = type(1)); '); % a condition is made by the player to determine weather they'll guess a word or letter
if guess_x==0 %guess condition for letter
letter_input=input('type a letter; ','s'); % letter input
elseif guess_x==1 %guess condition for word
word_input=input('type a word; ','s'); %word input
end
correct_word_condition=0; %first condition is made to determine if the guess word is the chosen word
correct_letter_condition=0; %second conition is made to determine if the guess letter is correct
incorecct_letter_condition=0; %third condition is made to determine if the guess letter is incorect
fail_constant=0; %fail condition is made to determine if the player guesses a wrong letter or word
% chosen word conditions (true/false)
while guess_x==1 %creates a condition determining if the guess is a word
if length(word_input)~=length(selected_wrd) %creates a condition rulling out all other words that dont have the same length value as the chosen word
fprintf("incorrect the word is not %s\n :)" , word_input); %incorrec word prompt
guess_x=2;
fail_constant=1; % updates fail conition to true
if fail_constant==1
for i = 1:1
picture_condition = picture_condition + 1;
end
end
elseif word_input==selected_wrd
fprintf("correct, the word is %s\n :) !!!YOU WIN!!!" , selected_wrd); % correct word prompt
correct_word_condition=1; %updates first condition to true
win_constant=1; %updates win condition to true
guess_x=2;
else
fail_constant=1; % updates fail conition to true
if fail_constant==1
for i = 1:1
picture_condition = picture_condition + 1;
end
end
end
end
%chosen letter conditions
if correct_word_condition==0 %only continues if the correct word hasnt been guessed
for i=[1:length(selected_wrd)] %creates an index by the length of the word to show the positions of each letter
if letter_input==selected_wrd(i)==1 %determines and checks true letter position within chosen word
fprintf("correct this position is %s\n", letter_input); %prompts correct letter position
correct_letter_condition=1; % updates seccond position to true
elseif letter_input==selected_wrd(i)==0 %determines and checks false letter position within chosen word
fprintf("incorect this position isn't %s\n", letter_input); % prompts incorrect letter position
incorecct_letter_condition=1; % updates third position to true
end
end
end
if correct_letter_condition~=incorecct_letter_condition % creates condition checking if the letter isnt in the chosen word
fprintf("this word does not contain the letter %s\n", letter_input); %prompts player that the letter isnt in the word
fail_constant=1; %updates fail condition
if fail_constant==1
for i = 1:1
picture_condition = picture_condition + 1;
end
end
end
%picture condition storage
if picture_condition == 1
imshow(first_life)
elseif picture_condition == 2
imshow(second_life)
elseif picture_condition == 3
imshow(third_life)
elseif picture_condition == 4
imshow(fourth_life)
elseif picture_condition == 5
imshow(fifth_life)
elseif picture_condition == 6
imshow(sixth_life)
game_constant = 1
fprintf("YOU DIED");
end
end
end

Answers (1)

DGM
DGM on 23 Aug 2021
If you ask me, this is easier done with character vectors instead of strings.
wordarray = {'potato','tomato','apple','orange'};
thisword = wordarray{3}
thisword = 'apple'
blankword = repmat('_',[1 numel(thisword)]);
fprintf('The word is %s\n',blankword)
The word is _____
guessletter = 'p';
blankword(thisword==guessletter) = guessletter;
fprintf('The word is %s\n',blankword)
The word is _pp__

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!