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

→‎with positive integers: added/changed comments and statements, simplified the program.
(→‎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:
=={{header|REXX}}==
===with positive integers===
This version only handles positive integers   (up to the  nine ''numericdecimal digits'',   value)but the default HIGH is one thousand.
<lang rexx>/*REXX pgmprogram plays guess-the-numberguess─the─number (with itself) with positive integers. */
numericparse digitsarg low high seed . 9 /*thisobtain isoptional thearguments normalfrom REXXthe defaultCL*/
parseif arg low=='' high| . low==",guess" then low= 1 /*getNot specified? optional argsThen fromuse the C.Ldefault.*/
if lowhigh=='' | thenhigh=="," then lowhigh=1 1000 /* " " " " " /*Not given?" Then use the default*/
if highfrac=='' | frac=="," then highfrac=1000 1 /* " " " " " " */
?=randomif datatype(lowseed,high 'W') then call random ,,seed /*Useful seed? Then /*getuse a random (non-negative) intseed.*/
?= 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= do try=1; say $; say; oguess=guess /*savenullify oldthe first guess. */
do if pos('high',info)\=#=01; then high oldg=guess g /*testsave ifthe itsguess toofor highlater comparison. */
if pos('lowhigh' , info)\==0 then low high=guess g /*test if "the guess is "too "high. " low. */
if guess=low+pos(high-'low' , info)%2\==0 then low = g /* " " " /*calculate next" guess " " low. */
say /*display a blank line before prompt. */
if guess=oguess then guess=guess+1 /*bump the # of guesses*/
say 'My$ low guess is' guessand ' high " inclusive):" /*issue the prompt message to /*display comp's guessterminal.*/
say /*display a blank line after prompt. */
 
g= (low + (high - low) / 2) / 1 select /*calculate the next guess & normalize.*/
if g=oldg then g= g + 1 when /*bump guess>? by thenone info=right("It'scause toowe're highclose.",40)*/
say 'My guess is' g when guess<? then info=right("It /*display computer's tooguess low.to ",40)the term.*/
if g=? then leave otherwise leave /*leavethis theguess TRYis correct; leave do& loopinform*/
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 info
end /*try*/
say 'Congratulations! You guessed the secret number in' try "tries."
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:
<pre style="height:70ex">
──────── 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 500313.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 875344.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 843328.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 819336.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 825332.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 250329.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 63330.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 47330.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 59329.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 58329.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 329.985871 and 1000 330.016358 inclusive).:
 
My guess is 500330.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 375329.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 343329.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 351329.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 329.99921 and 1000 330.000163 inclusive).:
 
My guess is 500329.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 625329.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 656330.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 329.999985 and 1000 330.000044 inclusive).:
 
My guess is 500330.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 125330
 
Congratulations! You guessed the secret number in 326 tries.
</pre>