#!/usr/bin/perl -Tw
use strict;
use AnyDBM_File;
use Fcntl;
my $dbfile = "animals";
tie (my %brain, "AnyDBM_File", $dbfile, O_CREAT|O_RDWR, 0600) or die "
+Can't open $dbfile: $!\n";
&create_new unless keys %brain;
my $loop;
until ($loop) {
print "Hello, my name is AI! I love animals and guessing games.\n";
print "I have currently learned " . &animals_learned . " animals.\n";
print "\nPlease think of an animal and I will try to guess it - press
+ enter when ready\n";
<>;
my $x = my $y = my $found = 1;
while ($found) {
my $entry = my $response = my $animal = my $question = my $answer =
+0;
($entry = $brain{$x,$y}) =~ s/(^[QG]): //;
if ( $1 eq "Q" ) {
until ( $response =~ /^y/i || $response =~ /^n/i ) {
print "$entry\n";
chomp ($response = <>);
}
if ( $response =~ /^y/i ) {
$y = $y * 2 - 1;
++$x;
}
elsif ($response =~ /^n/i) {
$y *= 2;
++$x;
}
}
elsif ( $1 eq "G" ) {
$found = 0;
until ( $response =~ /^y/i || $response =~ /^n/i ) {
print "Is your animal a(n) $entry?\n";
chomp ($response = <>);
}
if ( $response =~ /^y/i ) {
print "\nYeah, I got it right!\n";
}
elsif ($response =~ /^n/i) {
until ( $animal ) {
print "OK, I give up, what animal were you thinking of?\n";
chomp ($animal = <>);
}
$animal =~ s/a[n]* //i;
until ( $question ) {
print "\nEnter a yes/no question to tell the difference between a
+(n) $animal and a(n) $entry?\n";
chomp ($question = <>);
}
$question .= "?" unless $question =~ /^.*\?$/;
until ( $answer =~ /^y/i || $answer =~ /^n/i ) {
print "\nWhat would the correct answer be for a(n) $animal?\n";
chomp ($answer = <>);
}
if ( $answer =~ /^y/i ) {
$brain{$x + 1,$y * 2 - 1} = "G: " . $animal;
$brain{$x + 1,$y * 2} = "G: " . $entry;
}
elsif ( $answer =~ /^n/i ) {
$brain{$x + 1,$y * 2} = "G: " . $animal;
$brain{$x + 1,$y * 2 - 1} = "G: " . $entry;
}
$brain{$x,$y} = "Q: " . $question;
}
$response = "";
until ( $response =~ /^y/i || $response =~ /^n/i ) {
print "Would you like to play again?\n";
chomp ($response = <>);
}
$loop = 1 if $response =~ /^n/i;
}
}
}
untie %brain;
sub animals_learned {
return grep /^G: / , %brain;
}
sub create_new {
$brain{1,1} = "Q: Does your animal live in the water";
$brain{2,1} = "Q: Is your animal a mammal?";
$brain{2,2} = "Q: Can your animal fly?";
$brain{3,1} = "Q: Does your animal get caught in Tuna nets?";
$brain{3,2} = "Q: Does your animal have tentacles?";
$brain{3,3} = "Q: Is your animal nocturnal?";
$brain{3,4} = "Q: Can your animal be domesticated as a house pet?";
$brain{4,1} = "G: dolphin";
$brain{4,2} = "G: whale";
$brain{4,3} = "G: octapus";
$brain{4,4} = "G: shark";
$brain{4,5} = "G: bat";
$brain{4,6} = "G: parrot";
$brain{4,7} = "G: dog";
$brain{4,8} = "G: lion";
}
-
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.