Bulls and cows: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Changed to Wren S/H) |
(Added Quackery.) |
||
Line 6,024: | Line 6,024: | ||
Loop |
Loop |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Quackery}}== |
|||
<code>transpose</code> is defined at [[Matrix transposition#Quackery]]. |
|||
<syntaxhighlight lang="Quackery"> [ size 4 = dup not if |
|||
[ say "Must be four digits." cr ] ] is 4chars ( $ --> b ) |
|||
[ true swap witheach |
|||
[ char 1 char 9 1+ within not if |
|||
[ say "Must be 1-9 only." cr |
|||
not conclude ] ] ] is 1-9 ( $ --> b ) |
|||
[ 0 9 of |
|||
swap witheach |
|||
[ 1 unrot char 1 - poke ] |
|||
0 swap witheach + |
|||
4 = dup not if |
|||
[ say "Must all be different." cr ] ] is all-diff ( $ --> b ) |
|||
[ $ "Guess four digits, 1-9, no duplicates: " |
|||
input |
|||
dup 4chars not iff drop again |
|||
dup 1-9 not iff drop again |
|||
dup all-diff not iff drop again ] is guess ( $ --> $ ) |
|||
[ $ "123456789" shuffle 4 split drop ] is rand$ ( --> $ ) |
|||
[ 2 pack transpose |
|||
[] swap witheach |
|||
[ dup unpack != iff |
|||
[ nested join ] |
|||
else drop ] |
|||
dup [] != if |
|||
[ transpose unpack ] |
|||
4 over size - ] is -bulls ( $ $ --> $ $ n ) |
|||
[ join sort |
|||
0 swap |
|||
behead swap witheach |
|||
[ tuck = if [ dip 1+ ] ] |
|||
drop ] is cows ( $ $ --> n ) |
|||
[ say "Guess the four numbers." cr cr |
|||
say "They are all different and" |
|||
say " between 1 and 9 inclusive." cr cr |
|||
randomise rand$ |
|||
[ guess |
|||
over -bulls |
|||
dup 4 = iff say "Correct." done |
|||
dup echo 1 = iff |
|||
[ say " bull." cr ] |
|||
else |
|||
[ say " bulls." cr ] |
|||
cows |
|||
dup echo 1 = iff |
|||
[ say " cow." cr ] |
|||
else |
|||
[ say " cows." cr ] |
|||
again ] |
|||
cr drop 2drop ] is play ( --> )</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Guess the four numbers. |
|||
They are all different and between 1 and 9 inclusive. |
|||
Guess four digits, 1-9, no duplicates: 1234 |
|||
0 bulls. |
|||
0 cows. |
|||
Guess four digits, 1-9, no duplicates: 5678 |
|||
1 bull. |
|||
3 cows. |
|||
Guess four digits, 1-9, no duplicates: 5786 |
|||
1 bull. |
|||
3 cows. |
|||
Guess four digits, 1-9, no duplicates: 5867 |
|||
0 bulls. |
|||
4 cows. |
|||
Guess four digits, 1-9, no duplicates: 7658 |
|||
2 bulls. |
|||
2 cows. |
|||
Guess four digits, 1-9, no duplicates: 7685 |
|||
0 bulls. |
|||
4 cows. |
|||
Guess four digits, 1-9, no duplicates: 6758 |
|||
Correct. |
|||
</pre> |
|||
=={{header|R}}== |
=={{header|R}}== |
||
{{works with|R|2.8.1}} |
{{works with|R|2.8.1}} |