#!/usr/bin/perl my (%g, $w, $s); my $s = shift || 9; my $file = shift || '/usr/share/dict/words'; die "Number of guesses must be greater than 0\n" if ($s <= 0); open(F, $file) || die "Can't open that dictionary file\n"; my $count = `wc -l < $file`; chomp($count); $count =~ s/[ \t]*//g; $count = int(rand() * $count) + 1; while ($count > 0 || $w =~ /^[A-Z]/) { $w = ; seek(F, 0, 0) if (! $w); $count--; } chomp($w); while ($s >= 0) { my $x = length($w); print "\f"; print ">>>hangman<<<\n\n"; for (0..length($w)-1) { if (exists($g{substr($w, $_, 1)})) { print " " . substr($w, $_, 1) . " "; $x--; } else { print " _ "; } } if (! $x) { print "\n\nCongratulations!!! ;)\n\n"; exit; } my @g = sort keys %g; print "\n\n"; print "Incorrect guesses left: $s\n"; print " Already guessed: @g\n"; GUESS: print " Your guess: "; my $g = <>; $g = lc(substr($g, 0, 1)); goto GUESS unless ($g =~ /[a-z]/); $s-- if ($w !~ /$g/ && ! exists $g{$g}); $g{$g} = 1; } print " The word was: \"$w\"\n\n";