Category: | Fun Stuff |
Author/Contact Info | ysth (but then you knew that) |
Description: | Based on a popular word game with a similar name on an unnamed ActiveX based site.
Given 6 letters, guess all words that can be made from those six letters.
Enter "-show" to show all unguessed words for one round, "-new" to start with
a new set of letters.
The code was hacked together quite sloppily, but it does what I want it to do. |
#!/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() } |
Back to
Code Catacombs