
msingh
New User
Aug 21, 2007, 6:18 AM
Post #1 of 1
(1433 views)
|
|
play animal guessing game
|
Can't Post
|
|
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)
|