Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling


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.
  10: use Data::Dumper; 
  12: my $info = "dog"; 
  14: { 
  15:   try($info); 
  16:   redo if (yes("play again?")); 
  17: } 
  18: print "Bye!\n"; 
  19: print Dumper($info); 
  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: } 
  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.


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


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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlcraft [id://200391]
Front-paged by TStanley
[thezip]: G'day all. Apparently Strawberry Perl has issues with deploying Spreadsheet::XLSX for Perl 5.24.1. I need this module to maintain compatability with an existing script. I'm new to Strawberry -- is there a safe way to manually deploy this?
[MidLifeXis]: Where is it hanging up?
[MidLifeXis]: ISTR having a couple of issue based on undeclared dependencies around the time I was looking at that module on my last project, but cannot say if that was the module that I had the dependency issues with.
[MidLifeXis]: (5.22 at that time)
[MidLifeXis]: Yeah, I know, really helpful :-)
[Corion]: If all else fails, Spreadsheet::XLSX should be "installable" by manually copying the files, provided that the prerequisites are met
[thezip]: It fails in the "formatted 2-digit numeric" tests

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (14)
As of 2017-03-23 17:12 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (290 votes). Check out past polls.