<?xml version="1.0" encoding="windows-1252"?>
<node id="967477" title="Re: HangMan Question2.." created="2012-04-26 18:20:33" updated="2012-04-26 18:20:33">
<type id="11">
note</type>
<author id="892462">
Riales</author>
<data>
<field name="doctext">
&lt;p&gt;Sounds like you just need to keep a count of how many guesses the user inputs. Increment the count each time you get do &lt;code&gt;my $guess=&lt;STDIN&gt;;&lt;/code&gt; and it'll reflect the number of letters the user guessed.&lt;/p&gt;

&lt;code&gt;
#!/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-|--&lt;";

my @letters=split(//, $choice);
my @hangman=split(//, $hangman);
my @blankword=(0) x scalar(@hangman);

my $num_guesses = 0;

    while ($wrong &lt; 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=&lt;STDIN&gt;;
        chomp $guess;
        my $right=0;
        for (my $i=0; $i&lt;@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 $num_guesses guesses!\n";
                exit;
        }
    }
    
print "$hangman\nBetter luck next time, the word was '$choice'.\n";
&lt;/code&gt;</field>
<field name="root_node">
967466</field>
<field name="parent_node">
967466</field>
</data>
</node>
