Splitting into function files
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Akzeptierte Antwort
Chandra Kurniawan
am 6 Dez. 2011
Just call the function's name.
I will give U first sample code :)
Replace code below :
fid = fopen('hangman.txt','r');
if fid < 0, error('Cannot open file'); end
data = textscan(fid,'%s');
data = data{1};
fclose(fid);
with :
filename = 'Hangman.txt';
data = load_data(filename);
Now, create a m-file then type this code :
function data = load_data(filename)
fid = fopen(filename,'r');
if fid < 0,
error('Cannot open file');
end
data = textscan(fid,'%s');
data = data{1};
fclose(fid);
Save the code above with filename : 'load_data.m'.
And try to run your code modified now.
1 Kommentar
Chandra Kurniawan
am 6 Dez. 2011
@Chris : Hi,
Would you finish the rest with your own ideas? :)
You said that your professor want to divide it into multiple files.
I have suggested you that your code can be divide into 5 function file as my version.
So, I hope my suggestion would helps you :)
Weitere Antworten (3)
Chandra Kurniawan
am 6 Dez. 2011
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 Kommentare
Matt Tearle
am 6 Dez. 2011
"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
am 6 Dez. 2011
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
am 6 Dez. 2011
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);
0 Kommentare
Siehe auch
Kategorien
Mehr zu Variables finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!