#!/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); 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=; 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); if (join('', @blankword) eq $choice) { print "The word is '$choice'... You got it right!\n"; exit; } } print "$hangman\nBetter luck next time, the word was '$choice'.\n";