Anonymous user
Bulls and cows: Difference between revisions
m
→{{header|R}}
m (→{{header|R}}) |
|||
Line 1,924:
}
print(paste("You won in",attempts,"attempt(s)!"))</lang>
=={{header|REXX}}==
<lang rexx>
/*REXX program to play the game of "Bulls & Cows". */
?=''
do until length(?)==4
r=random(1,9)
if pos(r,?)\==0 then iterate
?=?||r
end
do forever
call getN
if n==? then call winner
bulls=0
cows=0
g=?
do j=1 for 4
x=substr(n,j,1)
_=x==substr(g,j,1)
if _==0 then iterate
bulls=bulls+1
g=overlay(' ',g,j)
end /*j*/
do k=1 for 4
cows=cows+(pos(substr(n,k,1),g)\==0)
end
call sy "You got" bulls 'bull's(bulls) "and" cows 'cow's(cows)"."
end /*DO forever*/
/*─────────────────────────────────────GETN subroutine──────────────────*/
getN: bulls='[Bulls & Cows game] ' /*get a guess from the guesser. */
do forever
call sy bulls 'Please enter a four-digit guess (or QUIT):'
parse pull n _ .
nu=n
upper nu
if nu=='QUIT' then exit
if n=='' then do
call ser 'no argument specified.'
iterate
end
if _\=='' then do
call ser 'too many arguments specified.'
iterate
end
_=verify(0,n)
if _==0 then do
call ser 'illegal digit: 0'
iterate
end
_=verify(n,987654321)
if _\==0 then do
call ser 'illegal character:' substr(n,_,1)
iterate
end
if length(n)<4 then do
call ser 'not enough digits'
iterate
end
if length(n)>4 then do
call ser 'too many digits'
iterate
end
return
end
/*─────────────────────────────────────SY subroutine────────────────────*/
sy: say; say arg(1); say; return
/*─────────────────────────────────────SER subroutine───────────────────*/
ser: call sy '*** error! ***'; call sy arg(1); return
/*─────────────────────────────────────S subroutine─────────────────────*/
s: if arg(1)==1 then return ''; return 's'
/*─────────────────────────────────────WINNER subrouting────────────────*/
winner: say
say " ┌─────────────────────────────────────────┐"
say " │ │"
say " │ Congratulations, you've guessed it !! │"
say " │ │"
say " └─────────────────────────────────────────┘"
say
exit
</lang>
=={{header|Ruby}}==
|