File Coverage

blib/lib/Chess/Piece/Knight.pm
Criterion Covered Total %
statement 44 44 100.0
branch 24 24 100.0
condition 1 3 33.3
subroutine 6 6 100.0
pod 2 2 100.0
total 77 79 97.4


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Chess::Piece::Knight - an object representing a knight in a game of chess
4              
5             =head1 SYNOPSIS
6              
7             $knight = Chess::Piece::Knight->new("g1", "white",
8             "White King's knight");
9             $true = $knight->can_reach("f3");
10             $true = $knight->can_reach("e2");
11             $false = $knight->can_reach("g3");
12              
13             =head1 DESCRIPTION
14              
15             The Chess module provides a framework for writing chess programs with Perl.
16             This class forms part of the framework, representing a knight in a
17             L.
18              
19             =head1 METHODS
20              
21             =over 4
22              
23             =head2 Construction
24              
25             =item new()
26              
27             Constructs a new Chess::Piece::Knight. Requires two scalar parameters
28             containing the initial square and color for this piece. Optionally takes a
29             third scalar parameter containing a description of this piece.
30              
31             $knight = Chess::Piece::Knight->new("g1", "white");
32             $knight = Chess::Piece::Knight->new("g8", "black",
33             "Black King's knight");
34              
35             =head2 Class methods
36              
37             There are no class methods for this class.
38              
39             =head2 Object methods
40              
41             =item reachable_squares()
42              
43             Overrides base class version. Returns a list of squares that this pawn can
44             reach from its current position. See L
45             for more details on this method.
46              
47             =back
48              
49             =head1 DIAGNOSTICS
50              
51             This subclass of L does not generate any warning messages by
52             itself. Please see L
53             or L for possible
54             error messages your program may produce.
55              
56             =head1 BUGS
57              
58             Please report any bugs to the author.
59              
60             =head1 AUTHOR
61              
62             Brian Richardson
63              
64             =head1 COPYRIGHT
65              
66             Copyright (c) 2002, 2005 Brian Richardson. All rights reserved. This module
67             is Free Software. It may be modified and redistributed under the same terms
68             as Perl itself.
69              
70             =cut
71             package Chess::Piece::Knight;
72              
73 4     4   21538 use Chess::Board;
  4         10  
  4         284  
74 4     4   743 use Chess::Piece;
  4         8  
  4         717  
75 4     4   25 use base 'Chess::Piece';
  4         7  
  4         397  
76 4     4   33 use strict;
  4         11  
  4         1868  
77              
78             sub new {
79 17     17 1 45 my ($caller, $sq, $color, $desc) = @_;
80 17   33     108 my $class = ref($caller) || $caller;
81 17         81 my $self = $caller->SUPER::new($sq, $color, $desc);
82 17         64 return bless $self, $class;
83             }
84              
85             sub reachable_squares {
86 511     511 1 730 my ($self) = @_;
87 511         3000 my $csq = $self->get_current_square();
88 511         2076 my $tsq = Chess::Board->add_vert_distance($csq, 2);
89 511         967 my @squares = ( );
90 511 100       1409 if (defined($tsq)) {
91 285         925 my $sq = Chess::Board->square_right_of($tsq);
92 285 100       918 push @squares, $sq if (defined($sq));
93 285         933 $sq = Chess::Board->square_left_of($tsq);
94 285 100       897 push @squares, $sq if (defined($sq));
95             }
96 511         1742 $tsq = Chess::Board->add_vert_distance($csq, -2);
97 511 100       1474 if (defined($tsq)) {
98 278         835 my $sq = Chess::Board->square_right_of($tsq);
99 278 100       1075 push @squares, $sq if (defined($sq));
100 278         856 $sq = Chess::Board->square_left_of($tsq);
101 278 100       851 push @squares, $sq if (defined($sq));
102             }
103 511         1615 $tsq = Chess::Board->add_horz_distance($csq, 2);
104 511 100       1254 if (defined($tsq)) {
105 311         892 my $sq = Chess::Board->square_up_from($tsq);
106 311 100       914 push @squares, $sq if (defined($sq));
107 311         1006 $sq = Chess::Board->square_down_from($tsq);
108 311 100       1165 push @squares, $sq if (defined($sq));
109             }
110 511         1613 $tsq = Chess::Board->add_horz_distance($csq, -2);
111 511 100       1268 if (defined($tsq)) {
112 286         876 my $sq = Chess::Board->square_up_from($tsq);
113 286 100       809 push @squares, $sq if (defined($sq));
114 286         813 $sq = Chess::Board->square_down_from($tsq);
115 286 100       928 push @squares, $sq if (defined($sq));
116             }
117 511         7219 return @squares;
118             }
119              
120             1;