Guess the number/With feedback (player): Difference between revisions
Content added Content deleted
m (Fix header.) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 173: | Line 173: | ||
Return, "Too High" |
Return, "Too High" |
||
}</lang> |
}</lang> |
||
=={{header|BBC BASIC}}== |
|||
<lang bbcbasic> min% = 1 |
|||
max% = 100 |
|||
PRINT "Think of a number between "; min% " and " ;max% |
|||
PRINT "I will try to guess your number." |
|||
REPEAT |
|||
guess% = (min% + max%) DIV 2 |
|||
PRINT "My guess is " ; guess% |
|||
INPUT "Is it higher than, lower than or equal to your number", answer$ |
|||
CASE LEFT$(answer$,1) OF |
|||
WHEN "L","l": min% = guess% + 1 |
|||
WHEN "H","h": max% = guess% - 1 |
|||
WHEN "E","e": EXIT REPEAT |
|||
OTHERWISE: PRINT "Sorry, I didn't understand your answer." |
|||
ENDCASE |
|||
UNTIL FALSE |
|||
PRINT "Goodbye." |
|||
END</lang> |
|||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
Line 238: | Line 219: | ||
Guesses: 7 |
Guesses: 7 |
||
</pre> |
</pre> |
||
=={{header|BBC BASIC}}== |
|||
<lang bbcbasic> min% = 1 |
|||
max% = 100 |
|||
PRINT "Think of a number between "; min% " and " ;max% |
|||
PRINT "I will try to guess your number." |
|||
REPEAT |
|||
guess% = (min% + max%) DIV 2 |
|||
PRINT "My guess is " ; guess% |
|||
INPUT "Is it higher than, lower than or equal to your number", answer$ |
|||
CASE LEFT$(answer$,1) OF |
|||
WHEN "L","l": min% = guess% + 1 |
|||
WHEN "H","h": max% = guess% - 1 |
|||
WHEN "E","e": EXIT REPEAT |
|||
OTHERWISE: PRINT "Sorry, I didn't understand your answer." |
|||
ENDCASE |
|||
UNTIL FALSE |
|||
PRINT "Goodbye." |
|||
END</lang> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
Line 336: | Line 336: | ||
else |
else |
||
printf("Your number is %d.\n", (int)(result - ZERO)); |
printf("Your number is %d.\n", (int)(result - ZERO)); |
||
return 0; |
|||
}</lang> |
|||
=={{header|C++}}== |
|||
A clever solution that takes advantage of C++'s built-in binary search function <code>lower_bound()</code>. Instead of searching a slice of a container, we search a range of numbers by implementing a specially-designed custom iterator. |
|||
{{trans|Go}} |
|||
<lang cpp>#include <iostream> |
|||
#include <algorithm> |
|||
#include <string> |
|||
#include <iterator> |
|||
struct GuessNumberIterator : std::iterator<std::random_access_iterator_tag, int> { |
|||
int i; |
|||
GuessNumberIterator() { } |
|||
GuessNumberIterator(int _i) : i(_i) { } |
|||
GuessNumberIterator& operator++() { ++i; return *this; } |
|||
GuessNumberIterator operator++(int) { |
|||
GuessNumberIterator tmp = *this; ++(*this); return tmp; } |
|||
bool operator==(const GuessNumberIterator& y) { return i == y.i; } |
|||
bool operator!=(const GuessNumberIterator& y) { return i != y.i; } |
|||
int operator*() { |
|||
std::cout << "Is your number less than or equal to " << i << "? "; |
|||
std::string s; |
|||
std::cin >> s; |
|||
return (s != "" && (s[0] == 'y' || s[0] == 'Y')) ? 0 : -1; |
|||
} |
|||
GuessNumberIterator& operator--() { --i; return *this; } |
|||
GuessNumberIterator operator--(int) { |
|||
GuessNumberIterator tmp = *this; --(*this); return tmp; } |
|||
GuessNumberIterator& operator+=(int n) { i += n; return *this; } |
|||
GuessNumberIterator& operator-=(int n) { i -= n; return *this; } |
|||
GuessNumberIterator operator+(int n) { |
|||
GuessNumberIterator tmp = *this; return tmp += n; } |
|||
GuessNumberIterator operator-(int n) { |
|||
GuessNumberIterator tmp = *this; return tmp -= n; } |
|||
int operator-(const GuessNumberIterator &y) { return i - y.i; } |
|||
int operator[](int n) { return *(*this + n); } |
|||
bool operator<(const GuessNumberIterator &y) { return i < y.i; } |
|||
bool operator>(const GuessNumberIterator &y) { return i > y.i; } |
|||
bool operator<=(const GuessNumberIterator &y) { return i <= y.i; } |
|||
bool operator>=(const GuessNumberIterator &y) { return i >= y.i; } |
|||
}; |
|||
inline GuessNumberIterator operator+(int n, GuessNumberIterator &i) { return i + n; } |
|||
const int lower = 0; |
|||
const int upper = 100; |
|||
int main() { |
|||
std::cout << "Instructions:\n" |
|||
<< "Think of integer number from " << lower << " (inclusive) to " |
|||
<< upper << " (exclusive) and\n" |
|||
<< "I will guess it. After each guess, I will ask you if it is less than\n" |
|||
<< "or equal to some number, and you will respond with \"yes\" or \"no\".\n"; |
|||
int answer = std::lower_bound(GuessNumberIterator(lower), GuessNumberIterator(upper), 0).i; |
|||
std::cout << "Your number is " << answer << ".\n"; |
|||
return 0; |
return 0; |
||
}</lang> |
}</lang> |
||
Line 528: | Line 473: | ||
} |
} |
||
</lang> |
</lang> |
||
=={{header|C++}}== |
|||
A clever solution that takes advantage of C++'s built-in binary search function <code>lower_bound()</code>. Instead of searching a slice of a container, we search a range of numbers by implementing a specially-designed custom iterator. |
|||
{{trans|Go}} |
|||
<lang cpp>#include <iostream> |
|||
#include <algorithm> |
|||
#include <string> |
|||
#include <iterator> |
|||
struct GuessNumberIterator : std::iterator<std::random_access_iterator_tag, int> { |
|||
int i; |
|||
GuessNumberIterator() { } |
|||
GuessNumberIterator(int _i) : i(_i) { } |
|||
GuessNumberIterator& operator++() { ++i; return *this; } |
|||
GuessNumberIterator operator++(int) { |
|||
GuessNumberIterator tmp = *this; ++(*this); return tmp; } |
|||
bool operator==(const GuessNumberIterator& y) { return i == y.i; } |
|||
bool operator!=(const GuessNumberIterator& y) { return i != y.i; } |
|||
int operator*() { |
|||
std::cout << "Is your number less than or equal to " << i << "? "; |
|||
std::string s; |
|||
std::cin >> s; |
|||
return (s != "" && (s[0] == 'y' || s[0] == 'Y')) ? 0 : -1; |
|||
} |
|||
GuessNumberIterator& operator--() { --i; return *this; } |
|||
GuessNumberIterator operator--(int) { |
|||
GuessNumberIterator tmp = *this; --(*this); return tmp; } |
|||
GuessNumberIterator& operator+=(int n) { i += n; return *this; } |
|||
GuessNumberIterator& operator-=(int n) { i -= n; return *this; } |
|||
GuessNumberIterator operator+(int n) { |
|||
GuessNumberIterator tmp = *this; return tmp += n; } |
|||
GuessNumberIterator operator-(int n) { |
|||
GuessNumberIterator tmp = *this; return tmp -= n; } |
|||
int operator-(const GuessNumberIterator &y) { return i - y.i; } |
|||
int operator[](int n) { return *(*this + n); } |
|||
bool operator<(const GuessNumberIterator &y) { return i < y.i; } |
|||
bool operator>(const GuessNumberIterator &y) { return i > y.i; } |
|||
bool operator<=(const GuessNumberIterator &y) { return i <= y.i; } |
|||
bool operator>=(const GuessNumberIterator &y) { return i >= y.i; } |
|||
}; |
|||
inline GuessNumberIterator operator+(int n, GuessNumberIterator &i) { return i + n; } |
|||
const int lower = 0; |
|||
const int upper = 100; |
|||
int main() { |
|||
std::cout << "Instructions:\n" |
|||
<< "Think of integer number from " << lower << " (inclusive) to " |
|||
<< upper << " (exclusive) and\n" |
|||
<< "I will guess it. After each guess, I will ask you if it is less than\n" |
|||
<< "or equal to some number, and you will respond with \"yes\" or \"no\".\n"; |
|||
int answer = std::lower_bound(GuessNumberIterator(lower), GuessNumberIterator(upper), 0).i; |
|||
std::cout << "Your number is " << answer << ".\n"; |
|||
return 0; |
|||
}</lang> |
|||
=={{header|Ceylon}}== |
=={{header|Ceylon}}== |
||
Line 1,049: | Line 1,049: | ||
} |
} |
||
}</lang> |
}</lang> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Line 1,090: | Line 1,089: | ||
"c" -> putStrLn "Yay!" |
"c" -> putStrLn "Yay!" |
||
</lang> |
</lang> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
Line 1,586: | Line 1,584: | ||
end |
end |
||
</lang> |
</lang> |
||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
|||
<lang>guessnumber[min0_, max0_] := |
|||
DynamicModule[{min = min0, max = max0, guess, correct = False}, |
|||
guess[] := Round@Mean@{min, max}; |
|||
Dynamic@If[correct, Row@{"Your number is ", guess[], "."}, |
|||
Column@{Row@{"I guess ", guess[], "."}, |
|||
Row@{Button["too high", max = guess[]], |
|||
Button["too low", min = guess[]], |
|||
Button["correct", correct = True]}}]]; |
|||
guessnumber[1, 100]</lang> |
|||
=={{header|MATLAB}}== |
=={{header|MATLAB}}== |
||
Line 1,638: | Line 1,647: | ||
Is 7 too high (H), too low (L), or equal (E)? H |
Is 7 too high (H), too low (L), or equal (E)? H |
||
Incorrect scoring. No further guesses.</pre> |
Incorrect scoring. No further guesses.</pre> |
||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
|||
<lang>guessnumber[min0_, max0_] := |
|||
DynamicModule[{min = min0, max = max0, guess, correct = False}, |
|||
guess[] := Round@Mean@{min, max}; |
|||
Dynamic@If[correct, Row@{"Your number is ", guess[], "."}, |
|||
Column@{Row@{"I guess ", guess[], "."}, |
|||
Row@{Button["too high", max = guess[]], |
|||
Button["too low", min = guess[]], |
|||
Button["correct", correct = True]}}]]; |
|||
guessnumber[1, 100]</lang> |
|||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
Line 2,002: | Line 2,000: | ||
I knew it! It took me only 5 tries.</pre> |
I knew it! It took me only 5 tries.</pre> |
||
=={{header|Perl 6}}== |
|||
<lang perl6>multi sub MAIN() { MAIN(0, 100) } |
|||
multi sub MAIN($min is copy where ($min >= 0), $max is copy where ($max > $min)) { |
|||
say "Think of a number between $min and $max and I'll guess it!"; |
|||
while $min <= $max { |
|||
my $guess = (($max + $min)/2).floor; |
|||
given lc prompt "My guess is $guess. Is your number higher, lower or equal? " { |
|||
when /^e/ { say "I knew it!"; exit } |
|||
when /^h/ { $min = $guess + 1 } |
|||
when /^l/ { $max = $guess } |
|||
default { say "WHAT!?!?!" } |
|||
} |
|||
} |
|||
say "How can your number be both higher and lower than $max?!?!?"; |
|||
}</lang> |
|||
You may execute this program with '<tt>perl6 program</tt>' or with '<tt>perl6 program min max</tt>'. Perl 6 creates a usage for us if we don't give the right parameters. It also parses the parameters for us and provides them via <tt>$min</tt> and <tt>$max</tt>. We use multi-subs to provide two MAIN subroutines so the user is able to choose between min and max parameters and no parameters at all, in which case min is set to 0 and max to 100. |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 2,272: | Line 2,251: | ||
(set! minimum (add1 guess))))) |
(set! minimum (add1 guess))))) |
||
(displayln "I was RIGHT!"))</lang> |
(displayln "I was RIGHT!"))</lang> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
<lang perl6>multi sub MAIN() { MAIN(0, 100) } |
|||
multi sub MAIN($min is copy where ($min >= 0), $max is copy where ($max > $min)) { |
|||
say "Think of a number between $min and $max and I'll guess it!"; |
|||
while $min <= $max { |
|||
my $guess = (($max + $min)/2).floor; |
|||
given lc prompt "My guess is $guess. Is your number higher, lower or equal? " { |
|||
when /^e/ { say "I knew it!"; exit } |
|||
when /^h/ { $min = $guess + 1 } |
|||
when /^l/ { $max = $guess } |
|||
default { say "WHAT!?!?!" } |
|||
} |
|||
} |
|||
say "How can your number be both higher and lower than $max?!?!?"; |
|||
}</lang> |
|||
You may execute this program with '<tt>perl6 program</tt>' or with '<tt>perl6 program min max</tt>'. Perl 6 creates a usage for us if we don't give the right parameters. It also parses the parameters for us and provides them via <tt>$min</tt> and <tt>$max</tt>. We use multi-subs to provide two MAIN subroutines so the user is able to choose between min and max parameters and no parameters at all, in which case min is set to 0 and max to 100. |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 2,760: | Line 2,759: | ||
}</lang> |
}</lang> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
{{works with|Guile}} |
{{works with|Guile}} |
||
Line 3,025: | Line 3,025: | ||
NONE => print "That is impossible.\n" |
NONE => print "That is impossible.\n" |
||
| SOME (result, _) => print ("Your number is " ^ Int.toString result ^ ".\n")</lang> |
| SOME (result, _) => print ("Your number is " ^ Int.toString result ^ ".\n")</lang> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
<lang>import Cocoa |
<lang>import Cocoa |