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.139 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.138 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.137 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.136 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.135 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.134 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.133 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.132 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.131 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.130 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.129 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.128 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.127 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.126 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.125 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.124 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.123 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.122 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.121 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.120 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm