The program described below has been updated since this version was released, the newer version can be found here:
, or alternatively below.
The code below is for a semi-automatic word search solver, that uses a recursive function to search a multi-dimension array containing the puzzle for the word that the user asks for.
The program is semi-autonomous, in that the search itself is carried out automatically, but the puzzle is (for the moment) hard-coded, and the user must enter each word that they want to search for one-by-one.
The interface is a simplistic text-based one, the user enters the word that they want to search for, and the program responds with a set of coordinates for the start and end positions of the word being searched for, or 'NO RESULT' if the word can't be found.
The only command available to the user apart from the search ability is the '#quit' command which exits the program.
The program relies on the assumption that the dimensions of the grid to be searched are equivalent, i.e. rows == cols
#!/usr/bin/perl
# wordSearchSolve.pl
# Program to solve a word search puzzle semi-automatically.
# Define the puzzle to be searched as '@puzzle', see the example below
+.
#
# Christopher Dykes (2009-09-25) - (v1.0)
#Enable the following packages:
use strict; #Enable strict syntax checking
use warnings; #Enable diagnostic warnings
#Declare global variables:
our @end = undef; #The location of the end of the word
#Declare local variables:
my $done = 0; #Whether we're finished or not
my @start = undef; #The location of the start of the puzzle
my @puzzle =
(
['r','e','l','a','e','d','b','y'],
['e','s','c','r','e','e','n','t'],
['m','i','o','e','i','s','h','l'],
['i','s','l','a','t','e','r','a'],
['t','n','u','r','c','n','n','u'],
['r','g','m','u','a','i','h','s'],
['o','u','b','m','t','h','a','a'],
['m','e','o','a','n','c','c','c']
);
#Display the header:
print "Wordsearch Solver:\n\n";
print "Enter the term to search for, enter '#quit' to exit\n\n";
#Allow the user to search
while(!$done)
{
my($word, $found);
@end = undef;
#Get the word from the user:
print "> "; chomp($word = <STDIN>);
$done++ if($word eq "#quit");
unless($done)
{
my($i, $j, $k);
print $word, "\t= ";
my @word = split(//, $word);
for($i = 0, $found = 0; $i <= $#puzzle && !$found; $i++) #R
+ow loop
{
for($j = 0; $j <= $#puzzle && !$found; $j++) #Col l
+oop
{
for(my $k = 0; $k < 8 && !$found; $k++) #Dir lo
+op
{
my @gen = ("");
$found = &search($k, $i, $j, \@puzzle, \@word, @ge
+n);
}
}
}
print "($i,$j) - ($end[0],$end[1])\n" if($found);
print "NO RESULT\n" if(!$found);
}
}
#Subroutines begin here:
sub search ($ $ $ $ $ @) #Performs a recursive search across the pu
+zzle
{
#Declare local variables:
my($dir, $row, $col, $puzRef, $wrdRef, @gen) = @_;
my @puzzle = @{$puzRef};
my @word = @{$wrdRef};
($end[0], $end[1]) = (($row + 1), ($col + 1)); #Set our end loc
+ation
return 0 if($puzzle[$row][$col] ne $word[$#gen]);
return 1 if($#word == $#gen);
#Decide what to do:
$row++ if(($dir == 0 || $dir == 4 || $dir == 5) && $row < $#puzzle
+);
$row-- if(($dir == 1 || $dir == 6 || $dir == 7) && $row > 0);
$col++ if(($dir == 3 || $dir == 5 || $dir == 7) && $col < $#puzzle
+);
$col-- if(($dir == 2 || $dir == 4 || $dir == 6) && $col > 0);
#Do the useful stuff:
push(@gen, $puzzle[$row][$col]);
return 1 if(&search($dir, $row, $col, \@puzzle, \@word, @gen)) ||
+return 0;
}