Guess the number/With feedback (player): Difference between revisions
Content added Content deleted
Not a robot (talk | contribs) (Add FOCAL) |
Not a robot (talk | contribs) (Add BCPL) |
||
Line 268: | Line 268: | ||
PRINT "Goodbye." |
PRINT "Goodbye." |
||
END</lang> |
END</lang> |
||
=={{header|BCPL}}== |
|||
<lang bcpl>get "libhdr" |
|||
let reads(s) = valof |
|||
$( s%0 := 0 |
|||
$( let c = rdch() |
|||
if c = endstreamch resultis false |
|||
if c = '*N' resultis true |
|||
s%0 := s%0 + 1 |
|||
s%(s%0) := c |
|||
$) repeat |
|||
$) |
|||
let choose(chs) = valof |
|||
$( let ans = vec 80 |
|||
writef("[%S]? ",chs) |
|||
unless reads(ans) finish |
|||
unless ans%0=1 loop |
|||
for i=1 to chs%0 |
|||
if (ans%1|32) = (chs%i|32) resultis chs%i |
|||
$) repeat |
|||
let tantrum() be |
|||
$( writes("Cheater!*N") |
|||
finish |
|||
$) |
|||
let guess(lo, hi, t) = valof |
|||
$( let ans = vec 80 |
|||
test hi<lo do tantrum() |
|||
or test hi=lo |
|||
$( writef("Is the number %N ",lo) |
|||
test choose("yn")='y' resultis t or tantrum() |
|||
$) |
|||
or |
|||
$( let g = (hi-lo)/2+lo |
|||
writef("My guess is %N. Too low, too high, or correct ",g) |
|||
switchon choose("lhc") into |
|||
$( case 'l': resultis guess(g, hi, t+1) |
|||
case 'h': resultis guess(lo, g, t+1) |
|||
case 'c': resultis t |
|||
$) |
|||
$) |
|||
$) |
|||
let start() be |
|||
$( let min = ? and max = ? |
|||
writes("Lower bound? ") ; min := readn() |
|||
writes("Upper bound? ") ; max := readn() |
|||
writef("It took %N attempts.*N", guess(min, max, 1)) |
|||
$)</lang> |
|||
{{out}} |
|||
<pre>Lower bound? 1 |
|||
Upper bound? 100 |
|||
My guess is 50. Too low, too high, or correct [lhc]? l |
|||
My guess is 75. Too low, too high, or correct [lhc]? l |
|||
My guess is 87. Too low, too high, or correct [lhc]? h |
|||
My guess is 81. Too low, too high, or correct [lhc]? l |
|||
My guess is 84. Too low, too high, or correct [lhc]? l |
|||
My guess is 85. Too low, too high, or correct [lhc]? c |
|||
It took 6 attempts.</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |