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;
wrong=0;
while complete == 0
clc;
fprintf('Word : %s\n',masked);
letter = char(input('Guess a letter : ','s'));
stat = findstr(word,letter);
fprintf('letters used so far',letter);
if ~isempty(stat)
masked(stat) = letter;
elseif ~isequal(word,letter)
wrong=wrong+1;
end
if isempty(findstr(masked,'*'))
complete = 1;
fail=0;
end
if wrong==6
complete=1;
fail=1;
end
end
if fail==0
clc; fprintf('You win, the word is : %s\n',masked);
elseif fail==1
disp('You lose')
end
How would I display all the letters the to the user, so they can see what letters they already guessed? Like if I guess an E, and there is no E, I want the program to show that I guessed E. And continue to do that for every guess, and list all the letters I've guessed.
And how do I get my words to be not be case sensitive? :[

 채택된 답변

Chandra Kurniawan
Chandra Kurniawan 2011년 12월 6일

1 개 추천

Hello,
I modify my 'hangman.txt' at first line :
Signature
chair
calculator
window
picture
browser
phone
book
table
glass
Note that 'signature' becomes 'Signature'
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 = lower(data{index});
masked = word;
masked(~isspace(masked)) = '*';
complete = 0;
wrong=0;
letter_guessed = '';
while complete == 0
clc;
%fprintf('Word : %s\n',masked);
fprintf('Letter guessed : %s\n',letter_guessed);
letter = char(input('Guess a letter : ','s'));
stat = findstr(word,letter);
fprintf('letters used so far\n',letter);
if ~isempty(stat)
masked(stat) = letter;
letter_guessed = strcat(letter_guessed,letter);
elseif ~isequal(word,letter)
wrong=wrong+1;
end
if isempty(findstr(masked,'*'))
complete = 1;
fail=0;
end
if wrong==6
complete=1;
fail=1;
end
end
if fail==0
clc; fprintf('You win, the word is : %s\n',masked);
elseif fail==1
disp('You lose')
end

댓글 수: 7

Chris
Chris 2011년 12월 6일
Thank you! Would you have any idea how to solve my first question?
Chandra Kurniawan
Chandra Kurniawan 2011년 12월 6일
Do you mean 'list all the letters I've guessed'??
Walter Roberson
Walter Roberson 2011년 12월 6일
Chandra, Chris is doing a school assignment, and you are giving him complete answers for that assignment :(
Chris
Chris 2011년 12월 6일
I've put some things together, but Chandra has helped me tremendously. And yes I mean that.
Chandra Kurniawan
Chandra Kurniawan 2011년 12월 6일
Oh sorry before.
It's will be better if I give you hint step-by-step :)
So, dont you see line : letter_guessed = '';
I store every words that has been guessed to variable 'letter_guessed' at line :
letter_guessed = strcat(letter_guessed,letter);
Then I decide not to display the masked word at line :
%fprintf('Word : %s\n',masked);
Have you run my code?
Chris
Chris 2011년 12월 6일
Yes, I've used your code ;].
%Clears all data from previous program
clear
clc
%Opens text file with words
fid = fopen('hangman.txt','r');
%If file name isn't correctly spelt, file fails to open
if fid < 0, error('Cannot open file'); end
data = textscan(fid,'%s');
%Initialize
data = data{1};
%Closes text file
fclose(fid);
%Chooses a random word from text file
index = ceil(rand * numel(data));
%Makes word not case sensitive
word = lower(data{index});
masked = word;
%Replaces characters with astferisks
masked(~isspace(masked)) = '*';
complete = 0;
wrong=0;
letter_guessed = '';
while complete == 0
clc;
fprintf('Word : %s\n',masked);
letter = char(input('Guess a letter : ','s'));
stat = findstr(word,letter)
fprintf('Word : %s\n',masked);
if ~isempty(stat)
masked(stat) = letter;
letter_guessed = strcat(letter_guessed,letter);
elseif ~isequal(word,letter)
wrong=wrong+1;
end
if isempty(findstr(masked,'*'))
complete = 1;
fail=0;
end
if wrong==6
complete=1;
fail=1;
end
end
if fail==0
clc; fprintf('You win, the word is : %s\n',masked);
elseif fail==1
disp('You lose')
end
Try running that, yours left the asterisks out. The letters that were guessed do not appear still.
Chandra Kurniawan
Chandra Kurniawan 2011년 12월 6일
Good job!
Now do your next problem :
http://www.mathworks.com/matlabcentral/answers/23172-splitting-into-function-files

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

추가 답변 (0개)

카테고리

도움말 센터File 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