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??
Boggle's a very simple game, in which 16 dice containing random letter sets are rattled and set into a 4x4 grid. Then players have a limited amount of time to located words in the grid by starting at any letter then moving to an adjacent cube (all 8 directions, no 'warping' on sides, however) to form out a word. For example, with the random grid:
REWE MTAS KLET NVFO
You can form the word 'WATER', but you can't form the word 'SETTER' as you can't stay on a letter to make a double letter. At the end of a specified amount of time, the players would reveal their lists and the one with the highest number of valid words wins.

Now, Boggle has been approached before on PM: chipmunk has a basic boggle word finder that locates words in a boggle grid. And gaspodethewonderdog has a node that generates a boggle board.

The golf now is to try to 'simplify' these. Your code should fit into the following blocks:

sub find_boggle_words { $storage = prepare_boggle_search( @bogglelist ); @matched = grep { test_boggle_word( $_, $storage ) } @$dict; return @matched; } sub prepare_boggle_search { # YOUR CODE HERE } sub test_boggle_word { # YOUR CODE HERE }
$dict is a list of valid dictionary words. @bogglelist is an array of the current randomly-selected boggle characters; if the above example was used, then @bogglelist would look like: qw( R E W E M T A S K L E T N V F O ).

The first function, prepare_boggle_search, can be any method to make searching for words easier. If you choose not to use it, treat this function as zero strokes towards your golf total; additionally, you can pass @bogglelist instead of $storage to test_boggle_word (eg test_boggle_word( $_, @bogglelist ). If you do use this function, you should pass back your data structure as this will be passed on to test_boggle_word.

The second function test_boggle_word should return true of the word passed can be found on the boggle board as defined by either @bogglelist or by your $storage variable.

No extra modules are allowed, and strictness need not apply. Golf will only count characters in the subroutines, not in the code provided, and the character count should be done as if the entire code was on one single line. Assume that all inputs are valid (that is, the @bogglelist will always have 16 elements, for example).

For bonus points, generalize the situation when you have a NxN boggle board. In this case, the wrapping code will look like:

sub find_boggle_words { $storage = prepare_boggle_search( $n, @bogglelist ); @matched = grep { test_boggle_word( $_, $n, $storage ) } @$dict; return @matched; }
where $n is the board size N (>1). Again, if you don't use prepare_boggle_search, you can replace $storage with @bogglelist.

Update - You may assume that no word that can be found will be longer than 12 characters, if you need to use this information.

-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
"I can see my house from here!"
It's not what you know, but knowing how to find it if you don't know that's important


In reply to (Golf) The Perl Boggles by Masem

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 chanting in the Monastery: (9)
As of 2024-03-28 14:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found