Bulls and cows/Player: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: added better checks for guesses that contain a zero digit.) |
|||
Line 2,512: | Line 2,512: | ||
do tries=1 until #()<2 | bull==4; say |
do tries=1 until #()<2 | bull==4; say |
||
call prompter |
call prompter |
||
do ?= |
do ?=1234 to 9876 /*traipse through the whole list. */ |
||
if @.?==. then iterate /*was this choice already eliminated ? */ |
if @.?==. then iterate /*was this choice already eliminated ? */ |
||
call bull# ?,g /*obtain the bulls and cows count. */ |
call bull# ?,g /*obtain the bulls and cows count. */ |
||
Line 2,529: | Line 2,529: | ||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
#: #=0; do k= |
#: #=0; do k=1234 to 9876; if @.k==. then iterate; #=# + 1; g=k; end; return # |
||
gen@: @.=.; |
gen@: @.=.; do j=1234 to 9876; 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 |
|||
⚫ | |||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
bull#: parse arg n,q; L=length(n); bulls=0; cows=0 /*initialize some vars.*/ |
bull#: parse arg n,q; L=length(n); bulls=0; cows=0 /*initialize some vars.*/ |
||
Line 2,550: | Line 2,549: | ||
do forever; say |
do forever; say |
||
say pad "How many bulls and cows were guessed with " g '? [─── or QUIT]' |
say pad "How many bulls and cows were guessed with " g '? [─── or QUIT]' |
||
pull x 1 bull cow |
pull x 1 bull cow . /*PULL capitalizes the arguments.*/ |
||
if abbrev('QUIT', x, 1) then exit /*the user wants to quit playing.*/ |
if abbrev('QUIT', x, 1) then exit /*the user wants to quit playing.*/ |
||
select |
select |
||
when bull=='' |
when bull == '' then != "no numbers were entered." |
||
when cow =='' |
when cow == '' then != "not enough numbers were entered." |
||
when |
when words(x) > 2 then != "too many numbers entered: " x |
||
when \datatype(bull, 'W') then != "1st number (bulls) not an integer: " bull |
when \datatype(bull, 'W') then != "1st number (bulls) not an integer: " bull |
||
when \datatype(cow , 'W') then != "2nd number (cows) not an integer: " cow |
when \datatype(cow , 'W') then != "2nd number (cows) not an integer: " cow |
||
Line 2,564: | Line 2,563: | ||
end /*select*/ |
end /*select*/ |
||
if !\=='' then do; call serr; iterate; end /*prompt the user and try again. */ |
if !\=='' then do; call serr; iterate; end /*prompt the user and try again. */ |
||
bull=bull/1; |
bull=bull / 1; cow=cow / 1; return /*normalize bulls & cows numbers.*/ |
||
end /*forever*/</lang> |
end /*forever*/</lang> <br><br> |
||
=={{header|Ring}}== |
=={{header|Ring}}== |