Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Hangmonks

by krisahoch (Deacon)
on Aug 05, 2003 at 15:00 UTC ( [id://281008]=note: print w/replies, xml ) Need Help??

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(" +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

Replies are listed 'Best First'.
Re: Re: Hangmonks
by krisahoch (Deacon) on Aug 05, 2003 at 15:11 UTC

    Additionally, I could use the super way of doing things...

    #This function replaces GamesGuesswordExtend's #guess method. sub guess { my $self = shift(); my $word = shift(); chop($word); push (@{$self->{guesses}}, $word) if ($self->secret eq $word); $self->SUPER::guess(substr($word,0,1)); }
    Update: Condensed the code, just for fun

    Kristofer Hoch

    Si vos can lego is, vos es super erudio

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://281008]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-25 13:49 GMT
Find Nodes?
    Voting Booth?

    No recent polls found