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: }
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |