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. */
call # L=1234; H=9876; call gen@ /*getgenerate theall first guess for the gamepossibilities. */
do tries=1forever; until #g=random(L,H)<2; |if bull@.g\==4;. then leave /*obtain a random 1st guess. say*/
call gen@ end /*forever*/ /*generate all the possibilities./* [↑] obtain rand 1st guess.*/
call prompter
$$1= '───── How many bulls and cows were guessed with '; $$2=" ? [─── or QUIT]"
do ?=1234 to 9876 /*traipse through the whole list. */
do until #()<2 | bull==4; say; call ask if @.?==. then iterate /*wasexamine this@ choicelist; already eliminated ?get answer.*/
do ?=L to H; if call bull# @.?,g ==. then iterate /*obtainis the bulls and cows count.this already eliminated ?*/
call bull# ?,g if bull\==bulls | cow\==cows then @.?=. /*eliminateobtain bulls and cows choicecount.*/
if bull\==bulls | cow\==cows then @.?=. end /*?eliminate this possibility. */
call # end /*?*/
end /*triesuntil*/
 
if #==0 then do; call serr "At least one of your responses was invalid."; exit; end
say; say " ╔═════════════════════════════════════════════════╗"
say " ║ ║"
Line 2,526:
say " ║ ║"
say " ╚═════════════════════════════════════════════════╝"; say
say tries 'tries.'
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
#: #=0; do k=1234L to 9876H; if @.k==. then iterate; #=# + 1; g=k; end; return #
gen@: @.=.; do j=1234L to 9876H; if \rep() & pos(0, j)==0 then @.j=j; end; return
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 pad '───── '***error*** ' ! arg(1); return
/*──────────────────────────────────────────────────────────────────────────────────────*/
bull#: parse arg n,q; Lw=length(n); bulls=0; cows=0 /*W: # digits /*initializein someN; bull cntr=0 vars.*/
do j=1 for Lw; if substr(n, j, 1) \== substr(q, j, 1) then iterate
bulls=bulls + 1; q=overlay(., q, j) /*bump thecounter; disallow for bull counter. cow*/
q=overlay(., q, j) /*disallow this for a cow count.*/
end /*j*/ /* [↑] bull count═══════════════*/
cows=0 q=overlay(., q, j) /*disallowset thisthe fornumber aof cowcows to countzero.*/
 
do k=1 for Lw; _=substr(n, k, 1); if pos(_, q)==0 then iterate
cows=cows + 1; q=translate(q, , _) /*bump thecounter; allow multiple cow counter. #*/
q=translate(q, , _) /*this allows for multiple digits*/
end /*k*/ /* [↑] cow count═══════════════*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
ask: do forever; say $$1 g $$2; pull x 1 bull cow . /*display prompt; obtain answer.*/
prompter: pad= '─────' /*define PAD characters for msgs.*/
select /* [↑] PULL capitalizes the args*/
do forever; say
saywhen padabbrev('QUIT', "Howx, many1) bulls andthen cowsexit were guessed with " g /*the user '?wants to quit [─── or QUIT]'playing.*/
pull x 1when bull cow . == '' then != /*PULL"no capitalizesnumbers thewere argumentsentered.*/"
ifwhen abbrev('QUIT',cow x, 1)== '' then exit then /*the!= user"not wantsenough tonumbers quitwere playingentered.*/"
when words(x) > 2 then != "too many numbers entered: " x
select
when \datatype(bull ==, 'W') then != "1st number (bulls) not an integer: " then != "no numbers were entered."bull
when \datatype(cow ==, 'W') then != "2nd number (cows) not an integer: " then != "not enough numbers were entered."cow
when words(x) > 2 bull <0 | bull >4 then != "too1st manynumber numbers entered: "(bulls) not 0 ──► 4: " xbull
when cow <0 | cow >4 when \datatype(bull, 'W') then != "1st2nd number (bullscows) not an0 integer──► 4: " bull cow
when bull when+ \datatype(cow ,> 4 'W') then != "2ndsum numberof bulls and (cows) notcan't anbe integer> 4: " cow x
otherwise when bull <0 | bull >4 then != "1st number (bulls) not 0 ──► 4: " bull!=
end /*select*/
when cow <0 | cow >4 then != "2nd number (cows) not 0 ──► 4: " cow
if !\=='' then do; when bullcall +serr; cow >iterate; 4 end /*prompt the then != "sum of bullsuser and cows can't be > 4: "try again. x*/
bull=bull/1; otherwise cow=cow/1; return /*normalize bulls & cows !=numbers.*/
end end /*selectforever*/</lang><br><br>
if !\=='' then do; call serr; iterate; end /*prompt the user and try again. */
bull=bull / 1; cow=cow / 1; return /*normalize bulls & cows numbers.*/
end /*forever*/</lang> <br><br>
 
=={{header|Ring}}==