Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

"animal"

by merlyn (Sage)
on Sep 24, 2002 at 15:20 UTC ( #200391=perlcraft: print w/ replies, xml ) Need Help??

   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: } 

Comment on "animal"
Download Code
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 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 kryberg (Pilgrim) on Sep 25, 2002 at 20:24 UTC
    Nice example and fun to try. thanks.
Re: "animal"
by data (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!!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlcraft [id://200391]
Front-paged by TStanley
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2014-10-31 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (219 votes), past polls