Anonymous user
Bulls and cows: Difference between revisions
→Version 1: indented some DO loops, compressed source code, added DO-END comment label, moved 1-liner subroutines to end of program. -- ~~~
Walterpachl (talk | contribs) m (→Version 2) |
(→Version 1: indented some DO loops, compressed source code, added DO-END comment label, moved 1-liner subroutines to end of program. -- ~~~) |
||
Line 2,516:
===Version 1===
This REXX version doesn't allow repeated digits.
<lang rexx>/*REXX
/* [CBLs = Carbon Based Lifeforms.] */
do forever /*play until guessed or enters QUIT */
if getN()==? then leave /*the CBL guessed (or deduced) "?".*/
call scorer /*tell CBL the number of bulls&cows.*/
call sy "You got" bulls 'bull's(bulls) "and" cows 'cow's(cows)"."
end /*forever*/
Line 2,533 ⟶ 2,534:
exit
/*─────────────────────────────────────GETN subroutine──────────────────*/
getN: bulls='[Bulls & Cows game] ' /*get a guess from the
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
if verify(0,n)==0 then do; call ser 'illegal numeral: 0' ; iterate; end
if length(n)<4 then do; call ser 'not enough numerals' ; iterate; end
if length(n)>4 then do; call ser 'too many numerals' ; iterate; end
▲ call ser 'illegal digit: 0'
_=verify(n,987654321)
if _\==0 then do; call ser 'illegal character:' substr(n,_,1);iterate;end
return n
end /*forever*/
/*─────────────────────────────────────GETRAND subroutine───────────────*/
getRand: ?=''; do until length(?)==4
return ?
/*─────────────────────────────────────SCORER subroutine────────────────*/
scorer: g=?
bulls=0; do j=1 for 4
if x\==substr(
cows=0
do k=1 for 4
cows=cows + (pos(substr(n,k,1),g)\==0)
end /*k*/
return
/*─────────────────────────────────────1-liner subroutines──────────────*/
ser: call sy '*** error! ***'; call sy arg(1) ; return
sy: say; say arg(1); say ; return</lang>
===Version 2===
|