 Your skill will accomplishwhat the force of many cannot PerlMonks

### (Golf) The Animal Game

by Masem (Monsignor)
 on Jan 23, 2002 at 00:58 UTC Need Help??

One of the earlier games that came out of the first computers was the so-called Animal game. The game would be to have the computer try to guess what animal the person was thinking of by asking a series of yes or no questions. If the animal was not identified, then the computer would ask the user to type in some question that would identify the user's animal from any other animal that would be classified in the current line of questioning. For example, a run of the program might go like (C == computer, U == user):
```C> Is your animal smaller than a breadbox?
U> n
C> Is your animal a cat?
U> n
U> sparrow
C> Please enter a question that would be true for a bird, but false fo
+r a cat:
U> Does your animal have wings?

# Next iter #

U> n
C> Does your animal have wings?
U> Y
C> Is your animal a sparrow?
U> n
U> chicken
C> Please enter a question that would be true for a chicken, but false
+ for a sparrow:

# ETC... #
In other words, the computer would become more intelligent as more replies were added. Of course, this assumes that the user was faithful in entering replies and had a sufficiently good set of distinguising questions to separate out one type of animal from another.

Now, for the golf: Write an engine that does the above in as few characters as possible. Of course, to help, you've got 4 'free' functions that you can use:

• a(\$question) asks the user the passed \$question, and returns either true if the user replied yes to it, or false otherwise.
• g(\$animal) asks the user if their animal is \$animal. Returns true if so, false otherwise. THIS IS NEW IN UPDATE
• t() asks the user what their animal is, and returns the answer the user gives
• i(\$guessed, \$user) asks the user to identify a question that is true for \$user but false for \$guessed (where \$guesses should be what the computer guessed).
• w() asks the user if they want to continue; true if so, false otherwise (and should end the program).
You may also preinitialize any data structure for 'free' with a single question and the y/n answers for it. (Such as "Is your animal larger than a breadbox?" with 'giraffe' for true, 'housecat' for false).

Everything else inside the code block that does this will count as strokes to the golf score. You can use any data structure that you like. No extra modules, however, but the program need not be strict or -w compliant.

Update As tilly's solution indicates, I missed one 'free' function, the g() function which does the final guess (as described in the problem statement). Also, indirectly replying to tilly, assume the free functions return 0 or 1 for false or thuth, respectively. (I'm figuring the 0 value is more useful for further golfing). Also, don't worry about 'a' vs 'an'. Assume that you can decide which is appropriate in the free functions, and doesn't need to be handled by your golf sub.

-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
"I can see my house from here!"
It's not what you know, but knowing how to find it if you don't know that's important

Replies are listed 'Best First'.
Re (tilly) 1: (Golf) The Animal Game
by tilly (Archbishop) on Jan 23, 2002 at 02:21 UTC
117 116 117. But with caveats.

The main caveat is that I can add or lose characters depending on exact behaviour of these additional functions. (Particularly important is the behaviour of "a". I assumed that it returned 0,1 and added returns on prints but not a ?.) Here are the exact functions as I used them for my solution:

```sub a {
print @_, "\n";
my \$r = <STDIN>;
if (\$r =~ /y/i) {
return 1;
}
elsif (\$r =~ /n/i) {
return 0;
}
else {
a(@_);
}
}

sub t {
my \$ans = <STDIN>;
chomp(\$ans);
return \$ans;
}

sub i {
print "Please enter a question that would be true for a "
. "\$user_answer, but false for a \$guessed:\n";
my \$ans = <STDIN>;
chomp(\$ans);
return \$ans;
}

