Splitting into function files
显示 更早的评论
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);
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
My Professor wants me to split this into multiple function files, I'm not entirely sure how he wants me to accomplish that.
采纳的回答
更多回答(3 个)
Chandra Kurniawan
2011-12-6
Hello, Chris
Was your professor wants to divide or split this code into multiple function files?
I have tried with my own code,
and I can divide it into 5 function files.
1] load_data.m => function data = load_data(filename)
2] select_word.m => function [word masked] = select_word(data)
3] update.m => function [masked letter_guessed wrong]= update(word, masked, wrong, letter_guessed, letter)
4] check_win => function [complete fail] = check_win(masked, wrong)
5] display_message => function display_message(fail, masked)
And I have main file that named 'main.m'. So, totally I have 6 m-files.
3 个评论
Chris
2011-12-6
Matt Tearle
2011-12-6
"Open"? Do you mean how to use/call the functions from the main file? Just like you would with any MATLAB function. So it looks like Chandra has moved lines 3 - 7 into the load_data function. This function takes a filename as input and returns data as an output. Hence, in the main code, lines 3 - 7 would be replaced with a single call to load_data:
data = load_data('hangman.txt');
Chandra Kurniawan
2011-12-6
@Matt : You are right :)
Chandra Kurniawan
2011-12-6
Hello,
After modification, I get my code becomes shorter :
clear, clc;
filename = 'Hangman.txt';
data = load_data(filename);
[word masked] = select_word(data);
complete = 0; wrong = 0;
letter_guessed = '';
while complete == 0
clc; fprintf('%d letters word\n', length(word));
fprintf('Letter guessed : %s\n',letter_guessed);
letter = char(input('Guess a letter : ','s'));
[masked letter_guessed wrong]= update(word, masked, wrong, letter_guessed, letter);
[complete fail] = check_win(masked, wrong);
end
display_message(fail, masked);
But you need to build 5 functions.
Chandra Kurniawan
2011-12-6
I will give you one more.
function [complete fail] = check_win(masked, wrong)
if isempty(findstr(masked,'*'))
complete = 1; fail = 0;
elseif wrong >= 6
complete = 1; fail = 1;
else
complete = 0; fail = 0;
end
Now, you have 3 function files.
See my shorten main file.
So, now you can replace
if isempty(findstr(masked,'*'))
complete = 1;
fail=0;
end
if wrong==6
complete=1;
fail=1;
end
with :
[complete fail] = check_win(masked, wrong);
类别
在 帮助中心 和 File Exchange 中查找有关 Variables 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!