#!/usr/local/bin/perl -w use strict; $|=1; # www.PerlMonks.org 140734|(Golf) The Animal Game # ask the question passed, return true or false sub a($) { my ($qu, $ans) = (shift); while(!$ans) { print "\n$qu?\n"; chomp ($ans = ) } $ans =~ m/y/io ? 1 : 0; }; # ask if animal is passed value, return true or false sub g($) { my ($ani, $ans) = (shift); while(!$ans) { print "\nis animal $ani?\n"; chomp ($ans = ) } $ans =~ m/^y/io ? 1 : 0; }; # ask what animal, return user input sub t() { my $ans; while(!$ans) { print "\nwhat animal?\n"; chomp ($ans = ) } $ans; }; # get question to identify animal: # true for new, false for old; return question sub i($$) { my ($old, $new, $qu) = (shift, shift); while(!$qu) { print "\nenter a question false for $old but true for $new:\n"; chomp ($qu = ); } $qu; }; # ask to continue, return true or false sub w() { a('play') ? 1 : exit 0 }; # question/answer data structure: hash of arrays my %q = ( "larger than a breadbox" => ['cat', 'giraffe'], ); no strict; while(w){($k,$v)=each%q;$k||redo;$u=$$v[a($k)];next if(g($u));while(($l,$x)=each%q){next unless$$x[0] eq $u;a($l)?$y=$$x[1]:goto N;goto E if(g($y))}N:$b=i($u,$c=t);$q{$b}=[$u,$c];E:}