Note: The program described below has been updated since this version was released, the newer version can be found here: The update, as promised, or alternatively below.

Description:

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.

Interface

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.

Considerations:

The program relies on the assumption that the dimensions of the grid to be searched are equivalent, i.e. rows == cols

Planned Improvements:

1.) The option for the user to enter in the puzzle data 'on the fly'

2.) Removal of the final global variable '@end'

3.) The ability for the user to define a list of values to search for

Program Code:

#!/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; }


In reply to A Semi-automatic word search solver by The Hindmost

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.