The deadly combination of Perl and /usr/dict/words can do more than just give a listing of one handed words, it can help you win at scrabble. The program takes two optional arguments, the length of the words and the amount of words you want generated.


#!/usr/bin/perl -w

#defaults
$top = 10; #Size of high score table.
$letters = 5; #how many letters you want the word to be....

$letters = $ARGV[0] if( defined $ARGV[0] );
$top = $ARGV[1] if( defined $ARGV[1] );

$dictionary = "/usr/dict/words";
@points = ( 1,3,3,2, 1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10 );
@tiles  = ( 9,2,2,4,12,2,3,2,9,1,1,4,2,6,7,2, 1,6,4,6,4,2,2,1,2, 1);
@highscore = ();

foreach( ord('a')..ord('z') ) {
    push(@alphabet, chr($_));
}

open F, "$dictionary" or die "Can't open $dictionary!";

WORD: while( <F> ) {
    chop;
    $word = lc;
    if( length($_) == $letters ) {
	foreach( @alphabet ) {
	    $max = $tiles[ idx($_) ];
	    eval "\$count = (\$word =~ tr/$_//);";
	    if( $count > $max ) {
		print "$word invalid, too many \"$_\"'s ($count, max=$max)\n";
		next WORD;		
	    }
	} 

	if( @highscore >= $top ) {
	    $s = score($word);
	    if( $s > $highscore[-1]->[1] ) {
		$highscore[-1] = [$word, $s];
		print "added $word (score=$s) to highscores\n";
		@highscore = sort { $b->[1] <=> $a->[1] } @highscore;
	    }
	} else {
	    push(@highscore, [$word, score($word)]);
	    if( @highscore >= $top ) {
		@highscore = sort { $a->[1] <=> $b->[1] } @highscore;
	    }
	}
    }
}

foreach( @highscore ) {
    print ++$i, ": ", $_->[0], " (score: ", $_->[1], ")\n";
}

sub idx {
    ord( lc shift ) - ord "a";
}

sub score {
    my $score = 0;
    my $word = shift;

    while( $word =~ /([a-z])/g ) {
	$score += $points[ idx($1) ];
    }

    return $score;

}


Eg the programs output with 11 five letter words (arguments "5 11"):

  1. czech : 21
  2. jacky : 21
  3. jiffy : 21
  4. hafiz : 20
  5. muzak : 20
  6. quack : 20
  7. quaff : 20
  8. quick : 20
  9. crazy : 19
  10. jerky : 19
  11. junky : 19
Compared to yours:
  1. diazo : 15
  2. gonzo : 15
  3. beaux : 14
  4. djinn : 13
  5. ninja : 12
  6. footy : 11
  7. kiang : 10
  8. abaci : 9
  9. cubit : 9
  10. amiga : 8
  11. aurum : 7

Log in or register to write something here or to contact authors.