Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

AI Animals

by Limbic~Region (Chancellor)
on Sep 08, 2002 at 06:27 UTC ( #195991=sourcecode: print w/ replies, xml ) Need Help??

Category: [Fun Stuff]
Author/Contact Info Limbic~Region
Description: Program follows a logic tree to guess an animal that you are thinking of. The more it plays - the better it gets, hence the AI reference. It is based off a game I saw on an Apple something or other way back in gradeschool. Of course, I never saw the source - so this is my guess of how it is done.
#!/usr/bin/perl -Tw
use strict;
use AnyDBM_File;
use Fcntl; 

my $dbfile = "animals";
tie (my %brain, "AnyDBM_File", $dbfile, O_CREAT|O_RDWR, 0600) or die "
+Can't open $dbfile: $!\n";
&create_new unless keys %brain;

my $loop;
until ($loop) {
 print "Hello, my name is AI!  I love animals and guessing games.\n";
 print "I have currently learned " . &animals_learned . " animals.\n";
 print "\nPlease think of an animal and I will try to guess it - press
+ enter when ready\n";
 <>;
 my $x = my $y = my $found = 1;
 while ($found) {
  my $entry = my $response = my $animal = my $question = my $answer = 
+0;
  ($entry = $brain{$x,$y}) =~ s/(^[QG]): //;
  if ( $1 eq "Q" ) {
   until ( $response =~ /^y/i || $response =~ /^n/i ) {
    print "$entry\n";
    chomp ($response = <>);
   }
   if ( $response =~ /^y/i ) {   
    $y = $y * 2 - 1;
    ++$x;
   }
   elsif ($response =~ /^n/i) {
    $y *= 2;
    ++$x;
   }
  }
  elsif ( $1 eq "G" ) {
   $found = 0;
   until ( $response =~ /^y/i || $response =~ /^n/i ) {
    print "Is your animal a(n) $entry?\n";
    chomp ($response = <>);
   }
   if ( $response =~ /^y/i ) {
    print "\nYeah, I got it right!\n";
   }
   elsif ($response =~ /^n/i) {
    until ( $animal ) {
     print "OK, I give up, what animal were you thinking of?\n";
     chomp ($animal = <>);
    }
    $animal =~ s/a[n]* //i;
    until ( $question ) {
     print "\nEnter a yes/no question to tell the difference between a
+(n) $animal and a(n) $entry?\n";
     chomp ($question = <>);
    }
    $question .= "?" unless $question =~ /^.*\?$/;
    until ( $answer =~ /^y/i || $answer =~ /^n/i ) {
     print "\nWhat would the correct answer be for a(n) $animal?\n";
     chomp ($answer = <>);
    }
    if ( $answer =~ /^y/i ) {
     $brain{$x + 1,$y * 2 - 1} = "G: " . $animal;
     $brain{$x + 1,$y * 2} = "G: " . $entry;    
    }
    elsif ( $answer =~ /^n/i ) {
     $brain{$x + 1,$y * 2} = "G: " . $animal;
     $brain{$x + 1,$y * 2 - 1} = "G: " . $entry;   
    }
    $brain{$x,$y} = "Q: " . $question;
   }
   $response = "";
   until ( $response =~ /^y/i || $response =~ /^n/i ) {
    print "Would you like to play again?\n";
    chomp ($response = <>);
   }
   $loop = 1 if $response =~ /^n/i;
  }
 }
}
untie %brain;

sub animals_learned {
 return grep /^G: / , %brain;
}

sub create_new {
 $brain{1,1} = "Q: Does your animal live in the water";
 $brain{2,1} = "Q: Is your animal a mammal?";
 $brain{2,2} = "Q: Can your animal fly?";
 $brain{3,1} = "Q: Does your animal get caught in Tuna nets?";
 $brain{3,2} = "Q: Does your animal have tentacles?";
 $brain{3,3} = "Q: Is your animal nocturnal?";
 $brain{3,4} = "Q: Can your animal be domesticated as a house pet?";
 $brain{4,1} = "G: dolphin";
 $brain{4,2} = "G: whale";
 $brain{4,3} = "G: octapus";
 $brain{4,4} = "G: shark";
 $brain{4,5} = "G: bat";
 $brain{4,6} = "G: parrot";
 $brain{4,7} = "G: dog";
 $brain{4,8} = "G: lion";
}

Comment on AI Animals
Download Code
Re: AI Animals
by Django (Pilgrim) on Sep 08, 2002 at 10:45 UTC

    Nice one!
    The "-T" option produced on ActivePerl 5.6.1 (win32) the following error:

    Too late for "-T" option at D:\...\ai_animals.pl line 1.
    It worked fine however after removing that.
    untie %brain; #LOL

    ~Django
    "Why don't we ever challenge the spherical earth theory?"

      Your choice, but you could have left the -T option on the shebang line by running it this way:

      D:\<some directory>\>perl -T ai_animals.pl

Re: AI Animals
by Limbic~Region (Chancellor) on Sep 08, 2002 at 16:29 UTC
    Ok, so a few people have mentioned that they remember playing the same game I got the idea from back in the day on an Apple something or other. Zaxo thinks he might even have the original source code in a book some place. Other people have asked how the logic tree works.

    1,1 /\ / \ Y / \ N / \ 2,1 2,2 /\ / \ Y / \ N Y / \ N / \ / \ 3,1 3,2 3,3 3,4

    It starts with a root question at ($x = 1,$y = 1) (it is very important to pick a good root question because you are eliminating half of the possibilities every time you answer a question. To get to the next entry in the logic tree, you need only determine if the answer was yes or no. $x always increases by 1, and $y either becomes 2 * $y or (2 * $y) - 1. You continue through the logic tree until you find a Guess and not another Question. It is then a simple matter of modifying the logic tree with the new information to get "smarter".

    I want to remind everyone that my code is a guess as to what the original looks like. It was many years ago, and I am sure it could be much cleaner. Some ideas that I had are:

  • Improve the I/O interface
  • Improve the regex's to ensure proper capitalization
  • Check dict if possible for typos on animals Anyone wanting to play around with these or your own ideas are more than welcome. Improvement on the logic tree would greatly be appreciated.

    Thanks,

    Limbic~Region

Re: AI Animals
by hossman (Prior) on Sep 08, 2002 at 20:32 UTC
    This game is acctually older then the Apple, I don't know who wrote the orriginal version, but the most famous is probably the 1975 UNIVAC version written by "John Walker", it included a sub routine called PERVADE that caused it to be self replicating...

    http://www.fourmilab.ch/documents/univac/animal.html

      major ++ - see my reply to jaldhar.

      Thanks!

      Thanks for linking this. Though I've never met Walker, I was introduced to ANIMAL around 1977 on a Univac 1108. That was after the PERVADE subroutine quit working due to the release of a new version of the Exec-8 operating system. I was working then as an 1108 FORTRAN programmer, and learning 1108 assembly. Walker's code was a great example to learn from.

      I suspect the earliest versions were written in the '50s when Claude Shannon and David Huffman were working on compression algorithms.

      P.S. The reason ANIMAL and PERVADE are in all caps is that the 1108 used a character set called FIELDATA http://bit.ly/10w9dwq wherein a 36 bit byte, or word comprised six 6 bit characters...no lower case. We thought in octal, not hexadecimal.

      When they made ASCII available, there were 4 9-bit bytes to the word

Re: AI Animals
by jaldhar (Vicar) on Sep 09, 2002 at 04:38 UTC

    The proper name of this game is "Pangolins." Back when I was about 9 (so this would be around 1980) I was deemed one of the most gifted and talented students in Essex.


    (Pauses to bask in your adoration.)


    Anyway so I was sent to this camp to mingle with other superior individuals and that's where I saw my first computer up close. It was a Commodore PET with a whopping 8k of RAM and it was playing this game. It's uncanny ability to guess the animal I was thinking of totally scared the hell out of me. How long before such powerful AIs would enslave the human race?

    --
    જલધર

      Thanks - what I discovered was that there have been numerous variations on the game, but they all seem to have their roots in ANIMALS mentioned by hossman. Every example I could find out there today uses the same tree structure that I independently came up with. Though the original ANIMALS was written in UNIVAC assembly, it is head and shoulders above anything that came after it. Of course, that was probably due to its auther - John Walker. In any account, his assembly has given me some ideas, though I am not sure it is worth the time an effort. My goal was to replicate the functionality of the game I played as a kid (which I did), not that of a UNIVAC assembly programmer (which appears to be way cooler).

      Thanks for the input and congrats on a prestigious child hood!

Re: AI Animals
by Mr. Muskrat (Abbot) on Sep 09, 2002 at 17:51 UTC

    ++

    Hmm, I thought I was the one that wrote the Apple II version!
    ;)
    Okay, so I saw a version of it at school and had no way of getting it home so I wrote my own. Too bad I no longer have my source code. This really brings back some nice memories.

Re: AI Animals
by rattusillegitimus (Friar) on Sep 09, 2002 at 21:19 UTC

    Serious ++ for some happy childhood memories. ;) This is one of the programs I tended to write in every programming language I learned when I was younger (along with an AD&D character generator, though I never actually finished any of those).

    I've been thinking more and more about little programs I might be able to write that would entertain my daughter. I don't know if she's quite old enough to try this one out, even with me reading the questions to her, but I think I'll give it a try soon. Thanks for an excellent starting point.

    --
    Does it leave a sinking ship? (Y|N): Y
    Is its parentage uncertain? (Y|N): Y
    Are you thinking of rattusillegitimus? (Y|N): Y

