Newsgroups: comp.lang.apl
Path: watmath!watserv2.uwaterloo.ca!torn!cs.utexas.edu!uunet!s5!hui
From: hui@fid.morgan.com (Roger Hui)
Subject: Re: Please send me your J code
Message-ID: <1992Dec13.174151.25847@fid.morgan.com>
Organization: Morgan Stanley & Co., New York, NY
References: <1992Dec10.154228.8389@ips.cs.tu-bs.de>
Date: Sun, 13 Dec 1992 17:41:51 GMT
Lines: 72

Martin Neitzel writes:

> NAME
>     moo - guessing game
>
> SYNOPSIS
>     /usr/games/moo
>
> DESCRIPTION
>     moo is a guessing game imported from England.  The  computer
>     picks  a  number consisting of four distinct decimal digits.
>     The player guesses four distinct digits being scored on each
>     guess.   A  ``cow'' is a correct digit in an incorrect posi-
>     tion.  A ``bull'' is a correct digit in a correct  position.
>     The  game  continues  until the player guesses the number (a
>     score of four bulls).

read  =. 1!:1
pr    =. 1!:2&2

univ  =. (*./@(~:"1) # ])@(#: i.@(*/))

score =. ; read@1:@pr@":
guess =. ?@# { ]
bull  =. +/@('b'&=)@(>@{:@[) = >@{.@[ +/@(="1) ]
bc    =. #@(>@{:@[) = ] +/@e."1 >@{.@[
prune =. (bull *. bc) # ]

s0    =. pr@('no choices left'&[)
s1    =. pr@('one choice left; it must be '&,)@":@,
sn    =. pr@(,&' choices left')@":@#
state =. s0`s1`sn@.(0 1&i.@#)

moo   =. ([state)@(score@guess prune ])^:(1&<@#) @ univ

The program above plays the guesser in the game of moo.
The algorithm was described to me in 1976 by W.F. Appleyard
of I.P. Sharp Associates, Calgary:  A universe of remaining
choices is kept.  At each round, the computer selects a
guess at random from the universe, then prunes the universe
according to the guess and its score of bulls and cows.

The definitions are divided into groups:

"read 1" reads from the keyboard.  "pr" prints the string
argument on the screen.

"univ" generates the universe of choices, given an integer
list of the upper limits for each position.  e.g. univ 4$10
or univ 10 11 12.

"score" displays an integer list on the screen, solicits
a score from the keyboard, and returns a list of the score 
and the guess, individually boxed.  "guess" selects a guess at 
random from the universe.  "bull" scores each choice in the 
universe regarding bulls; "bc" regarding bulls _and_ cows.  
"prune" discards choices inconsistent with the guess and 
its score.

"state" shows the current size of the universe.

"moo" plays the guesser in the game of moo.  The argument
is an integer list of the upper limits for each position.
(e.g. moo 4$10 would be the game as described in the man page.)
In each round, "moo"
. checks that the universe contains more than one choice;
. selects a guess at random and solicits its score;
. prunes the universe; and
. displays the state.

--------------
Roger Hui
