File Coverage

blib/lib/Chess/Piece/Bishop.pm
Criterion Covered Total %
statement 45 45 100.0
branch 8 8 100.0
condition 1 3 33.3
subroutine 6 6 100.0
pod 2 2 100.0
total 62 64 96.8


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Chess::Piece::Bishop - an object representation of a bishop in a game of chess
4              
5             =head1 SYNOPSIS
6              
7             $bishop = Chess::Piece::Bishop->new("f1", "white",
8             "White King's bishop");
9             $true = $bishop->can_reach("c4");
10             $true = $bishop->can_reach("h3");
11             $false = $bishop->can_reach("d4");
12              
13             =head1 DESCRIPTION
14              
15             The Chess module provides a framework for writing chess programs with Perl.
16             This class forms part of that framework, representing a bishop in a
17             L.
18              
19             =head1 METHODS
20              
21             =head2 Construction
22              
23             =item new()
24              
25             Constructs a new Chess::Piece::Bishop. Requires two scalar parameters
26             containing the initial square and color of the piece. Optionally takes a
27             third parameter containing a description of the piece.
28              
29             =head2 Class methods
30              
31             There are no class methods for this class.
32              
33             =head2 Object methods
34              
35             =item reachable_squares()
36              
37             Overrides base class version. Returns a list of squares that this pawn can
38             reach from its current position. See L
39             for more details on this method.
40            
41             =head1 DIAGNOSTICS
42              
43             This module produces no warning messages. See
44             L or
45             L for possible
46             errors or warnings the program may produce.
47              
48             =head1 BUGS
49              
50             Please report any bugs to the author.
51              
52             =head1 AUTHOR
53              
54             Brian Richardson
55              
56             =head1 COPYRIGHT
57              
58             Copyright (c) 2002, 2005 Brian Richardson. All rights reserved. This module is
59             Free Software. It may be modified and redistributed under the same terms as
60             Perl itself.
61              
62             =cut
63             package Chess::Piece::Bishop;
64              
65 4     4   15507 use Chess::Board;
  4         10  
  4         122  
66 4     4   644 use Chess::Piece;
  4         10  
  4         139  
67 4     4   23 use base 'Chess::Piece';
  4         8  
  4         373  
68 4     4   32 use strict;
  4         8  
  4         2898  
69              
70             sub new {
71 17     17 1 45 my ($caller, $sq, $color, $desc) = @_;
72 17   33     74 my $class = ref($caller) || $caller;
73 17         81 my $self = $caller->SUPER::new($sq, $color, $desc);
74 17         63 return bless $self, $class;
75             }
76              
77             sub reachable_squares {
78 458     458 1 699 my ($self) = @_;
79 458         1083 my $csq = $self->get_current_square();
80 458         1616 my $hdist = abs(Chess::Board->horz_distance("a1", $csq));
81 458         1432 my $vdist = abs(Chess::Board->vert_distance("a1", $csq));
82 458 100       1200 my $dist = $hdist > $vdist ? $vdist : $hdist;
83 458         1545 my $sq = Chess::Board->add_horz_distance($csq, -$dist);
84 458         1640 $sq = Chess::Board->add_vert_distance($sq, -$dist);
85 458         823 my @squares = ( );
86 458         1488 push @squares, Chess::Board->squares_in_line($csq, $sq);
87 458         2204 $hdist = abs(Chess::Board->horz_distance("h1", $csq));
88 458         1477 $vdist = abs(Chess::Board->vert_distance("h1", $csq));
89 458 100       998 $dist = $hdist > $vdist ? $vdist : $hdist;
90 458         1418 $sq = Chess::Board->add_horz_distance($csq, $dist);
91 458         1571 $sq = Chess::Board->add_vert_distance($sq, -$dist);
92 458         1531 push @squares, Chess::Board->squares_in_line($csq, $sq);
93 458         1677 $hdist = abs(Chess::Board->horz_distance("a8", $csq));
94 458         2447 $vdist = abs(Chess::Board->vert_distance("a8", $csq));
95 458 100       1084 $dist = $hdist > $vdist ? $vdist : $hdist;
96 458         1537 $sq = Chess::Board->add_horz_distance($csq, -$dist);
97 458         1436 $sq = Chess::Board->add_vert_distance($sq, $dist);
98 458         1532 push @squares, Chess::Board->squares_in_line($csq, $sq);
99 458         1586 $hdist = abs(Chess::Board->horz_distance("h8", $csq));
100 458         1400 $vdist = abs(Chess::Board->vert_distance("h8", $csq));
101 458 100       1016 $dist = $hdist > $vdist ? $vdist : $hdist;
102 458         1348 $sq = Chess::Board->add_horz_distance($csq, $dist);
103 458         1465 $sq = Chess::Board->add_vert_distance($sq, $dist);
104 458         1398 push @squares, Chess::Board->squares_in_line($csq, $sq);
105 458         14044 @squares = grep !/^$csq$/, @squares;
106 458         7601 return @squares;
107             }
108              
109             1;