The old "Cows and Bulls" game in REXX
Select messages from
# through # FAQ
[/[Print]\]

MVSFORUMS.com -> TSO and ISPF

#1: The old "Cows and Bulls" game in REXX Author: HimeshLocation: Chicago PostPosted: Fri Jan 03, 2003 11:50 pm
    —
Got hold of a working version of this game.....and here it is....

Code:

/*====REXX===========================================================*/         
/*                                                                   */         
/*        This rexx plays the game of moo.  The object is to guess a */         
/*   four digit number generated by the program.  The number must    */         
/*   have 4 non-repeating digits.  After each guess, the program     */         
/*   tells the player the number of digits in the guess that appear  */         
/*   anywhere in the number to be guessed ( cows ) and the number    */         
/*   of digits in the guess that appear in the same position in the  */         
/*   guess and the target ( bulls ).                                 */         
/*                                                                   */         
/*===================================================================*/         
                                                                               
select = 'do j = 1 to 4;if substr(aim,i,1) = substr(line,j,1) then' ,           
   'cows = cows + 1; end'                                                       

say ' Should BULLS be excluded form COW count (Y/N) ?'                         
parse upper pull line                                                           

if left(line,1,1) = 'Y' then select = 'else' select                             
select = 'if substr(aim,i,1) = substr(line,i,1) then' ,                         
   'bulls = bulls + 1;' select                                                 
sum = 0                                                                         
ngame = 0                                                                       

goagain: ,                                                                     

aim = genran()                                                                 
NGUESS = 0                                                                     
guessagain: ,                                                                   
nguess = nguess + 1                                                             
reguess: ,                                                                     

say 'Enter guess #' nguess                                                     
parse pull line                                                                 

pos = index(line,'?')                                                           
if pos <> 0 then do                                                             
   say 'You gave up on guess #' nguess || '.'                                   
   say 'The correct number was' aim || '.'                                     
   ngame = ngame + 1                                                           
   sum = sum + nguess + 5                                                       
   signal offer                                                                 
   end                                                                         

if datatype(line,'W') <> 1 then do                                             
   say 'Guess must be only numerics.  Please re-enter guess #' nguess           
   signal reguess                                                               
   end                                                                         

if length(line) <> 4 then do                                                   
   say 'Guess must be 4 characters.  Please re-enter guess #' nguess           
   signal reguess                                                               
   end                                                                         

bulls = 0                                                                       
cows = 0                                                                       

do i = 1 to 4                                                                   
   interpret select                                                             
   end                                                                         

if bulls < 4 then do                                                           
   say 'Bulls =' bulls 'and cows =' cows                                       
   signal guessagain                                                           
   end                                                                         

say 'CONGRATULATIONS you guessed the number on guess #' nguess || '.'           

ngame = ngame + 1                                                               
sum = sum + nguess                                                             

offer: ,                                                                       
say 'Your average for' ngame 'games was' sum / ngame || '.'                     
say 'Would you like to play again ?'                                           
parse upper pull ans                                                           
if left(ans,1) = 'Y' then signal goagain                                       
exit                                                                           
                                                                               
genran: procedure                                                               
temp = time('L')                                                               
temp = right(temp,6)                                                           
aim = left(temp,1)                                                             

start = 2                                                                       
nexttry: ,                                                                     

do i = 6 to start by -1                                                         
   next = substr(temp,i,1)                                                     
   pos = index(aim,next)                                                       

   if pos = 0 then do                                                           
      aim = aim || next                                                         
      if length(aim) = 4 then return aim                                       
      end                                                                       
   end                                                                         

start = 1                                                                       
temp = time('L')                                                               
temp = right(temp,6)                                                           
signal nexttry                                                                 




MVSFORUMS.com -> TSO and ISPF


output generated using printer-friendly topic mod. All times are GMT - 5 Hours

Page 1 of 1

Powered by phpBB © 2001, 2005 phpBB Group