Straddling checkerboard: Difference between revisions

Content added Content deleted
(→‎{{header|Perl 6}}: Mark as broken)
(→‎{{header|Perl 6}}: various fixes, works again)
Line 1,481: Line 1,481:


=={{header|Perl 6}}==
=={{header|Perl 6}}==
{{works with|Rakudo|2018.03}}
{{broken|Perl 6}}
The .trans method in Perl 6 improves on Perl 5's tr/// by allowing multi-character translation tables.
The .trans method in Perl 6 improves on Perl 5's tr/// by allowing multi-character translation tables.


Line 1,487: Line 1,487:
<lang perl6>class Straddling_Checkerboard {
<lang perl6>class Straddling_Checkerboard {
has @!flat_board; # 10x3 stored as 30x1
has @!flat_board; # 10x3 stored as 30x1
has $!plain2code; # Full translation table; invertable
has $!plain2code; # full translation table, invertable
has @!table; # Printable layout, like Wikipedia entry
has @!table; # Printable layout, like Wikipedia entry
Line 1,493: Line 1,493:
my $exclude = /<-[A..Z0..9.]>/; # Omit the escape character
my $exclude = /<-[A..Z0..9.]>/; # Omit the escape character


method display_table { say ~ .list for @!table };
method display_table { gather { take ~ .list for @!table } };


method decode ( Str $s --> Str ) {
method decode ( Str $s --> Str ) {
$s.trans($!plain2code.invert);
$s.trans($!plain2code.antipairs);
}
}


Line 1,506: Line 1,506:
submethod BUILD ( :$alphabet, :$u where 0..9, :$v where 0..9 ) {
submethod BUILD ( :$alphabet, :$u where 0..9, :$v where 0..9 ) {
die if $u == $v;
die if $u == $v;
die if $alphabet.comb.sort.join ne [~] './', 'A'..'Z';
die if $alphabet.comb.sort.join ne [~] flat './', 'A'..'Z';


@!flat_board = $alphabet.uc.comb;
@!flat_board = $alphabet.uc.comb;
Line 1,512: Line 1,512:
@!flat_board.splice( $u max $v, 0, Any );
@!flat_board.splice( $u max $v, 0, Any );


@!table = [ ' ', [ 0 .. 9] ],
@!table = [ ' ', [ 0 .. 9] ],
[ ' ', @!flat_board[ 0 .. 9].map: * // ' ' ],
[ ' ',|@!flat_board[ 0 .. 9].map: {.defined ?? $_ !! ' '} ],
[ $u, @!flat_board[10 .. 19] ],
[ $u, @!flat_board[10 .. 19] ],
[ $v, @!flat_board[20 .. 29] ];
[ $v, @!flat_board[20 .. 29] ];


my @order = 0..9; # This may be passed as a param in the future
my @order = 0..9; # This may be passed as a param in the future


my @nums = @order,
my @nums = flat @order,
@order.map({ +"$u$_" }),
@order.map({ +"$u$_" }),
@order.map({ +"$v$_" });
@order.map({ +"$v$_" });


my %p2c = @!flat_board Z=> @nums;
my %c2p = @nums Z=> @!flat_board;
%p2c.delete(Any);
%c2p{$_}:delete if %c2p{$_} eqv Any for keys %c2p;
my %p2c = %c2p.invert;
%p2c{$_} = %p2c{$numeric_escape} ~ $_ for 0..9;
%p2c{$_} = %p2c{$numeric_escape} ~ $_ for 0..9;

$!plain2code = [%p2c.keys] => [%p2c.values];
$!plain2code = [%p2c.keys] => [%p2c.values];
}
}