
use strict;
use warnings;

die "Usage: $0 dictionary_name number_of_guesses\n"
   if @ARGV < 2 or $ARGV[1] !~ /^\d+$/;

my ( $dictionary, $countdown ) = @ARGV;
my $word = get_word ( $dictionary );
my $tried = ' ';

while ( $countdown ) {
   ( my $grid = $word ) =~ s/[^$tried]/_/g;
   print "LIFE!\n" and exit if $grid eq $word;
   print "$grid\n";
   # print "Used so far:", sort split //, $tried;
   # print "\nGuesses left: $countdown\n";
   chomp ( my $guess = lc <STDIN> );
   next if $guess !~ /^[a-z]$/ or $tried =~ /$guess/;
   $tried .= $guess;
   $countdown-- unless $word =~/$guess/;
}
print "DEATH!\n";
# print "($word)\n";

sub get_word {
   open my $fh, '<', $_[0] or die "Can't open dictionary file: $!";
   my $choice;
   rand $. < 1 and chomp ( $choice = $_ ) while <$fh>;
   return $choice;
}




