CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
play animal guessing game

 



msingh
New User

Aug 21, 2007, 6:18 AM

Post #1 of 1 (1725 views)
play animal guessing game Can't Post


Code
use strict; 
sub yes {
print shift;
my $ans = <STDIN>;
return $ans =~ /^[yY]/;
}

sub animal {
my $tree = { cargo=>'bird', left=>'', right=>'' };
# loop until the user quits
while (1) {
last unless yes "\nAre you thinking of an animal? ";

# walk the tree
while($tree->{left}) {
if (yes "$tree->{cargo} ? " ) { $tree = $tree->{right}; }
else { $tree = $tree->{left}; }
}

# make a guess
my $guess = $tree->{cargo};
if (yes "Is it a $guess? ") {
print "I rule!";
next;
}

# get new information
print "What is the animal's name? ";
my $animal = <STDIN>;
chomp($animal);
print "What question would distinguish a $animal from a $guess? ";
my $question = <STDIN>;
chomp($question);

# add new information to the tree
$tree->{cargo} = $question;
if (yes "If the animal were $animal the answer would be? ") {
$tree->{left} = { cargo=>$guess, left=>'', right=>'' };
$tree->{right} = { cargo=>$animal, left=>'', right=>'' };
}
else {
$tree->{left} = { cargo=>$animal, left=>'', right=>'' };
$tree->{right} = { cargo=>$guess, left=>'', right=>'' };
}

}
}
animal;



(This post was edited by msingh on Aug 21, 2007, 6:38 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives