Bulls and cows: Difference between revisions

m
→‎version 1: changed command and whitespace.
m (added it to the games category.)
m (→‎version 1: changed command and whitespace.)
Line 4,077:
<br>and also change the prompt message.
<br>The REXX statement that contains the &nbsp; '''translate''' &nbsp; statement can be removed if repeated digits aren't allowed.
<lang rexx>/*REXX pgmprogram scores the Bulls & Cows game with CBLFs (Carbon Based Life Forms). */
?=; do until length(?)==4 /*generate a unique 4four-digit number. */
r=random(1, 9) /*change 1──►01──► 0 to allow a 0zero digdigit*/
if pos(r, ?)\==0 then iterate /*don't allow a repeated digit/numeral. */
?=? || r /*add random digdigit by concatenation. */
end /*until length ··· */ /* [↑] builds a unique 4four-digit #num.*/
id= '──────── [Bulls & Cows] '
do until bulls==4; say /*play until guessed or enters "QUIT".*/
say '───── [Bulls & Cows] say id 'Please enter a 4-digit guess (with no zeroes) [or Quit]:'
pull n; n=space(n,0); if abbrev('QUIT',n,1) then exit /*Quitwants to quit ? */
q=?; L=length(n); bulls=0; cows=0 /*initialize some REXX variables. */
 
do until bulls==4; say do j=1 for L; /*play untilif guessedsubstr(n,j,1)\==substr(q, |entersj, 1) then iterate /*bull? QUIT*/
bulls=bulls+1; q=overlay(.,q,j) /*bump bull count; disallow for cow*/
say '───── [Bulls & Cows] Please enter a 4-digit guess (with no zeroes) [or Quit]:'
end /*j*/ /* [↑] bull count───────────────count─────────────────*/
pull n; n=space(n,0); if abbrev('QUIT',n,1) then exit /*Quit ?*/
say " │" /*cow ? */
q=?; L=length(n); bulls=0; cows=0 /*initialize some REXX variables.*/
do k=1 for L; _=substr(n,k,1); if pos(_,q)==0 then iterate
 
do j=1 for L; cows=cows+1 ; if substr(n,j,1)\= q=substrtranslate(q,j ,1 _) then iterate/*bump cow count; allow mult digits*/
bulls=bulls+1 end /*k*/ /*bump the bull count. /* [↑] cow count─────────────────*/
q=overlay(.,q,j) say; /*disallow this for a cow@='You count.got' */bulls
if L\==0 & bulls\==4 then say "─────id You got"@ bulls 'bull's(bulls) "and" cows 'cow's(cows)"."
end /*j*/ /* [↑] bull count───────────────*/
end /*until bulls···==4*/
 
say
do k=1 for L; _=substr(n,k,1); if pos(_,q)==0 then iterate
say; say " ┌─────────────────────────────────────────┐"
cows=cows+1 /*bump the cow count. */
q=translate(q,,_) /*thissay allows" for multiple digits*/ │ │"
end /*k*/ say " /* [↑] cow Congratulations, count───────────────*/you've guessed it !! │"
say " │ │"
say
say " └─────────────────────────────────────────┘"; say
if L\==0 & bulls\==4 then say "───── You got" bulls 'bull's(bulls) "and" cows 'cow's(cows)"."
exit /*stick a fork in it, we're all done. */
end /*until bulls···*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
 
s: if arg(1)==1 then return ''; return 's' /*this function /*handles pluralspluralization. */</lang>
say; say " ┌─────────────────────────────────────────┐"
say " │ │"
say " │ Congratulations, you've guessed it !! │"
say " │ │"
say " └─────────────────────────────────────────┘"; say
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────S subroutine────────────────────────*/
s: if arg(1)==1 then return ''; return 's' /*handles plurals.*/</lang>
 
===Version 2===