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.119 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.118 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.117 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.116 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.115 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.114 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.113 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.111 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.110 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.109 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.108 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.107 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm
trackler-2.2.1.106 tracks/perl5/exercises/queen-attack/.meta/solutions/Queens.pm