Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^6: implementing a scrabble-esque game on Termux III and how best to create a main data structure

by Aldebaran (Deacon)
on Dec 10, 2019 at 05:19 UTC ( #11109897=note: print w/replies, xml ) Need Help??


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

Hope this helps...

It does. I was able to see these masks by unpacking them as hexidecimal. Here's a bit of output:

new mask is ffff00ffff in score pos old word 57, motif, motor score mask s 000000ffff helper is mot-- .......... .......... ....r..... ....o..... ....o..... ..motif... ....a..... ....g..... ....e..... .......... - - - - - .......... .......... ....r..... ....o..... ....o..... ..mot--... ....a..... ....g..... ....e..... ..........

with these changes to the score subroutine:

sub score { my ( $bd, $hi, $pos, $old, $word ) = @_; my $len = length $word; my $mask = ( $old ^ $word ) =~ tr/\0/\xff/cr; say "in score pos old word"; say "$pos, $old, $word"; my( $hex ) = unpack( 'H*', $mask ); print "score mask s $hex\n"; ; my $helper = ( $old & ~$mask ) =~ tr/\0/-/r; say "helper is $helper"; say "$bd";

With the mask questions addressed, I wanted to chop up the beginning of the script into subroutines, where possible.

I want to switch gears here, and address breaking up this program into smaller, digestable parts. tybalt89 has already placed several of the back end functions like matchrule and crosswords in past versions. I'm intending to get code out of the main script and shepherded to subroutines beginning with the beginning. What I have right now is:

#!/usr/bin/perl use strict; # multiplayer upwords use warnings; use 5.016; use Path::Tiny; use Data::Dump; @ARGV or @ARGV = qw( one two three four ); # for testing ## paths and constraints my $abs = path(__FILE__)->absolute; my $path1 = Path::Tiny->cwd; my %var; #main data structure my $ref_var = \%var; $var{abs} = $abs; $var{cwd} = $path1; $ref_var = init_vars($ref_var); my ( $board, $heights ) = create_board($ref_var); my @dictwords = create_dict($ref_var); my %isword = map +( $_, 1 ), @dictwords; #dd \%isword; my @drawpile =create_draw_pile(); #dd \@drawpile; dd \$ref_var; #$var{max_tiles} * @ARGV > @drawpile and die "too many players for ti +les\n"; sub init_vars { use Path::Tiny; use Data::Dump; use 5.016; use warnings; use POSIX qw(strftime); my $rvars = shift; my %vars = %$rvars; my $script = $vars{abs}; my $path1 = $vars{cwd}; my $games = "games"; my $path2 = path( $path1, $games ); say "abs is $abs"; say "path1 is $path1"; say "path2 is $path2"; print "This script will build the above path2. Proceed? (y|n)"; my $prompt = <STDIN>; chomp $prompt; die unless ( $prompt eq "y" ); $vars{side} = 9; $vars{d_file} = path( "my_data", 'enable1.txt' ); $vars{ca_name} = path( "my_data", "words.11108138.$vars{side}" ); my $munge = strftime( "%d-%m-%Y-%H-%M-%S", localtime ); $munge .= ".txt"; $vars{save_file} = path( $path2, $munge )->touchpath; $vars{save_file}->append_utf8("Script executing is $script\n"); # other initializations $vars{max_tiles} = 7;and how best to create a main data structure $vars{n_1} = $vars{side} + 1; $rvars = \%vars; dd $rvars; say "exiting init"; return ($rvars); } sub create_board { use warnings; use 5.016; my $rvars = shift; my %vars = %$rvars; my $board = ( '.' x $vars{side} . "\n" ) x $vars{side}; my $heights = $board =~ tr/./0/r; return ( $board, $heights ); } sub create_dict { use warnings; use 5.016; my $rvars = shift; my %vars = %$rvars; my @dictwords; if ( -f $vars{ca_name} ) { @dictwords = split /\n/, $vars{ca_name}->slurp; system("stat $vars{ca_name}"); } else { print "caching words of max length $vars{side}\n"; @dictwords = sort { length $b <=> length $a } grep /^[a-z]{2,$vars{ $vars{max_tiles} = 7;side}}$/, split /\n/, $vars{d_file}->slurp; $vars{ca_name}->spew( join "\n", @dictwords, '' ); } return @dictwords; } sub create_draw_pile{ use List::Util qw( shuffle ); use warnings; use 5.016; my @drawpile = shuffle + # thanks to GrandFather 11108145 ('a') x 7, ('b') x 3, ('c') x 4, ('d') x 5, ('e') x 8, ('f') x 3, ('g') x 3, ('h') x 3, ('i') x 7, ('j') x 1, ('k') x 2, ('l') x 5, (' +m') x 5, ('n') x 5, ('o') x 7, ('p') x 3, ('q') x 1, ('r') x 5, ('s') x 6, (' +t') x 5, ('u') x 5, ('v') x 2, ('w') x 2, ('x') x 1, ('y') x 2, ('z') x 1; return @drawpile; }

This has the output I'm looking for:

$ ./2.1.pt.pl abs is /home/bob/4.scripts/distro/2.1.pt.pl path1 is /home/bob/4.scripts/distro path2 is /home/bob/4.scripts/distro/games This script will build the above path2. Proceed? (y|n)y { abs => bless([ "/home/bob/4.scripts/distro/2.1.pt.pl", "/home/bob/4.scripts/distro/2.1.pt.pl", ], "Path::Tiny"), ca_name => bless(["my_data/words.11108138.9", "my_data/words.1110813 +8.9"], "Path::Tiny"), cwd => bless(["/home/bob/4.scripts/distro", "/home/bob/4.scripts/dis +tro"], "Path::Tiny"), d_file => bless(["my_data/enable1.txt", "my_data/enable1.txt"], "Pat +h::Tiny"), max_tiles => 7, n_1 => 10, save_file => bless([ "/home/bob/4.scripts/distro/games/09-12-2019-12-51-34.txt", "/home/bob/4.scripts/distro/games/09-12-2019-12-51-34.txt", "", "/home/bob/4.scripts/distro/games/", "09-12-2019-12-51-34.txt", ], "Path::Tiny"), side => 9, } exiting init File: my_data/words.11108138.9 Size: 871513 Blocks: 1704 IO Block: 4096 regular file Device: 806h/2054d Inode: 3409780 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/ bob) Gid: ( 1000/ bo +b) Access: 2019-12-09 12:31:54.804156635 -0800 Modify: 2019-12-06 14:11:08.392888086 -0800 Change: 2019-12-06 14:11:08.396887925 -0800 Birth: - \{ abs => bless([ "/home/bob/4.scripts/distro/2.1.pt.pl", "/home/bob/4.scripts/distro/2.1.pt.pl", ], "Path::Tiny"), ca_name => bless(["my_data/words.11108138.9", "my_data/words.1110813 +8.9"], "Path::Tiny"), cwd => bless(["/home/bob/4.scripts/distro", "/home/bob/4.scripts/dis +tro"], "Path::Tiny"), d_file => bless(["my_data/enable1.txt", "my_data/enable1.txt"], "Pat +h::Tiny"), max_tiles => 7, n_1 => 10, save_file => bless([ "/home/bob/4.scripts/distro/games/09-12-2019-12-51-34.txt", "/home/bob/4.scripts/distro/games/09-12-2019-12-51-34.txt", "", "/home/bob/4.scripts/distro/games/", "09-12-2019-12-51-34.txt", ], "Path::Tiny"), side => 9, } $

But if I remove the remark on the ultimate statement before the subs, I get:

...(same as above until) max_tiles => 7, n_1 => 10, save_file => bless([ "/home/bob/4.scripts/distro/games/09-12-2019-13-32-58.txt", "/home/bob/4.scripts/distro/games/09-12-2019-13-32-58.txt", "", "/home/bob/4.scripts/distro/games/", "09-12-2019-13-32-58.txt", ], "Path::Tiny"), side => 9, } Use of uninitialized value in multiplication (*) at ./2.1.pt.pl line 3 +1. $

So I'm mystified that

  $vars{max_tiles} = 7;

does not initialize, even demoralized. I had wanted to create a central data structure, and thought a hash would be the ticket. Let me ask this:

Would I have an easier time housing and calling this data if I created an object out of it?

Right now, I have to pass a reference and receive it as well; I think it's hideous:

$ref_var = init_vars($ref_var);

How would $vars{max_tiles} look like if it were blessed into a class?

Fishing for tips. Thanks for your comment,

  • Comment on Re^6: implementing a scrabble-esque game on Termux III and how best to create a main data structure
  • Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2020-11-30 11:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?