Re: AI Animals
by merlyn (Sage) on Sep 24, 2002 at 15:59 UTC
Re: AI Animals
by fglock (Vicar) on Sep 24, 2002 at 16:52 UTC
(jcwren) Re: AI Animals
by jcwren (Prior) on Sep 24, 2002 at 17:00 UTC

    Technically, it's not an AI, but an expert system. At the shop I worked at, we created a database of about 800 animals for it. I "ported" a copy of the Apple code to the CP/M systems, where we had some real disk storage. We also created variations on it for physical objects, etc.

    --Chris

    e-mail jcwren

      Expert systems are really a subset of AI as are learning algorithms. Learning algorithms may be "cooler" AI but I'd guess that people get more done with expert systems.

      Speaking of learning systems, it would be quite easy to change this program to utilize a decision tree (see ISBN 0070428077, chap. 3 for a good reference).

      To do this, one would need to create a set of examples of the form
      Dolphin 1 0 1 0 0 0
      although I'd add some more animals and attributes to make this interesting. You need a number of examples per animal. The dtree algorithm uses the examples to build a tree and decides which attributes are most important and moves those attributes towards the top of the tree. It might place the same question in more than one place and might ignore questions in certain subtrees. For example, knowing that an animal gets stuck in tuna nets narrows the field down quite a bit.

      Of course, DTree's tend to be used for more broad classifications, like deciding the party of a senator based on her voting record.

Seen that in Corel
by ambrus (Abbot) on Nov 18, 2003 at 19:30 UTC

    This game is also an example script in CorelDraw 8.

    CorelDraw includes a Basic-based scripting language, named CorelScript. It is also used with other Corel apps. This language is not object-oriented. Scripts can be compiled to windows exe. It is well-thought and documented well, which is a great advantage in my eyes. It is of course inferior to Perl. There are a few examples included which does not use CorelDraw itself, just the core script language. One of these is animals.csc the animals game, dialog-based. It can save the tree to file.

Re: AI Animals
by Anonymous Monk on Jan 19, 2009 at 12:31 UTC
    Very Nice! This game was given to us when I was at university, as a task to program in a functional programming language, prolog. A little while ago I knocked up a version using ajax and the google app engine. its up at http://nicappleby.za.net nic

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://195991]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-10-02 09:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (52 votes), past polls