Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^5: implementing a scrabble-esque game on Termux III

by tybalt89 (Prior)
on Dec 03, 2019 at 16:42 UTC ( #11109606=note: print w/replies, xml ) Need Help??

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11109606]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2020-11-24 12:20 GMT
Find Nodes?
    Voting Booth?

    No recent polls found