0: #!/usr/bin/perl -w
1: use strict;
2: ## I originally wrote this for a column,
3: ## but haven't gotten around to using it yet.
4: ## just think of an animal, and invoke it.
5: ## It's an example of a self-learning game.
6: ## When you choose not to continue, it'll dump out
7: ## the data structure of knowledge it has accumulated.
8:
9: use Data::Dumper;
10:
11: my $info = "dog";
12:
13: {
14: try($info);
15: redo if (yes("play again?"));
16: }
17: print "Bye!\n";
18: print Dumper($info);
19:
20: sub try {
21: my $this = $_[0];
22: if (ref $this) {
23: return try($this->{yes($this->{Question}) ? 'Yes' : 'No' });
24: }
25: if (yes("Is it a $this")) {
26: print "I got it!\n";
27: return 1;
28: };
29: print "no!? What was it then? ";
30: chomp(my $new = <STDIN>);
31: print "And a question that distinguishes a $this from a $new would be? ";
32: chomp(my $question = <STDIN>);
33: my $yes = yes("And for a $new, the answer would be...");
34: $_[0] = {
35: Question => $question,
36: Yes => $yes ? $new : $this,
37: No => $yes ? $this : $new,
38: };
39: return 0;
40: }
41:
42: sub yes {
43: print "@_ (yes/no)?";
44: <STDIN> =~ /^y/i;
45: }
In reply to "animal"
by merlyn
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|