in reply to Re^4: implementing a scrabble-esque game on Termux III and Path::Tiny output
in thread implementing a scrabble-esque game on Termux III
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.
|
---|