Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hangman versus small file #416

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions bin/hangman
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,18 @@ print "\nTHANKS FOR PLAYING!\n";

sub get_a_word {
my $wordlist = "wordlist.txt";
my( $word );
my( $fh, $word );
my( $line_num ) = 1;
my( $random ) = rand();
my( $val ) = int( $random * 9151 ) + 1;
open my $fh, '<', $wordlist or die "Could not open <$wordlist>: $!\n";

open($fh, '<', $wordlist) or die "Could not open <$wordlist>: $!\n";
my $lines = 0;
$lines++ while (<$fh>);
my $val = int($random * $lines) + 1;

close($fh) or die "Could not close <$wordlist>: $!\n";
open($fh, '<', $wordlist) or die "Could not open <$wordlist>: $!\n";

while( <$fh> ) {
if( $line_num == $val ) {
$word = $_;
Expand All @@ -86,7 +93,7 @@ sub get_a_word {
$line_num++;
}
}
close( FILE );
close($fh) or die "Could not close <$wordlist>: $!\n";
return( $word );
}

Expand Down
Loading