Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight


by dcpve (Sexton)
on Jul 12, 2002 at 17:14 UTC ( #181348=sourcecode: print w/replies, xml ) Need Help??
Category: Fun Stuff
Author/Contact Info Collin Winter aka dcpve
Description: Finds suitable Scrabble words based on your current tiles, the tiles on the board that are available for word building, and how many of your current tiles you are willing to omit to form a word.
#!/usr/bin/perl -w - A program to find suitable words while playing Scrabbl
#By Collin Winter
#    -f : Means that this is the first turn.
#         This will skip the request for base letters (see below),
#         since there will be none.

use strict;

#Replace /usr/share/dict/linux.words with the path to the words file o
+n your system.
open WORDLIST, "/usr/share/dict/linux.words" or die "Unable to open wo
+rd list: $!\n";
my @wordlist=<WORDLIST>;

#Find out what letters the player has, and make a hash of their freque
print "Your letters: ";
chomp(my $master_letters=<STDIN>);

#Removes any non-alphacharacters that may have been used to separate t
+he letters;

my $base_letters;
my %letter_freq;
#If this is the first turn of the game, then there be no base letters 
+to use, so we need to obtain one
if($ARGV[0] eq '-f'){

for (split(//,$master_letters)){

#"Base letters" are tiles that are already out on the board and availa
+ble for to build words on.
#One base letter will be added at a time to $letters, and valid words 
+looked up for that combination.
if(!$ARGV[0] || $ARGV[0] ne '-f'){
    print "Base letters: ";

#"Initial omission level" is the number of characters from $letters th
+at maybe left out of a word.
#A value of 0 will return only words that contain all of the character
+s in $letters.
#Setting it to 1 will return all words that contain every letter and a
+ll words with all letters except one, etc
print "Initial omission level: ";
chomp(my $master_om_level=<STDIN>);

my @possible_words;
my @valid_words;

foreach my $base (split(//,$base_letters)){
    my $om_level=$master_om_level;

    #Add the current base letter to the string and to the letter frequ
+ency hash
    my $letters=$master_letters.$base;

    my $do_it_again=1;

    #Repeat the word-evaluation until a word is found.
        #This is done like it is so that $max_length and $min_length c
+an be modified later.
        my $max_length=length($letters)+1;
        my $min_length=length($letters)-$om_level+1;
        #Thanks to mt2k for suggesting this kind of for loop.
        for my $i ($min_length .. $max_length){
        foreach my $word (@possible_words){
            my %this_freq=%letter_freq;
            my $this_word=$word;
            my $key_count=0;
            my $remaining_oms=$om_level;
            #Repeat until either:
            # a) there are no more letters to check for,
            # b) all keys in %letter_freq have been used, or
            # c) the word has too many omissions to be used.
            while((length($this_word)>1) && ($key_count<=(scalar keys 
+%letter_freq)-1) && ($remaining_oms>=0)){
                my $key=(keys %letter_freq)[$key_count];
                #If the current letter isn't in the word, then
                #There is one less omission remaining for this word.
                if(($this_freq{$key} > 0) && ($this_word=~/$key/i)){
        #If no words are found at your current omission level, the sea
        #will be automatically enlarged until at least one word is ret
        print $#valid_words+1," results with $om_level allowed omissio
+ns for base \'$base\'";
            print "; Expanding search...\n";
            print ":\n",@valid_words;

    #Remove the current base from the letter frequency hash
Replies are listed 'Best First'.
Re: Scrabbler
by jdavidboyd (Friar) on Jul 12, 2002 at 18:36 UTC
    I like it, but I think my family would disapprove of my having a computer next to me when we play!
      That's the downside; you can really only use this with online scrabble games, like Yahoo!s Literati.
Re: Scrabbler
by sschneid (Deacon) on Jul 12, 2002 at 18:01 UTC
    nice! useful! dishonest! ++.
      how do I use this codes? it's in perl?
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://181348]
[choroba]: the reporter is my colleague, BTW, albeit in a different team

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2018-06-20 12:06 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.