#!/usr/bin/perl -w open (DICT, "/Users/programming/dictionary.txt"); @words=; @guesses=(); $wrong=0; $choice=$words[rand @words]; $hangman=" O\n,/,|,\\,\n/, \\"; @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=; chomp $guess; foreach(@guesses) { next OUTER if ($_ eq $guess);p } $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"; print "the word was $choice!\n"; exit; } } @hangman=join('', @hangman); print "@hangman\nSorry, the word was $choice. \n"; #### #!/usr/bin/perl -w @words=qw(printer internet); @guesses=(); $wrong=0; $choice=$words[rand @words]; $hangman=" O\n,/,|,\\,\n/, \\"; @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=; chomp $guess; foreach(@guesses) { next OUTER if ($_ eq $guess);p } $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"; print "the word was $choice!\n"; exit; } } @hangman=join('', @hangman); print "@hangman\nSorry, the word was $choice. \n";