String and display help

조회 수: 2 (최근 30일)
Chris
Chris 2011년 12월 5일
clear all clc
fid = fopen('hangman.txt', 'r'); if fid < 0, error('Cannot open file.'); end CC = textscan(fid, '%s'); C = CC{1}; fclose(fid); index = ceil(rand * numel(C)); word = C{index};
original='Hello World'; Word = word; Word(~isspace(Word)) = '*'
first_guess=char(input('Guess a letter \n','s'));
result1 = ~isempty(findstr(word,first_guess));
selected = first_guess; stars(original == first_guess) = first_guess
OUTPUT Word =
*******
Guess a letter i
stars =
Empty string: 1-by-0
The word is signature, but I don't unerstand how it outputs an empty string. Also, how would I get the letter to replace the asterisk after each guess? Would I end up using a while loop to keep the user guessing until the word is uncovered? This is a lot harder than I thought it would be.
  댓글 수: 3
Chris
Chris 2011년 12월 5일
Hangman text is just a document filled with different words.My program is supposed to be a hangman game.It chooses a random word from that document.The asterisks are there to shield the user from the answer. Although, I did figure some of the problem out, I still do not know how to get the asterisks show up with the letters if the user guesses correctly.
fid = fopen('hangman.txt', 'r');
if fid < 0, error('Cannot open file.');
end
CC = textscan(fid, '%s');
C = CC{1};
fclose(fid);
index = ceil(rand * numel(C));
word = C{index};
Word = word;
Word(~isspace(Word)) = '*'
first_guess=char(input('Guess a letter \n','s'));
result1 = ~isempty(findstr(word,first_guess));
selected = first_guess;
Progress(word == first_guess) = first_guess
OUTPUT
Word =
*********
Guess a letter
e
Progress =
e
Walter Roberson
Walter Roberson 2011년 12월 5일
http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup

댓글을 달려면 로그인하십시오.

채택된 답변

Chandra Kurniawan
Chandra Kurniawan 2011년 12월 5일
Hello,
So long time you have waiting. Here I give you my code.
I have 'Hangman.txt'
signature
chair
calculator
window
picture
browser
phone
book
table
glass
And this is my code :
clear; clc;
fid = fopen('Hangman.txt','r');
if fid < 0, error('Cannot open file'); end
data = textscan(fid,'%s');
data = data{1};
fclose(fid);
index = ceil(rand * numel(data));
word = data{index};
masked = word;
masked(~isspace(masked)) = '#';
complete = 0;
while complete == 0
clc;
fprintf('Word : %s\n',masked);
letter = char(input('Guess a letter : ','s'));
stat = findstr(word,letter);
if ~isempty(stat)
masked(stat) = letter;
end
if isempty(findstr(masked,'#'))
complete = 1;
end
end
clc; fprintf('Word : %s\n',masked);
Just try to run it.
  댓글 수: 3
Chris
Chris 2011년 12월 5일
Wow, thank you! I do have a couple more questions. How would I add a break to that after the user guesses 6 times.
Jan
Jan 2011년 12월 5일
You do not need a CHAR in "char(input('Guess a letter : ','s'))".

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sean de Wolski
Sean de Wolski 2011년 12월 5일
Use ismember to determine if the letter 'is a member' of the word.
%Sample word, disguised word and letter guess
word = 'signature';
word_disguised = repmat('*',size(word));
letter = 'q'; %try various letters
%Engine
idx = ismember(word,letter); %index of members
if ~any(idx)
disp('Fail Lose an arm!');
else
word_disguised(idx) = letter;
end

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by