Bulls and cows/Player: Difference between revisions

Content added Content deleted
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: Line 2,508:
About a third of the REXX program deals with presentation and/or validation of answers.
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). */
<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 # /*get the first guess for the game. */
L=1234; H=9876; call gen@ /*generate all possibilities. */
do tries=1 until #()<2 | bull==4; say
do forever; g=random(L,H); if @.g\==. then leave /*obtain a random 1st guess. */
end /*forever*/ /* [↑] 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. */
if @.?==. then iterate /*was this choice already eliminated ? */
do until #()<2 | bull==4; say; call ask /*examine @ list; get answer.*/
call bull# ?,g /*obtain the bulls and cows count. */
do ?=L to H; if @.?==. then iterate /*is this already eliminated ?*/
if bull\==bulls | cow\==cows then @.?=. /*eliminate choice.*/
call bull# ?,g /*obtain bulls and cows count.*/
end /*?*/
if bull\==bulls | cow\==cows then @.?=. /*eliminate this possibility. */
call #
end /*?*/
end /*tries*/
end /*until*/


if #==0 then do; call serr "At least one of your responses was invalid."; exit; end
if #==0 then do; call serr "At least one of your responses was invalid."; exit; end
say; say " ╔═════════════════════════════════════════════════╗"
say; say " ╔═════════════════════════════════════════════════╗"
say " ║ ║"
say " ║ ║"
Line 2,526: Line 2,526:
say " ║ ║"
say " ║ ║"
say " ╚═════════════════════════════════════════════════╝"; say
say " ╚═════════════════════════════════════════════════╝"; say
say tries 'tries.'
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
#: #=0; do k=1234 to 9876; if @.k==. then iterate; #=# + 1; g=k; end; return #
#: #=0; do k=L to H; if @.k==. then iterate; #=#+1; g=k; end; return #
gen@: @.=.; do j=1234 to 9876; if \rep() & pos(0, j)==0 then @.j=j; end; return
gen@: @.=.; do j=L to H; 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
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
serr: say; say '───── ***error*** ' ! arg(1); return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
bull#: parse arg n,q; L=length(n); bulls=0; cows=0 /*initialize some vars.*/
bull#: parse arg n,q; w=length(n); bulls=0 /*W: # digits in N; bull cntr=0 */
do j=1 for L; if substr(n, j, 1) \== substr(q, j, 1) then iterate
do j=1 for w; if substr(n, j, 1) \== substr(q, j, 1) then iterate
bulls=bulls + 1 /*bump the bull counter. */
bulls=bulls+1; q=overlay(., q, j) /*bump counter; disallow for cow*/
q=overlay(., q, j) /*disallow this for a cow count.*/
end /*j*/ /* [↑] bull count═══════════════*/
end /*j*/ /* [↑] bull count═══════════════*/
cows=0 /*set the number of cows to zero.*/

do k=1 for L; _=substr(n, k, 1); if pos(_, q)==0 then iterate
do k=1 for w; _=substr(n, k, 1); if pos(_, q)==0 then iterate
cows=cows + 1 /*bump the cow counter. */
cows=cows + 1; q=translate(q, , _) /*bump counter; allow multiple #*/
q=translate(q, , _) /*this allows for multiple digits*/
end /*k*/ /* [↑] cow count═══════════════*/
end /*k*/ /* [↑] cow count═══════════════*/
return
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
say pad "How many bulls and cows were guessed with " g '? [─── or QUIT]'
when abbrev('QUIT', x, 1) then exit /*the user wants to quit playing.*/
pull x 1 bull cow . /*PULL capitalizes the arguments.*/
when bull == '' then != "no numbers were entered."
if abbrev('QUIT', x, 1) then exit /*the user wants to quit playing.*/
when cow == '' then != "not enough numbers were entered."
when words(x) > 2 then != "too many numbers entered: " x
select
when bull == '' then != "no numbers were entered."
when \datatype(bull, 'W') then != "1st number (bulls) not an integer: " bull
when cow == '' then != "not enough numbers were entered."
when \datatype(cow , 'W') then != "2nd number (cows) not an integer: " cow
when words(x) > 2 then != "too many numbers entered: " x
when bull <0 | bull >4 then != "1st number (bulls) not 0 ──► 4: " bull
when \datatype(bull, 'W') then != "1st number (bulls) not an integer: " bull
when cow <0 | cow >4 then != "2nd number (cows) not 0 ──► 4: " cow
when \datatype(cow , 'W') then != "2nd number (cows) not an integer: " cow
when bull + cow > 4 then != "sum of bulls and cows can't be > 4: " x
when bull <0 | bull >4 then != "1st number (bulls) not 0 ──► 4: " bull
otherwise !=
end /*select*/
when cow <0 | cow >4 then != "2nd number (cows) not 0 ──► 4: " cow
when bull + cow > 4 then != "sum of bulls and cows can't be > 4: " x
if !\=='' then do; call serr; iterate; end /*prompt the user and try again. */
otherwise !=
bull=bull/1; cow=cow/1; return /*normalize bulls & cows numbers.*/
end /*select*/
end /*forever*/</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}}==
=={{header|Ring}}==