Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Please send me your J code

13 views
Skip to first unread message

Martin Neitzel

unread,
Dec 10, 1992, 10:42:28 AM12/10/92
to
When trying to lure other people into APL, I usually choose the
Mastermind(tm)/Moo(6) game as an effective example. I thought it
would now be worth the effort to cook up a small paper (along the
lines of LJ.Dickeys "What_is_J") introducing J by using this nice
application example.

Now, while I was writing the necessary functions I began to wonder how
other people would do them. And that it might be nice to have about
half a dozen different approaches in said introductionary paper.

So I'm soliciting you for J code for a "moo" program I human could
guess against. Please send it by email, I will make a summary of all
collected items. (Your program is all I need - I'll care for comments
and explanations aimed at the novices later; they wouldn't hurt,
though.)

Martin Neitzel

PS: Here are the game's rules (they haven't changed since Unix Version 7)
PPS: speaking of versions: I just have docs for Version 5, so I
might get confused by v6 features.


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).

Roger Hui

unread,
Dec 13, 1992, 12:41:51 PM12/13/92
to
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

0 new messages