sub w {
if (a("\nDo you want to continue?")) {
print "\nStarting a new game.\n";
}
else {
print "BYE!\n";
exit();
}
}
Add to that my one free initialization:
```\$Q{a}="golfer";
And then my solution:
```#         1         2         3         4         5         6
+7         8         9        10        11
#123456789_123456789_123456789_123456789_123456789_123456789_123456789
+_123456789_123456789_123456789_123456789_1234567
{*q=\%Q;*q=\$q{a\$q{q}}while\$q{q};\$q{0}{a}=\$q{a},\$q{1}{a}=t,\$q{q}=i(\$q{a

# Was
#{*q=*Q;*q=\$q{a\$q{q}}while\$q{q};\$q{0}{a}=\$q{a},\$q{1}{a}=t,\$q{q}=i(\$q{a
UPDATE
jarich is right, I forgot to test something. My first typeglob assignment was wrong
Nice solution, but it really gets thrown out if you pick the same two animals in alternation.
```Starting a new game.
y
n
sparrow
Please enter a question that would be true for a sparrow, but false fo
+r a chicken:

Do you want to continue?
y

Starting a new game.
n
y

Do you want to continue?
y

Starting a new game.
n
sparrow
Please enter a question that would be true for a sparrow, but false fo
+r a chicken:

Do you want to continue?
y
In fact I can never get it to filter down beyond one quesiton. Surely it's trying to do a sort and should look like:
```does your animal have fur?
y
y
y

Do you want to continue?
but I can't get beyond:
```the last question you answered:
false -> computer's previous guess.
Re: (Golf) The Animal Game
by dragonchild (Archbishop) on Jan 23, 2002 at 02:18 UTC
I'll come in at 153 characters. I'm sure it has to be better than that ... I'm using too many temp vars.

Updated: Forgot to change my datastructure from a hash of hashes to a hash of arrays. That caused the bug jarich noticed. ++!

```#!/usr/local/bin/perl

my %tree = (
'larger than a breadbox' => [
'cat',
'giraffe',
],
);

sub a {
print 'Is your animal ', shift, "?\n";
return 1 if \$answer =~ /y/i;
return 0;
}

sub t {
}

sub i {
my (\$g, \$u) = @_;
print "What is a question that is true for \$u, but false for \$g?\n
+";
}

sub w {
print "Do you want to continue?\n";
return 1 if \$answer =~ /y/i;
return 0;
}

#234567890#234567890#234567890#234567890#234567890#234567890#234567890
+#234567890
sub f {
(\$s,%t)=@_;\$q=\$s;do{{\$v=a(\$q);\$a=\$t{\$q}[\$v];\$q=\$a,redo if\$t{\$a};\$q=\$s,
+next if a(
\$a);\$n=&t;\$o=i(\$a,\$n);\$t{\$o}=[\$t{\$q}[\$v],\$n];\$t{\$q}[\$v]=\$o;\$q=\$s}}whil
+e&w
}

__END__

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

I can't golf it better, but I can fix a bug I've spotted. If you say your animal isn't bigger than a bread box, it guesses your animal to be "1", which isn't much fun. These are my changes:
```my %tree = (
'1' => 'giraffe',
'0' => 'cat',
},
);              # anon hashref rather than array ref

#234567890#234567890#234567890#234567890#234567890
sub f{
(\$s,%t)=@_;\$q=\$s;do{{\$v=a(\$q);\$a=\$t{\$q}->{\$v};\$q=
\$a,redo if\$t{\$a};\$q=\$s,next if a(\$a);\$n=&t;\$o=i(\$a
,\$n);\$t{\$o}={0=>\$t{\$q}->{\$v},1=>\$n};\$t{\$q}->{\$v}=
\$o;\$q=\$s;}}while&w
}
Sorry about the extra 13 characters. ;) It's a cool program though, I've got to admit that the idea had me stumped 'til I saw yours and Tilly's answers.

jarich

update: I can't count (or rather I forgot the indexing), 13 extra characters, not 6.

It's supposed to be anon listref. I had it as anon hashref, then switched it to anon listref. Updated my original node.

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Heh. I found an interesting bug in mine. If you use the same question for two branches, it overwrites. This is due to the 1-D flattening of the N-D structure. *grins* I don't feel like fixing it. :-)

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: (Golf) The Animal Game
by redsquirrel (Hermit) on Jan 23, 2002 at 21:42 UTC
This is my second attempt at Perl Golf. Although my score (281) sucks, I wanted to post my code because I had a hard enough time just getting it to work at all. :-) I know I could make my code more concise, but I've run out of time! I've got to actually get some work done today!

My free subs:

```sub a {
print shift;
return 1 if \$answer =~ /y/i;
return 0;
}

sub t {
}

sub i {
my (\$g, \$u) = @_;
print "What is a question that is true for \$u, but false for \$g?\n
+";
}

sub w {
print "Do you want to continue?\n";
return 1 if \$answer =~ /y/i;
return 0;
}

sub g {
my \$animal = shift;
print "n" if \$animal =~ /^[aeiou]/;
print " \$animal?\n";
return 1 if \$answer =~ /y/i;
return 0;
}

My free initialization:

```my @t = (
[
undef,undef,
]
);

And my long-winded (281) solution:

```sub r{my\$t=shift;my\$x=a(\$t->);\$t->[\$x]?r(\$t->[\$x]):return \$t->[2
+][\$x]}sub
n{my\$t=shift;for(0,1){if(\$t->[\$_]eq\$b&&!\$t->[\$_]){\$t->[\$_]=[undef,u
+ndef,[\$b,\$
a,\$q]];return 1}last if\$t->[\$_]&&n(\$t->[\$_])}}while(1){\$b=r(\$t);if(
+!g(\$b)){\$a
=t();\$q=i(\$b,\$a);n(\$t)}last if!w()}
Re: (Golf) The Animal Game
by particle (Vicar) on Jan 24, 2002 at 03:29 UTC
i'm with redsquirrel; it's my second golf outing. i'm in with 183. obviously i'm not on the pro tour.

here's the full code:

```#!/usr/local/bin/perl -w
use strict;
\$|=1;

# www.PerlMonks.org 140734|(Golf) The Animal Game

# ask the question passed, return true or false
sub a(\$) {
my (\$qu, \$ans) = (shift);
while(!\$ans) { print "\n\$qu?\n"; chomp (\$ans = <STDIN>) }
\$ans =~ m/y/io ? 1 : 0;
};

# ask if animal is passed value, return true or false
sub g(\$) {
my (\$ani, \$ans) = (shift);
while(!\$ans) { print "\nis animal \$ani?\n"; chomp (\$ans = <STDIN>)
+ }
\$ans =~ m/^y/io ? 1 : 0;
};

# ask what animal, return user input
sub t() {
my \$ans;
while(!\$ans) { print "\nwhat animal?\n"; chomp (\$ans = <STDIN>) }
\$ans;
};

# get question to identify animal:
# true for new, false for old; return question
sub i(\$\$) {
my (\$old, \$new, \$qu) = (shift, shift);
while(!\$qu) {
print "\nenter a question false for \$old but true for \$new:\n"
+;
chomp (\$qu = <STDIN>);
}
\$qu;
};

# ask to continue, return true or false
sub w() { a('play') ? 1 : exit 0 };

# question/answer data structure: hash of arrays
my %q = (
"larger than a breadbox" => ['cat', 'giraffe'],
);

no strict;
while(w){(\$k,\$v)=each%q;\$k||redo;\$u=\$\$v[a(\$k)];next if(g(\$u));while((\$
+l,\$x)=each%q){next unless\$\$x eq \$u;a(\$l)?\$y=\$\$x:goto N;goto E i
+f(g(\$y))}N:\$b=i(\$u,\$c=t);\$q{\$b}=[\$u,\$c];E:}
i have a commented version, with slightly better variable names...

```no strict;
{
while(w){
# select a question
(\$k,\$v)=each%q;
# try again if no more questions (end of hash)
\$k||redo;
# ask question, get proper animal from question hash
\$t2=\$\$v[a(\$k)];
# guess animal, end loop if found answer
next if(g(\$t2));
# lookup keys with value matching \$t2
while((\$k2,\$v2)=each%q){
next unless\$\$v2 eq \$t2;
# if key exists, ask question
a(\$k2)?\$t5=\$\$v2:goto NEW;
goto END if(g(\$t5));
}
NEW:
\$t4=i(\$t2,\$t3=t);
\$q{\$t4}=[\$t2,\$t3];
END:
}
}
thanks for the fun.

~Particle

Re: (Golf) The Animal Game
by Anonymous Monk on Jan 23, 2002 at 22:56 UTC
I still can' t figure out what kind of sparrow is bigger than a breadbox. Either I have large loaves of bread around here, or very small sparrows.
Re: (Golf) The Animal Game
by trs80 (Priest) on Jan 28, 2002 at 03:42 UTC
Here is my entry in the Golf game, but with some additions that may throw me off the tour. I made two data structures @a for animals and @q for questions. I then update these inside of the allowed subs that are/were outlined in the rules. With that said I am at ~111 characters in my do_it sub.
```my @q = ( 'Is your animal larger then a bread box',
'Is your animal taller then a car' );
my @a = ( 'dog' );
sub a {
print shift, "?\n";
my \$a = <>;
return 1 if \$a =~ /y/i;
}

sub g {
print 'I think your animal is a ', shift , ", am I right?\n";
my \$a = <>;
return 1 if \$a =~ /y/i;
}

sub t {
my \$q = shift;
my \$a = <>;
chomp \$a;
push @a , \$a;
return \$a;
}

sub i {
my (\$u,\$g) = @_;
print "What is a question that is true for \$u, but false for \$g?\n
+";
}

sub w {
print "Do you want to conitue?\n";
my \$a = <>;
if (\$a =~ /n/i) {
die "Thanks for playing\n";
};
do_it();
}

sub do_it{
for\$s(@a){\$g=\$s;for\$q(@q){if(a(\$q)){\$g=\$r{\$q}||\$s}}
if(g(\$g)){w()}else{\$t=t();\$i=i(\$t,\$g);\$r{\$i}=\$t;do_it()}}}
&do_it();

1;

Create A New User
Node Status?
node history
Node Type: perlmeditation [id://140734]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2020-11-28 14:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?