File Coverage

blib/lib/Chess/Piece/Queen.pm
Criterion Covered Total %
statement 54 54 100.0
branch 8 8 100.0
condition 1 3 33.3
subroutine 6 6 100.0
pod 2 2 100.0
total 71 73 97.2


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Chess::Piece::Queen - an object representation of a queen in a game of chess
4              
5             =head1 SYNOPSIS
6              
7             $queen = Chess::Piece::Queen->new("d1", "white", "White Queen");
8             $true = $queen->can_reach("d8");
9             $true = $queen->can_reach("h1");
10             $true = $queen->can_reach("h5");
11             $true = $queen->can_reach("a4");
12             $false = $queen->can_reach("e4");
13              
14             =head1 DESCRIPTION
15              
16             The Chess module provides a framework for writing chess programs with Perl.
17             This class forms part of that framework, representing a bishop in a
18             L.
19              
20             =head1 METHODS
21              
22             =head2 Construction
23              
24             =item new()
25              
26             Constructs a new Chess::Piece::Queen. Requires two scalar parameters
27             containing the initial square and color of the piece. Optionally takes a
28             third parameter containing a description of the piece.
29              
30             =head2 Class methods
31              
32             There are no class methods for this class.
33              
34             =head2 Object methods
35              
36             =item can_reach()
37              
38             Overrides base class version. Returns a list of squares that this pawn can
39             reach from its current position. See L
40             for more details on this method.
41              
42             =head1 DIAGNOSTICS
43              
44             This module produces no warning messages. See
45             L or
46             L for possible
47             errors or warnings the program may produce.
48              
49             =head1 BUGS
50              
51             Please report any bugs to the author.
52              
53             =head1 AUTHOR
54              
55             Brian Richardson
56              
57             =head1 COPYRIGHT
58              
59             Copyright (c) 2002, 2005 Brian Richardson. All rights reserved. This module is
60             Free Software. It may be modified and redistributed under the same terms as
61             Perl itself.
62              
63             =cut
64             package Chess::Piece::Queen;
65              
66 5     5   22909 use Chess::Board;
  5         8  
  5         124  
67 5     5   657 use Chess::Piece;
  5         9  
  5         125  
68 5     5   26 use base 'Chess::Piece';
  5         9  
  5         435  
69 5     5   26 use strict;
  5         16  
  5         3234  
70              
71             sub new {
72 9     9 1 31 my ($caller, $sq, $color, $desc) = @_;
73 9   33     77 my $class = ref($caller) || $caller;
74 9         57 my $self = $caller->SUPER::new($sq, $color, $desc);
75 9         32 return bless $self, $class;
76             }
77              
78             sub reachable_squares {
79 358     358 1 614 my ($self) = @_;
80 358         1086 my $csq = $self->get_current_square();
81 358         716 my @squares = ( );
82 358         1366 my $x = Chess::Board->horz_distance("a4", $csq);
83 358         1325 my $y = Chess::Board->vert_distance("d1", $csq);
84 358         946 my $row_start = 'a' . ($y + 1);
85 358         1285 my $row_end = 'h' . ($y + 1);
86 358         674 my $col_start = chr(ord('a') + $x) . '1';
87 358         613 my $col_end = chr(ord('a') + $x) . '8';
88 358         1169 my @row = Chess::Board->squares_in_line($row_start, $row_end);
89 358         1545 my @col = Chess::Board->squares_in_line($col_start, $col_end);
90 358         2244 push @squares, @row, @col;
91 358         1316 my $hdist = abs(Chess::Board->horz_distance("a1", $csq));
92 358         1146 my $vdist = abs(Chess::Board->vert_distance("a1", $csq));
93 358 100       926 my $dist = $hdist > $vdist ? $vdist : $hdist;
94 358         1294 my $sq = Chess::Board->add_horz_distance($csq, -$dist);
95 358         1347 $sq = Chess::Board->add_vert_distance($sq, -$dist);
96 358         1281 push @squares, Chess::Board->squares_in_line($csq, $sq);
97 358         1230 $hdist = abs(Chess::Board->horz_distance("h1", $csq));
98 358         1226 $vdist = abs(Chess::Board->vert_distance("h1", $csq));
99 358 100       956 $dist = $hdist > $vdist ? $vdist : $hdist;
100 358         1285 $sq = Chess::Board->add_horz_distance($csq, $dist);
101 358         1225 $sq = Chess::Board->add_vert_distance($sq, -$dist);
102 358         3527 push @squares, Chess::Board->squares_in_line($csq, $sq);
103 358         1229 $hdist = abs(Chess::Board->horz_distance("a8", $csq));
104 358         1380 $vdist = abs(Chess::Board->vert_distance("a8", $csq));
105 358 100       888 $dist = $hdist > $vdist ? $vdist : $hdist;
106 358         1288 $sq = Chess::Board->add_horz_distance($csq, -$dist);
107 358         1208 $sq = Chess::Board->add_vert_distance($sq, $dist);
108 358         1174 push @squares, Chess::Board->squares_in_line($csq, $sq);
109 358         1254 $hdist = abs(Chess::Board->horz_distance("h8", $csq));
110 358         1376 $vdist = abs(Chess::Board->vert_distance("h8", $csq));
111 358 100       864 $dist = $hdist > $vdist ? $vdist : $hdist;
112 358         1146 $sq = Chess::Board->add_horz_distance($csq, $dist);
113 358         1277 $sq = Chess::Board->add_vert_distance($sq, $dist);
114 358         1156 push @squares, Chess::Board->squares_in_line($csq, $sq);
115 358         20832 @squares = grep !/^$csq$/, @squares;
116 358         13012 return @squares;
117             }
118              
119             1;