All pastes #58317 Raw Edit

Something

public text v1 · immutable
#58317 ·published 2006-05-23 09:43 UTC
rendered paste body
#!/usr/local/bin/perl

@words=qw( internet answers printer program );
@guesses=();
$wrong=0;

$choice=$words[rand @words];
$hangman="0\n|\n-\n|\n|\n^";

@letters=split(//, $choice);
@hangman=split(/\n/, $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 join("\n",@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!\nThe word was $choice\n";
         exit;
      }
 }
 print "$hangman\nSorry, the word was $choice.\n";