Show a letter in its right locations

7 views (last 30 days)
Hello everybody! I'm programming something with matlab and I need your help! For example: Tokyo has 2 "o"! I know how to hide the word tokyo but if the user enters "o" I don't know how to show the 2 "o"! An example of execution: --> * * * * * --> Enter a letter: 'o' --> * o * * o

Accepted Answer

Image Analyst
Image Analyst on 4 Jan 2014
Here's a simple hangman game I banged out in about 3 minutes. Adapt as needed.
% Simple hangman game.
correct = 'tokyo';
revealed = '*****';
defaultValue = '*';
titleBar = 'Enter a Letter';
userPrompt = 'Enter the Letter';
while ~strcmp(revealed, correct)
% Ask user for a letter.
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
% Bail out if they clicked Cancel.
if isempty(caUserInput),return,end;
% Convert from cell array to character.
usersLetter = char(caUserInput);
% Find locations that match the user's letter.
matches = correct == usersLetter;
% Change those locations from * to the actual letter.
revealed(matches) = usersLetter
% Change prompt to update revealed.
userPrompt = sprintf('%s\nEnter another Letter', revealed);
end
msgbox('You finished!');
  3 Comments
Image Analyst
Image Analyst on 5 Jan 2014
I already did help you. Probably too much since I didn't know it was a class assignment because you didn't tag it as homework. I'll add that tag for you. Please read this: http://www.mathworks.com/matlabcentral/answers/8626-how-do-i-get-help-on-homework-questions-on-matlab-answers
Sidson Aidson
Sidson Aidson on 5 Jan 2014
I finally found a solution of my problem in your first answer! "matches" is what I needed!!!!

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots 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!