Sha256: 15a369a98928250a328904dea8023cfc84969cf4757c67875324a5ebf0e763fb

Contents?: true

Size: 1.08 KB

Versions: 73

Compression:

Stored size: 1.08 KB

Contents

package Queens;
use strict;
use warnings;

sub new {
    my ($class, %data) = @_;

    my $self = bless \%data, $class;

	$self->{white} //= [0, 3];
	$self->{black} //= [7, 3];

    foreach my $c (@{ $self->{white} }, @{ $self->{black} }) {
       die "ArgumentError" if $c < 0 or $c > 7 or $c != int($c);
    }

    die "ArgumentError" if
        $self->{white}[0] == $self->{black}[0] and
        $self->{white}[1] == $self->{black}[1];

	return $self;
}

sub white {
    my ($self) = @_;
    $self->{white};
}

sub black {
    my ($self) = @_;
    $self->{black};
}

sub to_string {
    my ($self) = @_;
    my @board;
	push @board, [('O') x 8] for 1..8;
	$board[$self->white->[0]][$self->white->[1]] = 'W';
	$board[$self->black->[0]][$self->black->[1]] = 'B';

	return join '',
        map { "$_\n" }
		map { join ' ', @$_ } @board
}

sub can_attack {
	my ($self) = @_;

	return 1 if $self->white->[0] == $self->black->[0];
	return 1 if $self->white->[1] == $self->black->[1];
	return 1 if
		abs($self->white->[0] - $self->black->[0]) ==
		abs($self->white->[1] - $self->black->[1]);

	return;
}

1;

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.179 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.178 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.177 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.176 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.175 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.174 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.173 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.172 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.171 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.170 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.169 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.167 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.166 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.165 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.164 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.163 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.162 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.161 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.160 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm