Guess the number/With feedback (player): Difference between revisions
Content added Content deleted
(Added MATLAB example) |
|||
Line 1,134: | Line 1,134: | ||
end |
end |
||
</lang> |
</lang> |
||
=={{header|MATLAB}}== |
|||
<lang MATLAB>function GuessNumberFeedbackPlayer |
|||
lowVal = input('Lower limit: '); |
|||
highVal = input('Upper limit: '); |
|||
fprintf('Think of your number. Press Enter when ready.\n') |
|||
pause |
|||
nGuesses = 1; |
|||
done = false; |
|||
while ~done |
|||
guess = floor(0.5*(lowVal+highVal)); |
|||
score = input(sprintf( ... |
|||
'Is %d too high (H), too low (L), or equal (E)? ', guess), 's'); |
|||
if any(strcmpi(score, {'h' 'hi' 'high' 'too high'})) |
|||
highVal = guess-1; |
|||
nGuesses = nGuesses+1; |
|||
elseif any(strcmpi(score, {'l' 'lo' 'low' 'too low'})) |
|||
lowVal = guess+1; |
|||
nGuesses = nGuesses+1; |
|||
elseif any(strcmpi(score, {'e' 'eq' 'equal' 'right' 'correct'})) |
|||
fprintf('Yay! I win in %d guesses.\n', nGuesses) |
|||
done = true; |
|||
else |
|||
fprintf('Unclear response. Try again.\n') |
|||
end |
|||
if highVal < lowVal |
|||
fprintf('Incorrect scoring. No further guesses.\n') |
|||
done = true; |
|||
end |
|||
end |
|||
end</lang> |
|||
{{out}} |
|||
<pre>Lower limit: 0 |
|||
Upper limit: 50 |
|||
Think of your number. Press Enter when ready. |
|||
Is 25 too high (H), too low (L), or equal (E)? L |
|||
Is 38 too high (H), too low (L), or equal (E)? H |
|||
Is 31 too high (H), too low (L), or equal (E)? H |
|||
Is 28 too high (H), too low (L), or equal (E)? L |
|||
Is 29 too high (H), too low (L), or equal (E)? E |
|||
Yay! I win in 5 guesses.</pre> |
|||
<pre>Lower limit: 0 |
|||
Upper limit: 10 |
|||
Think of your number. Press Enter when ready. |
|||
Is 5 too high (H), too low (L), or equal (E)? L |
|||
Is 8 too high (H), too low (L), or equal (E)? hello |
|||
Unclear response. Try again. |
|||
Is 8 too high (H), too low (L), or equal (E)? H |
|||
Is 6 too high (H), too low (L), or equal (E)? L |
|||
Is 7 too high (H), too low (L), or equal (E)? H |
|||
Incorrect scoring. No further guesses.</pre> |
|||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |