Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-25 16:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found