Guest User

Basic Number Guesser

a guest
May 20th, 2016
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.24 KB | None | 0 0
  1. disp('I would like to try to guess a number you are thinking of in three questions, 1 through ten.');
  2. disp('Please answer using yes or no.');
  3. disp('[Press any key to continue]');
  4. pause
  5.  
  6. yes = 1;
  7. no = 0;
  8. prompt = 'Is the number divisible by 3? ';
  9. x(1) = input(prompt);
  10. if x(1) == 1;
  11.     n1 = [3 6 9];
  12. elseif x(1) == 0;
  13.     n1 = [1 2 4 5 7 8 10];
  14. end
  15. disp('hmmm...')
  16. disp('[Press any key to continue]');
  17. pause
  18.  
  19. prompt = 'Is the number even? ';
  20. x(2) = input(prompt);
  21. if x(2) == 1;
  22.     n2 = [2 4 6 8 10];
  23. elseif x(2) == 0;
  24.     n2 = [1 3 5 7 9];
  25. end
  26. disp('erm...')
  27. disp('[Press any key to continue]');
  28. pause
  29.  
  30. prompt = 'Is the number greater than 5? ';
  31. x(3) = input(prompt);
  32. if x(3) == 1;
  33.     n3 = [6 7 8 9 10];
  34. elseif x(3) == 0;
  35.     n3 = [1 2 3 4 5];
  36. end
  37. disp('ummm...')
  38. disp('[Press any key to continue]');
  39. pause
  40.  
  41. m1 = ismember(n1,n2);
  42. M1 = m1.*n1;
  43. m2 = ismember(M1,n3);
  44. M2 = m2.*M1;
  45. N = M2(M2~=0);
  46. TF = isempty(N);
  47.  
  48. if TF == 1
  49.     disp(' Im pretty sure you are messing with me...')
  50. else
  51.     disp(N(1));
  52.     prompt = 'Was that your number? ';
  53.     Z = input(prompt);
  54.     if Z == 1;
  55.     disp('Ha! I win!');
  56.     elseif Z == 0;
  57.     disp('Ok, fine, then your number is:')
  58.     disp(N(2))
  59.     end
  60. end
  61.  
  62. pause
  63.  
  64. clc
Advertisement
Add Comment
Please, Sign In to add comment