Your skill will accomplishwhat the force of many cannot PerlMonks

### Re: HangMan Question2..

by Riales (Hermit)
 on Apr 26, 2012 at 22:20 UTC ( #967477=note: print w/replies, xml ) Need Help??

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];
}
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";

Create A New User
Node Status?
node history
Node Type: note [id://967477]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (13)
As of 2017-07-26 07:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I came, I saw, I ...

Results (385 votes). Check out past polls.