Explanation of the masking in "matchrule"
sub matchrule {
my ( $old, $highs, $word ) = @_;
$old eq $word and return 0;
my $newmask = ( $old ^ $word ) =~ tr/\0/\xff/cr;
( $newmask & $highs ) =~ tr/5// and return 0;
my $tiles = "@tiles";
$tiles =~ s/$_// or return 0 for ( $newmask & $word ) =~ /\w/g;
return 1;
}
Let's follow a call to matchrule('hello', '14222', 'world')
First off, if old and word are the same, it's not a valid move.
Next, get the mask.
( note: strings have been printed by Data::Dumper::Useqq = 1 )
'hello' ^ 'world' => "\37\n\36\0\13"
However, I want to change all non nulls ("\0") to "\xff" so that
characters can pass through these positions unchanged.
my $newmask = ('hello' ^ 'world') =~ tr/\0/\xff/cr => "\377\377\377\0\
+377"
Now we use this mask against the tile heights and look for any 5's,
because the "\xff" in $newmask are at the positions where new tiles will be added.
("\377\377\377\0\377" & '14222') => "142\0002"
and since there is no 5, the new word is not invalid (yet).
"\377\377\377\0\377" & 'world' => "wor\0d"
This leaves only the new tiles that must be played, ignoring the "\0"
because those positions use the old tile. So remove each new letter from
a string of the tile rack, and if any are not there, the move is invalid.
Hope this helps.
-
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.