Anonymous user
Bulls and cows/Player: Difference between revisions
m
→{{header|REXX}}: simplified checking for eliminations, changed/added whitespace and comments, elided extra blanks when prompting.
m (→{{header|REXX}}: added better checks for guesses that contain a zero digit.) |
m (→{{header|REXX}}: simplified checking for eliminations, changed/added whitespace and comments, elided extra blanks when prompting.) |
||
Line 2,508:
About a third of the REXX program deals with presentation and/or validation of answers.
<lang rexx>/*REXX program plays the Bulls & Cows game with CBLFs (Carbon Based Life Forms). */
parse arg ? .; if datatype(?,'W') then call random ,,? /*Random seed? Make repeatable*/
call gen@ /*generate all the possibilities. */▼
$$1= '───── How many bulls and cows were guessed with '; $$2=" ? [─── or QUIT]"
do until #()<2 | bull==4; say; call ask
do ?=L to H; if
call bull# ?,g
if bull\==bulls | cow\==cows then @.?=.
if #==0 then do; call serr "At least one of your responses was invalid."; exit;
say; say " ╔═════════════════════════════════════════════════╗"
say " ║ ║"
Line 2,526:
say " ║ ║"
say " ╚═════════════════════════════════════════════════╝"; say
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
#: #=0; do k=
gen@: @.=.; do j=
rep: do k=1 for 3; if pos(substr(j, k, 1), j, k+1)\==0 then return 1; end; return 0
serr: say; say
/*──────────────────────────────────────────────────────────────────────────────────────*/
bull#: parse arg n,q;
do j=1 for
bulls=bulls
q=overlay(., q, j) /*disallow this for a cow count.*/▼
end /*j*/ /* [↑] bull count═══════════════*/
do k=1 for
cows=cows + 1; q=translate(q, ,
end /*k*/ /* [↑] cow count═══════════════*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
ask: do forever; say $$1 g $$2; pull x 1 bull cow . /*display prompt; obtain answer.*/
select /* [↑] PULL capitalizes the args*/
when words(x) > 2 then != "too many numbers entered: " x
select▼
when cow <0 | cow >4
when bull
otherwise
▲ end /*select*/
if !\=='' then do;
bull=bull/1;
end
=={{header|Ring}}==
|