Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re (tilly) 1: (Golf) The Animal Game

by tilly (Archbishop)
on Jan 23, 2002 at 02:21 UTC ( #140748=note: print w/replies, xml ) Need Help??


in reply to (Golf) The Animal Game

117 116 117. But with caveats.

The main caveat is that I can add or lose characters depending on exact behaviour of these additional functions. (Particularly important is the behaviour of "a". I assumed that it returned 0,1 and added returns on prints but not a ?.) Here are the exact functions as I used them for my solution:

sub a { print @_, "\n"; my $r = <STDIN>; if ($r =~ /y/i) { return 1; } elsif ($r =~ /n/i) { return 0; } else { print "Sorry, I don't understand that. Please answer Y/N.\n"; a(@_); } } sub t { print "What is your animal?\n"; my $ans = <STDIN>; chomp($ans); return $ans; } sub i { my ($guessed, $user_answer) = @_; chomp($guessed, $user_answer); print "Please enter a question that would be true for a " . "$user_answer, but false for a $guessed:\n"; my $ans = <STDIN>; chomp($ans); return $ans; } sub w { if (a("\nDo you want to continue?")) { print "\nStarting a new game.\n"; } else { print "BYE!\n"; exit(); } }
Add to that my one free initialization:
$Q{a}="golfer";
And then my solution:
# 1 2 3 4 5 6 +7 8 9 10 11 #123456789_123456789_123456789_123456789_123456789_123456789_123456789 +_123456789_123456789_123456789_123456789_1234567 {*q=\%Q;*q=$q{a$q{q}}while$q{q};$q{0}{a}=$q{a},$q{1}{a}=t,$q{q}=i($q{a +},$q{1}{a})if!a"Is your animal a $q{a}?";w,redo} # Was #{*q=*Q;*q=$q{a$q{q}}while$q{q};$q{0}{a}=$q{a},$q{1}{a}=t,$q{q}=i($q{a +},$q{1}{a})if!a"Is your animal a $q{a}?";w&&redo}
UPDATE
jarich is right, I forgot to test something. My first typeglob assignment was wrong

Replies are listed 'Best First'.
Re: Re (tilly) 1: (Golf) The Animal Game
by jarich (Curate) on Jan 23, 2002 at 05:36 UTC
    Nice solution, but it really gets thrown out if you pick the same two animals in alternation.
    Starting a new game. does your animal have feathers? y Is your animal a chicken? n What is your animal? sparrow Please enter a question that would be true for a sparrow, but false fo +r a chicken: is your animal smaller than your fist? Do you want to continue? y Starting a new game. is your animal smaller than your fist? n Is your animal a chicken? y Do you want to continue? y Starting a new game. Is your animal a chicken? n What is your animal? sparrow Please enter a question that would be true for a sparrow, but false fo +r a chicken: is your animal smaller than your fist? Do you want to continue? y
    In fact I can never get it to filter down beyond one quesiton. Surely it's trying to do a sort and should look like:
    does your animal have fur? y is your animal smaller than your fist? y is your animal a mouse? y Do you want to continue?
    but I can't get beyond:
    the last question you answered: true -> your last animal false -> computer's previous guess.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://140748]
help
Chatterbox?
[Cosmic37]: note that the two files only have datetimes which may match whereas other data per line is different format in file1 and file2 - is that really intersection?
[jedikaiti]: Hi Monks
[Corion]: Cosmic37: Well, if you want to use only parts of a line for the key, see split or whatever other mechanism to extract the key from the line
[Corion]: Hi jedikaiti!
[Cosmic37]: is there a webpage for full CB for this chat rather than side panel chat?
[Cosmic37]: thank you for advice
[Cosmic37]: peach greets jedi
[Cosmic37]: ok this one works better fullpage chat
[Cosmic37]: test
[Cosmic37]: sort of anyway... hurls some peaches... :-D

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2017-06-29 16:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (672 votes). Check out past polls.