Guess the number/With feedback: Difference between revisions
Content added Content deleted
(→{{header|Kotlin}}: Cleanup: Use Kotlin random; Use null chaining instead of null assertion) |
Not a robot (talk | contribs) (Add BCPL) |
||
Line 356: | Line 356: | ||
END</lang> |
END</lang> |
||
=={{header|BCPL}}== |
|||
<lang bcpl>get "libhdr" |
|||
static $( randstate = ? $) |
|||
let rand(min,max) = valof |
|||
$( let x = ? |
|||
let range = max-min |
|||
let mask = 1 |
|||
while mask < range do mask := (mask << 1) | 1 |
|||
$( randstate := random(randstate) |
|||
x := (randstate >> 6) & mask |
|||
$) repeatuntil 0 <= x < range |
|||
resultis x + min |
|||
$) |
|||
let readguess(min,max) = valof |
|||
$( let x = ? |
|||
writes("Guess? ") |
|||
x := readn() |
|||
if min <= x < max then resultis x |
|||
writes("Invalid input.*N") |
|||
$) repeat |
|||
let play(min,max,secret) be |
|||
$( let tries, guess = 0, ? |
|||
$( guess := readguess(min,max) |
|||
if guess < secret then writes("Too low!*N") |
|||
if guess > secret then writes("Too high!*N") |
|||
tries := tries + 1 |
|||
$) repeatuntil guess = secret |
|||
writef("Correct! You guessed it in %N tries.*N", tries) |
|||
$) |
|||
let start() be |
|||
$( let min, max = ?, ? |
|||
writes("Guess the number*N*N") |
|||
writes("Random seed? ") ; randstate := readn() |
|||
$( writes("Lower bound? ") ; min := readn() |
|||
writes("Upper bound? ") ; max := readn() + 1 |
|||
test max-1 > min break or writes("Invalid bounds.*N") |
|||
$) repeat |
|||
wrch('*N') |
|||
play(min, max, rand(min, max)) |
|||
$)</lang> |
|||
{{out}} |
|||
<pre>Guess the number |
|||
Random seed? 12345 |
|||
Lower bound? 1 |
|||
Upper bound? 100 |
|||
Guess? 0 |
|||
Invalid input. |
|||
Guess? 101 |
|||
Invalid input. |
|||
Guess? 50 |
|||
Too low! |
|||
Guess? 75 |
|||
Too high! |
|||
Guess? 62 |
|||
Too high! |
|||
Guess? 56 |
|||
Too high! |
|||
Guess? 53 |
|||
Too high! |
|||
Guess? 52 |
|||
Correct! You guessed it in 6 tries.</pre> |
|||
=={{header|Befunge}}== |
=={{header|Befunge}}== |
||