mbgbioinfo has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w @words = qw( internet answers printer program ); @guesses=(); $wrong=0; $line=0; my @words = qw( internet answers printer program ); open my $fh, '>', "words.txt" or die "Cannot open words.txt: $!"; foreach (@words) { print $fh "$_\n"; #Because I want each word in a separate line +. chomp($fh); #maybe something wrong with chomp();? } close $fh; srand; open MYFILE, "<words.txt" or die "Could not open words.txt: $!\n"; rand($.)<1 and ($line=$_) while <MYFILE>; close MYFILE; $choice=$line; $hangman="0-|--<"; @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=<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!\n"; exit; } } print "$hangman\nSorry, the word was $choice.\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: hangman game
by marto (Cardinal) on May 18, 2015 at 11:51 UTC | |
|
Re: hangman game
by Athanasius (Archbishop) on May 18, 2015 at 12:59 UTC | |
by mbgbioinfo (Novice) on May 28, 2015 at 10:36 UTC | |
by Athanasius (Archbishop) on May 28, 2015 at 15:15 UTC | |
by mbgbioinfo (Novice) on Jun 06, 2015 at 19:12 UTC | |
|
Re: hangman game
by GotToBTru (Prior) on May 18, 2015 at 13:19 UTC | |
|
Re: hangman game
by ww (Archbishop) on May 18, 2015 at 16:22 UTC | |
|
Re: hangman game
by laosland (Initiate) on May 18, 2015 at 14:39 UTC | |
by toolic (Bishop) on May 18, 2015 at 14:51 UTC | |
by laosland (Initiate) on May 18, 2015 at 15:05 UTC | |
by QM (Parson) on May 19, 2015 at 09:04 UTC | |
by Athanasius (Archbishop) on May 19, 2015 at 10:03 UTC | |
by QM (Parson) on May 19, 2015 at 10:18 UTC | |
by soonix (Chancellor) on May 19, 2015 at 11:13 UTC | |
by QM (Parson) on May 19, 2015 at 12:15 UTC | |
by Happy-the-monk (Canon) on May 19, 2015 at 10:02 UTC | |
by QM (Parson) on May 19, 2015 at 10:08 UTC | |
by Ralesk (Pilgrim) on May 19, 2015 at 09:55 UTC | |
by QM (Parson) on May 19, 2015 at 09:57 UTC | |
|
Re: hangman game
by vinoth.ree (Monsignor) on May 18, 2015 at 13:30 UTC |