Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

word twist

by ysth (Canon)
on Jun 18, 2007 at 06:48 UTC ( #621751=sourcecode: print w/replies, xml ) Need Help??
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() }
Replies are listed 'Best First'.
Re: word twist
by regexes (Hermit) on Aug 31, 2007 at 13:19 UTC
    Thanks for this one..

    Now I waste more time at work... :-D

    regexes


    -------------------------
    Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan "press on" has solved and always will solve the problems of the human race.
    -- Calvin Coolidge, 30th President of the USA.
Re: word twist
by artist (Parson) on Jun 19, 2007 at 17:46 UTC
    Nice one, I didn't find cheers at the end though:-)
    --Artist
      Manually cheer yourself by entering "-new". If it wasn't functional, it pretty much didn't get done. Patches welcome, though.
        I modified slightly for my need. I maintained a list of guesses and auto-fill next time if appeared. So, I don't have to re-write them again and again. We certainly can make some sort of AI game out of this. I also tweak the word generation, so it can give me interesting personalized progressive words, based upon my choice.
        --Artist

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://621751]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2020-09-27 10:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (142 votes). Check out past polls.

    Notices?