#!/usr/bin/perl
use strict;
use warnings;
use List::Util "shuffle";
my %dict;
my $clear = qx/clear/;
my $word;
my $dorw;
my $words;
my %guesses;
sub load_dict {
open my $dictfile, "/usr/share/dict/words" or die "error $!";
/^([a-z]{3,6})\n/ && push @{$dict{length($1)}}, "$1" while <$dictfi
+le>;
}
sub subwords { [ grep $dorw =~ join(".*", sort split(//, $_)), @{$dict
+{$_[0]}} ] }
sub set_word {
$word = $dict{6}[rand @{$dict{6}}];
$dorw = join "", sort split //, $word;
$words = { map {; $_ => subwords($_) } 3..6 };
@guesses{@{$words->{$_}}} = ("_" x $_) x @{$words->{$_}} for 3..6;
$^T = time();
return;
}
my $guess = '';
sub show {
print $clear;
print "\n",(time-$^T),"\n\n";
print join(" ", ($guess eq '-show' ? @{$words->{$_}} : @guesses{@{$
+words->{$_}}})), "\n\n" for 3..6;
print join(" ", shuffle(split //, $word)), "\n";
}
sub guess {
chomp($guess = <STDIN>);
$guesses{$guess} = $guess if exists $guesses{$guess};
set_word if $guess eq '-new';
}
print "Enter guesses, -show, or -new. Press Enter to start.\n";
<STDIN>;
load_dict();
set_word();
for (;;) { show(); guess() }
In reply to word twist
by ysth
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.