convenientstore has asked for the wisdom of the Perl Monks concerning the following question:
Say @letters = n o w, and iterating over 0..$#letters would assign i to $letters[0]=n $letters1=o $letters2=w and @blankword was 0 0 0, how is $blankword\$i\ is one of n o w ???foreach $i (0..$#letters) { if ($blankword[$i]) { print $blankword[$i]; } else { print "-"; }
$guesses[@guesses]=$guess;
#!/usr/bin/perl -w @words = qw( internet answers printer program ); @guesses=(); $wrong=0; $choice=$words[rand @words]; $hangman="0-|--<"; @letters=split(//, $choice); @hangman=split(//, $hangman); @blankword=(0) x scalar(@letters); OUTER: while ($wrong<@hangman) { foreach $i (0..$#letters) { if ($blankword[$i]) { print $blankword[$i]; } else { print "-"; } } print "\n"; if ($wrong) { print @hangman[0..$wrong-1] } print "\n Your Guess: "; $guess=<STDIN>; chomp $guess; foreach(@guesses) { next OUTER if ($_ eq $guess); } $guesses[@guesses]=$guess; $right=0; for ($i=0; $i<@letters; $i++) { if ($letters[$i] eq $guess) { $blankword[$i]=$guess; $right=1; } } $wrong++ if (not $right); if (join('', @blankword) eq $choice) { print "You got it right!\n"; exit; } } print "$hangman\nSorry, the word was $choice.\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: hangman question
by GrandFather (Saint) on Jul 15, 2007 at 23:25 UTC | |
by convenientstore (Pilgrim) on Jul 16, 2007 at 00:37 UTC | |
by GrandFather (Saint) on Jul 16, 2007 at 01:46 UTC | |
by convenientstore (Pilgrim) on Jul 16, 2007 at 02:00 UTC | |
by ysth (Canon) on Jul 16, 2007 at 02:46 UTC | |
| |
|
Re: hangman question
by ysth (Canon) on Jul 16, 2007 at 00:40 UTC | |
by Anno (Deacon) on Jul 16, 2007 at 11:13 UTC |