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   (up to the   ''numeric digits''   value).
This version only handles positive integers up to the nine decimal digits,   but the default HIGH is one thousand.
<lang rexx>/*REXX pgm plays guess-the-number (with itself) with positive integers*/
<lang rexx>/*REXX program plays guess─the─number (with itself) with positive integers. */
numeric digits 9 /*this is the normal REXX default*/
parse arg low high seed . /*obtain optional arguments from the CL*/
parse arg low high .,guess /*get optional args from the C.L.*/
if low=='' | low=="," then low= 1 /*Not specified? Then use the default.*/
if low=='' then low=1 /*Not given? Then use the default*/
if high=='' | high=="," then high= 1000 /* " " " " " " */
if high=='' then high=1000 /* " " " " " " */
if frac=='' | frac=="," then frac= 1 /* " " " " " " */
?=random(low,high) /*get a random (non-negative) int*/
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.*/

do try=1; say $; say; oguess=guess /*save old guess.*/
g= /*nullify the first guess. */
if pos('high',info)\==0 then high=guess /*test if its too high.*/
do #=1; oldg= g /*save the guess for later comparison. */
if pos('low' ,info)\==0 then low =guess /* " " " " low. */
if pos('high', info)\==0 then high= g /*test if the guess is too high. */
guess=low+(high-low)%2 /*calculate next guess.*/
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 'My guess is' guess /*display comp's guess.*/
say $ low ' and ' high " inclusive):" /*issue the prompt message to terminal.*/
say /*display a blank line after prompt. */

select
g= (low + (high - low) / 2) / 1 /*calculate the next guess & normalize.*/
when guess>? then info=right("It's too high.",40)
if g=oldg then g= g + 1 /*bump guess by one 'cause we're close.*/
when guess<? then info=right("It's too low. ",40)
say 'My guess is' g /*display computer's guess to the term.*/
otherwise leave /*leave the TRY do loop*/
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 500
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 875
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 843
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 819
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 825
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 250
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 63
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 47
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 59
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 58
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 1 and 1000 inclusive).
──────── Try to guess my number (it's between 329.985871 and 330.016358 inclusive):


My guess is 500
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 375
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 343
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 351
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 1 and 1000 inclusive).
──────── Try to guess my number (it's between 329.99921 and 330.000163 inclusive):


My guess is 500
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 625
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 656
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 1 and 1000 inclusive).
──────── Try to guess my number (it's between 329.999985 and 330.000044 inclusive):


My guess is 500
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 125
My guess is 330


Congratulations! You guessed the secret number in 3 tries.
Congratulations! You guessed the secret number in 26 tries.
</pre>
</pre>