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 | |
|
Re: "animal"
by bluto (Curate) on Sep 24, 2002 at 18:12 UTC | |
|
Re: "animal"
by kryberg (Pilgrim) on Sep 25, 2002 at 20:24 UTC | |
|
Re: "animal"
by data67 (Monk) on Sep 26, 2002 at 22:21 UTC | |
by Aristotle (Chancellor) on Sep 26, 2002 at 23:01 UTC | |
|
Re: "animal"
by Anonymous Monk on Oct 01, 2002 at 06:36 UTC |