Bulls and cows/Player: Difference between revisions
Content deleted Content added
→{{header|Tcl}}: (Hopefully) replaced constant: 4 with variable $size to make things genericc. |
J |
||
Line 2:
The task is to write a ''player'' of the [[Bulls and cows|Bulls and Cows game]], rather than a scorer. The player should give intermediate answers that respect the scores to previous attempts.
=={{header|J}}==
<lang j>require'misc'
poss=:1+~.4{."1 (i.!9)A.i.9
fmt=: ' ' -.~ ":
play=:3 :0
while.1<#poss=.poss do.
smoutput'guessing ',fmt guess=.({~ ?@#)poss
bc=.+/\_".prompt 'how many bull and cows? '
poss=.poss #~({.bc)=guess+/@:="1 poss
poss=.poss #~({:bc)=guess+/@e."1 poss
end.
if.#poss do.
'the answer is ',fmt,poss
else.
'no valid possibilities'
end.
)</lang>
For example:
<lang j> play''
guessing 7461
how many bull and cows? 0 1
guessing 3215
how many bull and cows? 0 3
guessing 2357
how many bull and cows? 2 0
guessing 1359
how many bull and cows? 3 0
the answer is 1358</lang>
=={{header|Python}}==
|