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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.