hsmyers has asked for the wisdom of the Perl Monks concerning the following question:

Once again I've become a perl beginner (do you ever stop?), except this time it's in perl6. Starting from ideas that I know well, I chose to redesign a portion of previous code of mine, in this case a chessboard. The first two classes went well (or at least worked, I know so little that I can't speak to their correctness…) but I'm stuck with the third. Here is the code:

#!/home/hsmyers/rakudo741/bin/perl6 # board.p6 - Beginnings of a PGN toolset. And place to start learning # Perl 6/Raku. use v6d; #!____________________________________________________________________ +_________ constant $size = 4; class Piece { my Str @namesOfPieces[$size] = < white-rook white-knight white-bishop white-queen >; my Str @abrevsOfPieces[$size] = < R N B Q K B N R >; my Str @symbolsOfPieces[$size] = < &#9814; &#9816; &#9815; &#9813; &#9812; &#9815; &#9816; &#9814 +; >; my Str @codeptsOfPieces[$size] = ( "\x2656", "\x2658", "\x2657", "\x2655", ); has Str $.name; has Str $.abrev; has Str $.symbol; has Uni $.codept; submethod BUILD( :$i ) { $!name = @namesOfPieces[$i]; $!abrev = @abrevsOfPieces[$i]; $!symbol = @symbolsOfPieces[$i]; $!codept = @codeptsOfPieces[$i].NFC; } } class Square { my Int @colors[$size] = < 1 0 1 0 1 0 1 0 >; my Str @names[$size] = < a1 b1 c1 d1 e1 f1 g1 h1 >; has Int $.color; has Int $.index; has Str $.name; has Piece $.piece; submethod BUILD( :$i ) { $!color = @colors[$i]; $!index = $i; $!name = @names[$i]; $!piece = Piece.new(:i($i)); } } class Board is Array { } my $p = Piece.new(:i(0)); $p.say; my $s = Square.new(:i(0)); $s.say; #!____________________________________________________________________ +_________ my @b := Board.new( Square.new(:i(0)), Square.new(:i(1)), Square.new(:i(2)) ); say @b; say @b.WHAT;

The Board class (empty as it is) is all that is left from my attempts so far. Amazingly (at least to me) it provides a degree of workability!. It has variously had a "new" and a "BUILD", neither provided a working solution. Obviously the current approach doesn't work considering that the actual count will be 64 and not 4.

My current notion is that I need to build an array of 64 Squares which in turn will create the needed pieces. I've tried to add to self with nothing working. Suggestions?

--hsm

"Never try to teach a pig to sing...it wastes your time and it annoys the pig."

Replies are listed 'Best First'.
Re: [raku] How to use an object array in perl6
by Anonymous Monk on Nov 18, 2019 at 12:22 UTC
    If you do not get an answer here (for whatever reason), perhaps posting the question on Questions tagged raku on StackOverflow could give you the answer you are looking for.

      Thanks for the tip! Been gone to long…

      --hsm

      "Never try to teach a pig to sing...it wastes your time and it annoys the pig."