Splitting into function files

2 views (last 30 days)
Chris
Chris on 6 Dec 2011
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.

Accepted Answer

Chandra Kurniawan
Chandra Kurniawan on 6 Dec 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 Comment
Chandra Kurniawan
Chandra Kurniawan on 6 Dec 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 :)

Sign in to comment.

More Answers (3)

Chandra Kurniawan
Chandra Kurniawan on 6 Dec 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 Comments
Matt Tearle
Matt Tearle on 6 Dec 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
Chandra Kurniawan on 6 Dec 2011
@Matt : You are right :)

Sign in to comment.


Chandra Kurniawan
Chandra Kurniawan on 6 Dec 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.
  1 Comment
Chris
Chris on 6 Dec 2011
That is my entire code so far. I called in 2 functions. I still do not understand how you called in the while loop and the if and else if statements after the while loop. -_-
%Clears all data from previous program
clear
clc
%Calls function (opens text file)
filename = 'Hangman.txt';
data = load_data(filename);
%Calls the function (random)
[word masked] = select_word(data);
complete = 0;
wrong=0;
letter_guessed = '';
while complete == 0
fprintf('Word : %s\n',masked);
letter = char(input('Guess a letter : ','s'));
stat = findstr(word,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
fprintf('You win, the word is : %s\n',word);
elseif fail==1
fprintf('You lose, the word was : %s\n',word);
end
ORIGINAL CODE
%Clears all data from previous program
clear
clc
%Calls function
filename = 'Hangman.txt';
data = load_data(filename);
%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
fprintf('Word : %s\n',masked);
letter = char(input('Guess a letter : ','s'));
stat = findstr(word,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
fprintf('You win, the word is : %s\n',word);
elseif fail==1
fprintf('You lose, the word was : %s\n',word);
end

Sign in to comment.


Chandra Kurniawan
Chandra Kurniawan on 6 Dec 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);

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!