Guess the number/With feedback (player): Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
m (Fix header.)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 173:
Return, "Too High"
}</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}}==
Line 238 ⟶ 219:
Guesses: 7
</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}}==
Line 336:
else
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;
}</lang>
Line 528 ⟶ 473:
}
</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}}==
Line 1,049:
}
}</lang>
 
 
=={{header|Haskell}}==
Line 1,090 ⟶ 1,089:
"c" -> putStrLn "Yay!"
</lang>
 
 
=={{header|Icon}} and {{header|Unicon}}==
Line 1,586 ⟶ 1,584:
end
</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}}==
Line 1,638 ⟶ 1,647:
Is 7 too high (H), too low (L), or equal (E)? H
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}}==
Line 2,002 ⟶ 2,000:
 
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}}==
Line 2,272 ⟶ 2,251:
(set! minimum (add1 guess)))))
(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}}==
Line 2,760 ⟶ 2,759:
 
}</lang>
 
=={{header|Scheme}}==
{{works with|Guile}}
Line 3,025:
NONE => print "That is impossible.\n"
| SOME (result, _) => print ("Your number is " ^ Int.toString result ^ ".\n")</lang>
 
=={{header|Swift}}==
<lang>import Cocoa
10,327

edits