|
This is my first
week using Perl, so my solution may be a little longer and more
complicated than it really needs to be.
Zack
Medlin
#!/usr/bin/perl
-w
open( WORDS, $ARGV[
0 ] ); chomp( @words = <WORDS> ); $answer = ${words}[ rand($#{words}
)]; $word_len = length( $answer ); for( $i=0; $i< $word_len ; $i++ )
{ $clue[ $i ] = '_
'; } print "\n\n\t\t\tHANGMAN\n\n\n"; print "Guess letters to try and
fill in the word. Guess right and you get to guess again\n"; print
"Guess wrong and you get a strike. ", $ARGV[ 1 ], " strikes and you are
DEAD!\n\n"; $num = 0; $guess = ""; @old =
('','1','2','3','4','5','6','7','8','9','0','`','~','!','@','#','$','%','^','&','*','(',')',' ; while(
$num < $ARGV[ 1 ] ) { GUESS:
if( $answer eq $guess )
{
print "\t", $answer, "\n\n\tLIVE!\nYou Guessed Right, You
WIN!\n";
exit;
} print "Turn#", $num + 1,
":\n"; print "\t",@clue,
"\n"; print "\tEnter
Letter\n"; chomp( $letter =
<STDIN> ); $guess_len =
length( $letter ); if( $guess_len
!= 1 )
{
print "Guess One Letter
Please\n";
goto GUESS;
} for( $i=0; $i < $#{old}; $i++
)
{
if( $letter eq $old[ $i ]
){
print "You Either Allready Guessed That Letter or it is Not a Valid
Guess.\n
goto
GUESS;
}
}
unshift @old, $letter; while (
$answer =~ m/$letter/g )
{
$clue[ pos( $answer ) -1 ] =
$letter;
for( $guess = "", $i=0; $i < $#clue + 1; $i++
){
$guess .= $clue[ $i
];
}
} if( $answer =~ m/$letter/ ) {
goto GUESS; }
$num++; } print "\n\n\tDEAD!\n You Lose!\n The answer was ", $answer,
"\n"; exit;
|