http://qs321.pair.com?node_id=281008


in reply to Hangmonks

This was a fun game. How about extending Games::Guessword to disallow the a..z guess cheat...

use strict; use XML::Simple; use LWP::UserAgent; use Games::GuessWord; package GamesGuesswordExtended; { use Games::GuessWord; use base 'Games::GuessWord'; sub new { my $class = shift(); my $monk = shift(); my $self = Games::GuessWord->new(words => [$monk]); bless($self,$class); return $self; } # 8-6-2003. sub guess can be completely replaced with # the 'sub guess' found in the following node
  #Re: Re: Hangmonks
sub guess { my $self = shift; my $word = shift; chop($word); if ($self->chances == 0) { return undef; } #orginally, '$letter' was made from the shift(). I #changed it to $word, and got '$letter' from the 1st #character in $word. my $letter = substr($word,0,1); #Don't want to type $self->secret() twice. my $secret = $self->secret(); push (@{$self->{guesses}}, $letter); #If you correctly guess the name, then push the #entire thing to guesses. push (@{$self->{guesses}}, $word) if ($secret eq $word); if ($secret =~ /$letter/){ $self->{score} += $self->chances + 1; } else { $self->{chances}--; } } } package main; my $ua = LWP::UserAgent->new(); my $response = $ua->get("http://www.perlmonks.org/index.pl?node_id=158 +51"); die $response->code() if $response->code() != 200; my $content = $response->content(); my $cblist = XMLin($content); my @user = map $_->{username}, @{$cblist->{user}}; my $monk = $user[rand @user]; #my $g = Games::GuessWord->new(words => [$monk]); my $g = new GamesGuesswordExtended($monk); # Uncomment out the following for testing print "Secret: " . $g->secret . "\n"; my @guesses = $g->guesses; do{ print "Guesses Remaining:" . $g->chances . "\n"; print "The Monk is: " . $g->answer . "\n"; print "What is your Guess?: "; my $response = <>; $g->guess($response); if ($g->won) { print "Yep, it was " . $g->secret . "...Great Guess!\n"; exit; } }until($g->chances<=0); print "Sorry, the monk was: " . $g->secret . "\n"
Updated: See comment dated 8-6-2003 in code near 'sub guess'

Kristofer Hoch

Si vos can lego is, vos es super erudio