Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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]; } 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";

In reply to Re: HangMan Question2.. by Riales
in thread HangMan Question2.. by 300zxmuro

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-19 02:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found