http://qs1969.pair.com?node_id=200391

   1: #!/usr/bin/perl -w 
   2: use strict; 
   3: ## I originally wrote this for a column,
   4: ## but haven't gotten around to using it yet.
   5: ## just think of an animal, and invoke it.
   6: ## It's an example of a self-learning game.
   7: ## When you choose not to continue, it'll dump out
   8: ## the data structure of knowledge it has accumulated.
   9:  
  10: use Data::Dumper; 
  11:  
  12: my $info = "dog"; 
  13:  
  14: { 
  15:   try($info); 
  16:   redo if (yes("play again?")); 
  17: } 
  18: print "Bye!\n"; 
  19: print Dumper($info); 
  20:  
  21: sub try { 
  22:   my $this = $_[0]; 
  23:   if (ref $this) { 
  24:     return try($this->{yes($this->{Question}) ? 'Yes' : 'No' }); 
  25:   } 
  26:   if (yes("Is it a $this")) { 
  27:     print "I got it!\n"; 
  28:     return 1; 
  29:   }; 
  30:   print "no!?  What was it then? "; 
  31:   chomp(my $new = <STDIN>); 
  32:   print "And a question that distinguishes a $this from a $new would be? "; 
  33:   chomp(my $question = <STDIN>); 
  34:   my $yes = yes("And for a $new, the answer would be..."); 
  35:   $_[0] = { 
  36:            Question => $question, 
  37:            Yes => $yes ? $new : $this, 
  38:            No => $yes ? $this : $new, 
  39:           }; 
  40:   return 0; 
  41: } 
  42:  
  43: sub yes { 
  44:   print "@_ (yes/no)?"; 
  45:   <STDIN> =~ /^y/i; 
  46: } 

Replies are listed 'Best First'.
Re: "animal"
by jarich (Curate) on Sep 25, 2002 at 00:35 UTC
    This game was covered in an great golfing thread over here too. Of course, yours is much easier to read though.

    jarich

Re: "animal"
by bluto (Curate) on Sep 24, 2002 at 18:12 UTC
    Just curious: Inspired from the venerable "101 Basic Computer Games"? (sp?)

    I think I still have a copy or two of that laying around somewhere. (I guess that dates me pretty badly...)

    Update:Somehow I missed the other discussion over at AI Animals...

    bluto

Re: "animal"
by kryberg (Pilgrim) on Sep 25, 2002 at 20:24 UTC
    Nice example and fun to try. thanks.
Re: "animal"
by data67 (Monk) on Sep 26, 2002 at 22:21 UTC
    I wonder what might be some ways that we could store the collected data instead of just dumping it when we are done. I am sure that could be put together but what might be a elegant way.

      The print Dumper($info); bit already reproduces the data in a form Perl can parse. If dumped to a file, you can use do 'FILE' to read it back in.

      There are more sophisticated approaches of course, but this probably suffices here.

      Makeshifts last the longest.

Re: "animal"
by Anonymous Monk on Oct 01, 2002 at 06:36 UTC
    Funny!!