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.


In reply to Re^5: implementing a scrabble-esque game on Termux III by tybalt89
in thread implementing a scrabble-esque game on Termux III by Aldebaran

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.