Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Hangmonks

by krisahoch (Deacon)
on Aug 05, 2003 at 15:00 UTC ( #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("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

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2022-05-25 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (84 votes). Check out past polls.

    Notices?