Welcome to the Monastery! You have nice puzzle, where I think I would use a different approach. Basically, when a user comes up with a starting grid coordinate, and a word to insert (horizontally or vertically) at that point, all you want to do really is to determine the length of the user's proposed word, and do two things:
- check that the word does not extend past the edge of the grid, and
- extract just the list of current grid contents from the cells affected by the word, join those contents into a string, and -- given that empty cells are initialized with period characters -- see whether that string matches the proposed word.
# Assume this is a subroutine, called with these args:
my ($grid_size); # number of rows, columns (square grid, right?)
my ($bgnX,$bgnY); # user's start coords;
my ($new_word); # user's proposed word;
my ($direction); # down or across
my $newlen = length( $new_word );
if ( $direction eq 'down' ) {
if ( $bgnY + $newlen > $grid_size ) {
return "$new_word doesn't fit going down from $bgnY\n";
}
my $ongrid = "";
for ($bgnY .. $grid_size-1) {
$ongrid .= $board[$bgnX][$_];
}
return "$new_word doen't match existing tiles\n"
unless ( $new_word =~ /^$ongrid$/ );
}
else {
# left as an exercise...
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.