Guess the number/With feedback (player): Difference between revisions
Content added Content deleted
(→with positive numbers: added/changed comments and whitespace, simplfied some statements.) |
(→with positive integers: added/changed comments and statements, simplified the program.) |
||
Line 2,263: | Line 2,263: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===with positive integers=== |
===with positive integers=== |
||
This version only handles positive integers |
This version only handles positive integers up to the nine decimal digits, but the default HIGH is one thousand. |
||
<lang rexx>/*REXX |
<lang rexx>/*REXX program plays guess─the─number (with itself) with positive integers. */ |
||
parse arg low high seed . /*obtain optional arguments from the CL*/ |
|||
if low=='' | low=="," then low= 1 /*Not specified? Then use the default.*/ |
|||
if |
if high=='' | high=="," then high= 1000 /* " " " " " " */ |
||
if |
if frac=='' | frac=="," then frac= 1 /* " " " " " " */ |
||
if datatype(seed, 'W') then call random ,,seed /*Useful seed? Then use a random seed.*/ |
|||
?= random(low, high) /*generate random number from low->high*/ |
|||
$="Try to guess my integer (it's between" low 'and' high" inclusive)." |
|||
$= "──────── Try to guess my number (it's between " /*part of a prompt message.*/ |
|||
g= /*nullify the first guess. */ |
|||
do #=1; oldg= g /*save the guess for later comparison. */ |
|||
if pos('high', info)\==0 then high= g /*test if the guess is too high. */ |
|||
if pos('low' , info)\==0 then low = g /* " " " " " " low. */ |
|||
say /*display a blank line before prompt. */ |
|||
if guess=oguess then guess=guess+1 /*bump the # of guesses*/ |
|||
say $ low ' and ' high " inclusive):" /*issue the prompt message to terminal.*/ |
|||
say /*display a blank line after prompt. */ |
|||
g= (low + (high - low) / 2) / 1 /*calculate the next guess & normalize.*/ |
|||
if g=oldg then g= g + 1 /*bump guess by one 'cause we're close.*/ |
|||
say 'My guess is' g /*display computer's guess to the term.*/ |
|||
if g=? then leave /*this guess is correct; leave & inform*/ |
|||
if g>? then info= right(' Your guess is too high.', 60, "─") |
|||
end /*select*/ |
|||
else info= right(' Your guess is too low.' , 60, "─") |
|||
end /*try*/ |
|||
say |
say info |
||
end /*try*/ |
|||
say 'Congratulations! You guessed the secret number in' try "tries." |
|||
/*stick a fork in it, we're done.*/ |
say /*stick a fork in it, we're all done. */ |
||
say 'Congratulations! You guessed the secret number in' # "tries."</lang> |
|||
</lang> |
|||
'''output''' shown is from playing several games: |
'''output''' shown is from playing several games: |
||
<pre style="height:70ex"> |
<pre style="height:70ex"> |
||
Try to guess my number (it's between 1 and 1000 inclusive) |
──────── Try to guess my number (it's between 1 and 1000 inclusive): |
||
My guess is 500.5 |
|||
──────────────────────────────────── Your guess is too high. |
|||
──────── Try to guess my number (it's between 1 and 500.5 inclusive): |
|||
My guess is 250.75 |
|||
───────────────────────────────────── Your guess is too low. |
|||
──────── Try to guess my number (it's between 250.75 and 500.5 inclusive): |
|||
My guess is 375.625 |
|||
──────────────────────────────────── Your guess is too high. |
|||
──────── Try to guess my number (it's between 250.75 and 375.625 inclusive): |
|||
My guess is |
My guess is 313.1875 |
||
───────────────────────────────────── Your guess is too low. |
|||
It's too low. |
|||
──────── Try to guess my number (it's between 313.1875 and 375.625 inclusive): |
|||
My guess is 750 |
|||
It's too low. |
|||
My guess is |
My guess is 344.40625 |
||
──────────────────────────────────── Your guess is too high. |
|||
It's too high. |
|||
──────── Try to guess my number (it's between 313.1875 and 344.40625 inclusive): |
|||
My guess is 812 |
|||
It's too low. |
|||
My guess is |
My guess is 328.796875 |
||
───────────────────────────────────── Your guess is too low. |
|||
It's too high. |
|||
──────── Try to guess my number (it's between 328.796875 and 344.40625 inclusive): |
|||
My guess is 827 |
|||
It's too high. |
|||
My guess is |
My guess is 336.601563 |
||
──────────────────────────────────── Your guess is too high. |
|||
It's too low. |
|||
──────── Try to guess my number (it's between 328.796875 and 336.601563 inclusive): |
|||
My guess is 823 |
|||
It's too low. |
|||
My guess is |
My guess is 332.699219 |
||
──────────────────────────────────── Your guess is too high. |
|||
──────── Try to guess my number (it's between 328.796875 and 332.699219 inclusive): |
|||
Congratulations! You guessed the secret number in 9 tries. |
|||
_______ |
|||
██████████████████████████████████████████████████████████████████████ |
|||
My guess is 330.748047 |
|||
Try to guess my number (it's between 1 and 1000 inclusive). |
|||
──────────────────────────────────── Your guess is too high. |
|||
──────── Try to guess my number (it's between 328.796875 and 330.748047 inclusive): |
|||
My guess is 500 |
|||
It's too high. |
|||
My guess is |
My guess is 329.772461 |
||
───────────────────────────────────── Your guess is too low. |
|||
It's too high. |
|||
──────── Try to guess my number (it's between 329.772461 and 330.748047 inclusive): |
|||
My guess is 125 |
|||
It's too high. |
|||
My guess is |
My guess is 330.260254 |
||
──────────────────────────────────── Your guess is too high. |
|||
It's too high. |
|||
──────── Try to guess my number (it's between 329.772461 and 330.260254 inclusive): |
|||
My guess is 32 |
|||
It's too low. |
|||
My guess is |
My guess is 330.016358 |
||
──────────────────────────────────── Your guess is too high. |
|||
It's too low. |
|||
──────── Try to guess my number (it's between 329.772461 and 330.016358 inclusive): |
|||
My guess is 55 |
|||
It's too low. |
|||
My guess is |
My guess is 329.89441 |
||
───────────────────────────────────── Your guess is too low. |
|||
It's too high. |
|||
──────── Try to guess my number (it's between 329.89441 and 330.016358 inclusive): |
|||
My guess is 57 |
|||
It's too low. |
|||
My guess is |
My guess is 329.955384 |
||
───────────────────────────────────── Your guess is too low. |
|||
──────── Try to guess my number (it's between 329.955384 and 330.016358 inclusive): |
|||
Congratulations! You guessed the secret number in 10 tries. |
|||
My guess is 329.985871 |
|||
██████████████████████████████████████████████████████████████████████ |
|||
───────────────────────────────────── Your guess is too low. |
|||
Try to guess my number (it's between |
──────── Try to guess my number (it's between 329.985871 and 330.016358 inclusive): |
||
My guess is |
My guess is 330.001115 |
||
──────────────────────────────────── Your guess is too high. |
|||
It's too high. |
|||
──────── Try to guess my number (it's between 329.985871 and 330.001115 inclusive): |
|||
My guess is 250 |
|||
It's too low. |
|||
My guess is |
My guess is 329.993493 |
||
───────────────────────────────────── Your guess is too low. |
|||
It's too high. |
|||
──────── Try to guess my number (it's between 329.993493 and 330.001115 inclusive): |
|||
My guess is 312 |
|||
It's too low. |
|||
My guess is |
My guess is 329.997304 |
||
───────────────────────────────────── Your guess is too low. |
|||
It's too low. |
|||
──────── Try to guess my number (it's between 329.997304 and 330.001115 inclusive): |
|||
My guess is 359 |
|||
It's too high. |
|||
My guess is |
My guess is 329.99921 |
||
───────────────────────────────────── Your guess is too low. |
|||
──────── Try to guess my number (it's between 329.99921 and 330.001115 inclusive): |
|||
Congratulations! You guessed the secret number in 7 tries. |
|||
My guess is 330.000163 |
|||
██████████████████████████████████████████████████████████████████████ |
|||
──────────────────────────────────── Your guess is too high. |
|||
Try to guess my number (it's between |
──────── Try to guess my number (it's between 329.99921 and 330.000163 inclusive): |
||
My guess is |
My guess is 329.999687 |
||
───────────────────────────────────── Your guess is too low. |
|||
It's too low. |
|||
──────── Try to guess my number (it's between 329.999687 and 330.000163 inclusive): |
|||
My guess is 750 |
|||
It's too high. |
|||
My guess is |
My guess is 329.999925 |
||
───────────────────────────────────── Your guess is too low. |
|||
It's too low. |
|||
──────── Try to guess my number (it's between 329.999925 and 330.000163 inclusive): |
|||
My guess is 687 |
|||
It's too high. |
|||
My guess is |
My guess is 330.000044 |
||
──────────────────────────────────── Your guess is too high. |
|||
──────── Try to guess my number (it's between 329.999925 and 330.000044 inclusive): |
|||
Congratulations! You guessed the secret number in 5 tries. |
|||
My guess is 329.999985 |
|||
██████████████████████████████████████████████████████████████████████ |
|||
───────────────────────────────────── Your guess is too low. |
|||
Try to guess my number (it's between |
──────── Try to guess my number (it's between 329.999985 and 330.000044 inclusive): |
||
My guess is |
My guess is 330.000015 |
||
──────────────────────────────────── Your guess is too high. |
|||
It's too high. |
|||
──────── Try to guess my number (it's between 329.999985 and 330.000015 inclusive): |
|||
My guess is 250 |
|||
It's too high. |
|||
My guess is |
My guess is 330 |
||
Congratulations! You guessed the secret number in |
Congratulations! You guessed the secret number in 26 tries. |
||
</pre> |
</pre> |
||