Skip to content

Commit 469f392

Browse files
authored
Merge pull request #416 from mknos/hangman-words
hangman versus small file
2 parents 3d28e3d + ba492e7 commit 469f392

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

bin/hangman

+11-4
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,18 @@ print "\nTHANKS FOR PLAYING!\n";
7272

7373
sub get_a_word {
7474
my $wordlist = "wordlist.txt";
75-
my( $word );
75+
my( $fh, $word );
7676
my( $line_num ) = 1;
7777
my( $random ) = rand();
78-
my( $val ) = int( $random * 9151 ) + 1;
79-
open my $fh, '<', $wordlist or die "Could not open <$wordlist>: $!\n";
78+
79+
open($fh, '<', $wordlist) or die "Could not open <$wordlist>: $!\n";
80+
my $lines = 0;
81+
$lines++ while (<$fh>);
82+
my $val = int($random * $lines) + 1;
83+
84+
close($fh) or die "Could not close <$wordlist>: $!\n";
85+
open($fh, '<', $wordlist) or die "Could not open <$wordlist>: $!\n";
86+
8087
while( <$fh> ) {
8188
if( $line_num == $val ) {
8289
$word = $_;
@@ -86,7 +93,7 @@ sub get_a_word {
8693
$line_num++;
8794
}
8895
}
89-
close( FILE );
96+
close($fh) or die "Could not close <$wordlist>: $!\n";
9097
return( $word );
9198
}
9299

0 commit comments

Comments
 (0)