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

"animal"

by merlyn (Sage)
 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

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!!

Login:
Password
remember me
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 examining the Monastery: (29)
GrandFather
talexb
jdporter
wfsp
runrig
johngg
atcroft
mr_mischief
LTjake
kennethk
herveus
MidLifeXis
Marshall
rowdog
psini
davies
SFLEX
jkva
ssandv
stefbv
wwe
umasuresh
AndyZaft
Neighbour
cmg
petecm99
DarthWavy
ldbpm
im2
As of 2010-09-02 15:10 GMT
Sections?
Seekers of Perl Wisdom
Cool Uses for Perl
Meditations
PerlMonks Discussion
Categorized Q&A
Tutorials
Obfuscated Code
Perl Poetry
Perl News
See About the sections of PerlMonks
Information?
PerlMonks FAQ
Guide to the Monastery
What's New at PerlMonks
Voting/Experience System
Tutorials
Reviews
Library
Perl FAQs
Other Info Sources
Find Nodes?
Nodes You Wrote
Super Search
List Nodes By Users
Newest Nodes
Recently Active Threads
Selected Best Nodes
Best Nodes
Worst Nodes
Saints in our Book
Leftovers?
The St. Larry Wall Shrine
Offering Plate
Awards
Craft
Snippets Section
Code Catacombs
Quests
Editor Requests
Buy PerlMonks Gear
PerlMonks Merchandise
Planet Perl
Perlsphere
Use Perl
Perl.com
Perl 5 Wiki
Perl Jobs
Perl Mongers
Perl Directory
Perl documentation
CPAN
Random Node
Voting Booth?

My favourite poll on PerlMonks is ...

Your first Perl Book - the first one ever
Average number of caffeinated beverages per work day - the poll with the highest participation
My Thoughts on the New Voting/Experience System - the poll with the fewest votes cast
When I grow up, I want to be: - one of the polls with the fewest options
Perl 6 will primarily be: - the first one on Perl6
When I see a poll - one of the many polls about polls
this poll ;-)
yet to come
none - I hate polls. Bah.
some other

Results (49 votes), past polls