Skip to content

Instantly share code, notes, and snippets.

@hpcslag
Created November 19, 2017 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hpcslag/b4c61fa776a6e55ccdfdf4a604bfd71d to your computer and use it in GitHub Desktop.
Save hpcslag/b4c61fa776a6e55ccdfdf4a604bfd71d to your computer and use it in GitHub Desktop.
Matlab homework 1
function guessNumber(m,n)
%call guessNumber(m,n) to play the game
%example:
%guessNumber(1,10) to play the game in range 1 to 10
%
r = randperm(n);
ans = r(m);
range_x = m;
range_y = n;
in = -1;
while (in) ~= ans
fprintf('Please Enter Number Between %i to %i\n',range_x,range_y)
in = input('Enter Number: ');
if in == ans
fprintf('You got it.\n')
ask = 0;
while (ask ~= 1)&&(ask ~=2)
ask = input('Do you want to play again? (1. YES, 2. NO): ')
end
if ( ask == 1)
guessNumber(m,n)
end
break;
else
fprintf('Wrong Answer, try again!\n')
if in < ans
range_x = in;
else
range_y = in;
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment