Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

HangMan Question2..

by 300zxmuro (Initiate)
on Apr 26, 2012 at 21:26 UTC ( #967466=perlquestion: print w/ replies, xml ) Need Help??
300zxmuro has asked for the wisdom of the Perl Monks concerning the following question:

Hello AGAIN! almighty Monks! Hope all is well with you and your brain because mine is burning with a simple question. I have this hangman game. I need to know what to ADD to the code to have at the end the number of attempts the gamer used to solve the puzzle. Thanks a lot! Here is the code:

#!/usr/bin/perl -w # hangman.pl # use strict; use warnings; my @words=qw( portion answers printer program ); my @guesses=(); my $wrong=0; my $choice=$words[rand @words]; my $hangman="0-|--<"; my @letters=split(//, $choice); my @hangman=split(//, $hangman); my @blankword=(0) x scalar(@hangman); while ($wrong < scalar(@hangman)) { foreach my $i (0..$#letters) { if ($blankword[$i]) { print $blankword[$i]; } else { print "-"; } } print "\n"; if ($wrong) { print @hangman[0..$wrong-1]; } print "\n Your Guess: "; my $guess=<STDIN>; chomp $guess; my $right=0; for (my $i=0; $i<@letters; $i++) { if ($letters[$i] eq $guess) { $blankword[$i]=$guess; $right=1; } } $wrong++ unless($right); if (join('', @blankword) eq $choice) { print "The word is '$choice'... You got it right!\n"; exit; } } print "$hangman\nBetter luck next time, the word was '$choice'.\n";

Comment on HangMan Question2..
Download Code
Re: HangMan Question2..
by snape (Pilgrim) on Apr 26, 2012 at 21:42 UTC

    Your program is working fine. I don't understand what do you mean by : "at the end the number of attempts the gamer used to solve the puzzle" Update: sorry I didn't understand the question and Riales code (below) works.

Re: HangMan Question2..
by Riales (Hermit) on Apr 26, 2012 at 22:20 UTC

    Sounds like you just need to keep a count of how many guesses the user inputs. Increment the count each time you get do my $guess=<STDIN>; and it'll reflect the number of letters the user guessed.

    #!/usr/bin/perl -w # hangman.pl # use strict; use warnings; my @words=qw( portion answers printer program ); my @guesses=(); my $wrong=0; my $choice=$words[rand @words]; my $hangman="0-|--<"; my @letters=split(//, $choice); my @hangman=split(//, $hangman); my @blankword=(0) x scalar(@hangman); my $num_guesses = 0; while ($wrong < scalar(@hangman)) { foreach my $i (0..$#letters) { if ($blankword[$i]) { print $blankword[$i]; } else { print "-"; } } print "\n"; if ($wrong) { print @hangman[0..$wrong-1]; } print "\n Your Guess: "; my $guess=<STDIN>; chomp $guess; my $right=0; for (my $i=0; $i<@letters; $i++) { if ($letters[$i] eq $guess) { $blankword[$i]=$guess; $right=1; } } $wrong++ unless($right); $num_guesses++; if (join('', @blankword) eq $choice) { print "The word is '$choice'... You got it right in $n +um_guesses guesses!\n"; exit; } } print "$hangman\nBetter luck next time, the word was '$choice'.\n";
Re: HangMan Question2..
by choroba (Abbot) on Apr 26, 2012 at 22:21 UTC
    Just declare $attempts at the beginning and set it to zero. Then increment it each time you read a guess. At the end, print it out.
Re: HangMan Question2..
by bobdabuilda (Sexton) on Apr 26, 2012 at 23:04 UTC

    You could also take a look at hangmonk for some ideas on other ways of doing certain parts of the code.

    Otherwise I guess you could ask the author of the code you're using how they'd have done it - if you knew enough to write the above, you'd certainly know how to declare and increment a variable...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://967466]
Approved by Argel
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2014-08-22 03:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (146 votes), past polls