Game-TextPatterns

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

1.24    2019-06-23
        Add fill_4way and fill_8way methods.

0.73    2018-10-07
        Break interface of crop (it was broken for negative bounds).
        flip_four now has more features. More examples in the docs.
        Require perl 5.24 for postderef.

0.42    2018-06-18
        More methods (append_cols, append_rows, as_array, crop, draw_in,
        flip_four, four_up, from_array, mask, overlay, rotate, trim,
        white_noise). More error checking, tests, and docs.

0.01    2018-04-10
        First version, released on an (some would say) unsuspecting world.

lib/Game/TextPatterns.pm  view on Meta::CPAN

        for my $row ($self->pattern->@*) {
            $row = $row x $cols;
        }
    }
    if ($rows > 1) {
        $self->pattern([ ($self->pattern->@*) x $rows ]);
    }
    return $self;
}

sub overlay {
    my ($self, $p, $overlay, $mask) = @_;
    my ($cols, $rows) = $self->dimensions;
    $p->[0] += $cols - 1 if $p->[0] < 0;
    $p->[1] += $rows - 1 if $p->[1] < 0;
    if ($p->[0] < 0 or $p->[0] >= $cols or $p->[1] < 0 or $p->[1] >= $rows) {
        local $" = ',';
        croak "point @$p out of bounds";
    }
    my ($colnum, $rownum) = map { $_ - 1 } $overlay->dimensions;
    my $subpat =
      $self->clone->crop($p,
        [ min($p->[0] + $colnum, $cols - 1), min($p->[1] + $rownum, $rows - 1) ]);
    my $to_draw = $overlay->clone->mask($mask, $subpat);
    $self->draw_in($p, $to_draw);
    return $self;
}

sub randomly {
    my ($self, $re, $percent, $fn) = @_;
    my $pat = $self->pattern;
    my ($cols, $rows) = (length $pat->[0], scalar $pat->@*);
    my $total   = $cols * $rows;
    my $to_fill = int($total * $percent);

lib/Game/TextPatterns.pm  view on Meta::CPAN

Returns the B<cols> and B<rows> of the current B<pattern>.

=item B<draw_in> I<point1> [ I<point2> ] I<pattern>

Draws the I<pattern> within the given bounds, though will not extend the
dimensions of the object if the I<pattern> exceeds that (hence the lower
right bound being optional). Should the I<pattern> be smaller than the
given bounds nothing will be changed at those subsequent points (this
differs from other methods that accept a I<fill> argument).

See also the more complicated B<overlay>.

=item B<fill_4way> I<point> I<char>

Replaces the character found at I<point> with I<char> and repeats this
fill for all similar characters found by 4-way motion from the
starting I<point>.

=item B<fill_8way> I<point> I<char>

Replaces the character found at I<point> with I<char> and repeats this

lib/Game/TextPatterns.pm  view on Meta::CPAN


B<mask> replaces instances of the I<char> in the object with the
corresponding character(s) of the given I<pattern>.

=item B<multiply> I<cols> [ I<rows> ]

Multiplies the existing data in the columns or rows, unless I<cols> or
I<rows> is C<1>. With no I<rows> set multiplies both the columns and
rows by the given value.

=item B<overlay> I<point> I<pattern> I<mask>

Draws the I<pattern> into the object at the given I<point> though
preserving anything from the original object that match the I<mask>
character in the I<pattern>.

See also the simpler B<draw_in>.

=item B<rows>

Returns the height (y, or number of rows) in the B<pattern>.

t/Game-TextPatterns.t  view on Meta::CPAN

EOF
    my $r = Game::TextPatterns->new(pattern => <<'EOF' );
qqq
q?q
qqq
EOF
    $p->mask('.', $r);
    eq_or_diff($p->pattern, [ "xxx", "x?x", "xxx" ]);
}

# overlay
{
    my $field = Game::TextPatterns->new(pattern => "a123\nb456\nc789");
    my $piece = Game::TextPatterns->new(pattern => "#??\n###");

    dies_ok(sub { $field->overlay([ 99, 99 ], $piece) }, 'out of bounds');

    $field->overlay([ 0, 0 ], $piece, '?');
    eq_or_diff($field->pattern, [ "#123", "###6", "c789" ]);
    $field = $field->rebuild;

    $field->overlay([ 1, 1 ], $piece, '?');
    eq_or_diff($field->pattern, [ "a123", "b#56", "c###" ]);
    $field = $field->rebuild;

    $field->overlay([ 2, 2 ], $piece, '?');
    eq_or_diff($field->pattern, [ "a123", "b456", "c7#9" ]);
}

# rotate
{
    my $r = Game::TextPatterns->new(pattern => <<'EOF' );
###.
#...
EOF
    $r->rotate(0);



( run in 0.328 second using v1.01-cache-2.11-cpan-49f99fa48dc )