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

→‎with positive numbers: added/changed comments and whitespace, simplfied some statements.
No edit summary
(→‎with positive numbers: added/changed comments and whitespace, simplfied some statements.)
Line 2,417:
</pre>
 
=== with positive numbers ===
This version handles decimal fractions, &nbsp; the (method used onlycan generatesgenerate numbers withfrom azero one-digitto decimal fraction,five butfractional thisdigits.
<lang rexx>/*REXX program plays guess─the─number (with itself) with positive rational numbers. */
<br>REXX program will handle any length of decimal fractions than can be generated with the current &nbsp; ''numeric digits''.
parse arg low high frac seed . /*obtain optional arguments from the CL*/
<lang rexx>/*REXX pgm plays guess-the-number (with itself) with positive numbers.*/
numericif digits 9low=='' | low=="," then low= 1 /*Not specified? /*thisThen isuse the normal REXX default.*/
parseif arghigh=='' low| high .==",guess" then high= 1000 info /*get optional" " " " " " args from the C.L.*/
if lowfrac=='' | frac=="," then lowfrac=1 1 /*Not given?" " " " " " Then use the default*/
if high==datatype(seed, 'W') then high=1000 call random ,,seed /*Useful " " " " " seed? Then "use a random seed.*/
?fdigs=random(low* 10,high*10)/10*frac /*Tougher? compute Itthe maynumber beof afractional fraction.digs*/
?= random(low, high) + random(0,fdigs) / fdigs /*Tougher game? It may have fractions.*/
$="Try to guess my number (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=pos('low+(high-low)/2' , info)\==0 then low = g /* 1 " " " " " " low. /*calc next guess, norm*/
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. */</lang>
say 'Congratulations! You guessed the secret number in' # "tries.""</lang>
'''output''' will generaly be about ten times longer (that is, has ten times the guesses) as the previous REXX version.
'''output''' will generally be about ten times longer (that is, has ten times the guesses) as the previous REXX version.
<pre style="height:35ex">
──────── 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 1000 inclusive).
 
──────── 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 313.1875
───────────────────────────────────── Your guess is too low.
 
──────── Try to guess my number (it's between 313.1875 and 375.625 inclusive):
 
My guess is 344.40625
──────────────────────────────────── Your guess is too high.
 
──────── Try to guess my number (it's between 313.1875 and 344.40625 inclusive):
 
My guess is 328.796875
───────────────────────────────────── Your guess is too low.
 
──────── Try to guess my number (it's between 328.796875 and 344.40625 inclusive):
 
My guess is 336.601563
──────────────────────────────────── Your guess is too high.
 
──────── Try to guess my number (it's between 328.796875 and 336.601563 inclusive):
 
My guess is 332.699219
──────────────────────────────────── Your guess is too high.
 
──────── Try to guess my number (it's between 328.796875 and 332.699219 inclusive):
 
My guess is 330.748047
──────────────────────────────────── Your guess is too high.
 
──────── Try to guess my number (it's between 328.796875 and 330.748047 inclusive):
 
My guess is 329.772461
───────────────────────────────────── Your guess is too low.
 
──────── Try to guess my number (it's between 329.772461 and 330.748047 inclusive):
 
My guess is 330.260254
───────────────────────────────────── Your guess is too low.
 
──────── Try to guess my number (it's between 330.260254 and 330.748047 inclusive):
 
My guess is 330.504151
───────────────────────────────────── Your guess is too low.
 
──────── Try to guess my number (it's between 330.504151 and 330.748047 inclusive):
 
My guess is 330.626099
──────────────────────────────────── Your guess is too high.
 
──────── Try to guess my number (it's between 330.504151 and 330.626099 inclusive):
 
My guess is 330.565125
───────────────────────────────────── Your guess is too low.
 
──────── Try to guess my number (it's between 330.565125 and 330.626099 inclusive):
 
My guess is 750330.25595612
───────────────────────────────────── 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 330.595612 and 330.626099 inclusive):
My guess is 625.375
Try to guess my number (it's between 1 and 1000 inclusive).
 
My guess is 687330.8125610856
──────────────────────────────────── 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 330.595612 and 330.610856 inclusive):
My guess is 656.59375
Try to guess my number (it's between 1 and 1000 inclusive).
 
My guess is 640330.984375603234
──────────────────────────────────── 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 330.595612 and 330.603234 inclusive):
My guess is 648.789063
Try to guess my number (it's between 1 and 1000 inclusive).
 
My guess is 644330.886719599423
───────────────────────────────────── 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 330.599423 and 330.603234 inclusive):
My guess is 646.837891
Try to guess my number (it's between 1 and 1000 inclusive).
 
My guess is 645330.862305601329
──────────────────────────────────── 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 330.599423 and 330.601329 inclusive):
My guess is 646.350098
Try to guess my number (it's between 1 and 1000 inclusive).
 
My guess is 646330.593995600376
──────────────────────────────────── 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 330.599423 and 330.600376 inclusive):
My guess is 646.472047
Try to guess my number (it's between 1 and 1000 inclusive).
 
My guess is 646330.4110735999
───────────────────────────────────── 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 330.5999 and 330.600376 inclusive):
My guess is 646.380586
Try to guess my number (it's between 1 and 1000 inclusive).
 
My guess is 646330.39583600138
──────────────────────────────────── 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 330.5999 and 330.600138 inclusive):
My guess is 646.403452
Try to guess my number (it's between 1 and 1000 inclusive).
 
My guess is 646330.399641600019
──────────────────────────────────── 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 330.5999 and 330.600019 inclusive):
My guess is 646.401547
Try to guess my number (it's between 1 and 1000 inclusive).
 
My guess is 646330.40059459996
───────────────────────────────────── 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 330.59996 and 330.600019 inclusive):
My guess is 646.400118
Try to guess my number (it's between 1 and 1000 inclusive).
 
My guess is 646330.3998859999
───────────────────────────────────── 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 330.59999 and 330.600019 inclusive):
My guess is 646.399999
Try to guess my number (it's between 1 and 1000 inclusive).
 
My guess is 646330.400059600005
──────────────────────────────────── 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 330.59999 and 330.600005 inclusive):
My guess is 646.400029
Try to guess my number (it's between 1 and 1000 inclusive).
 
My guess is 646330.400014599998
───────────────────────────────────── 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 330.599998 and 330.600005 inclusive):
My guess is 646.400007
Try to guess my number (it's between 1 and 1000 inclusive).
 
My guess is 646330.400003600002
──────────────────────────────────── 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 330.599998 and 330.600002 inclusive):
My guess is 646.400001
Try to guess my number (it's between 1 and 1000 inclusive).
 
My guess is 646330.46
 
Congratulations! You guessed the secret number in 3029 tries.
</pre>