File Coverage

blib/lib/Chess/Piece/Pawn.pm
Criterion Covered Total %
statement 41 43 95.3
branch 22 26 84.6
condition 7 15 46.6
subroutine 8 8 100.0
pod 3 3 100.0
total 81 95 85.2


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Chess::Piece::Pawn - a class representing a pawn in a chess game
4              
5             =head1 SYNOPSIS
6              
7             $pawn = Chess::Piece::Pawn->new("e2", "White King's pawn");
8             $true = $pawn->can_reach("e4");
9             $true = $pawn->can_reach("e3");
10             $true = $pawn->can_reach("f3");
11             $false = $pawn->can_reach("e5");
12             $queen = $pawn->promote("queen");
13              
14             =head1 DESCRIPTION
15              
16             The Chess module provides a framework for writing chess programs with Perl.
17             This class is part of that framework, representing a pawn in a
18             L.
19              
20             =head1 METHODS
21              
22             =head2 Construction
23              
24             =over 4
25              
26             =item new()
27              
28             Constructs a new Chess::Piece::Pawn. Requires a two scalar parameters
29             containing the square on which the pawn is to be constucted and its color,
30             Optionally takes a third parameter containing a text description of the pawn.
31              
32             $pawn = Chess::Piece::Pawn->new("d2", "white");
33             $pawn = Chess::Piece::Pawn->new("e2", "white",
34             "White King's pawn");
35              
36             =head2 Class methods
37              
38             There are no class methods for this class.
39              
40             =head2 Object methods
41              
42             =item reachable_squares()
43              
44             Overrides base class version. Returns a list of squares that this pawn can
45             reach from its current position. See L
46             for more details on this method.
47              
48             =item promote()
49              
50             Takes a parameter containing the type of piece to promote to. Returns itself
51             blessed as that type of piece. Returns undef and produces a warning (see
52             L if the piece is not one of 'bishop', 'knight', 'queen' or
53             'rook'.
54              
55             =head1 DIAGNOSTICS
56              
57             =over 4
58              
59             =item Can't promote a pawn to a 'king'
60              
61             You may only promote a pawn to a 'bishop', 'knight', 'queen' or 'rook'.
62              
63             =back
64              
65             =head1 BUGS
66              
67             Please report any bugs to the author.
68              
69             =head1 AUTHOR
70              
71             Brian Richardson
72              
73             =head1 COPYRIGHT
74              
75             Copyright (c) 2002, 2005 Brian Richardson. All rights reserved. This module
76             is Free Software. It may be modified and redistributed under the same terms as
77             Perl itself.
78              
79             =cut
80             package Chess::Piece::Pawn;
81              
82 6     6   68581 use Chess::Piece;
  6         14  
  6         154  
83 6     6   1945 use Chess::Board;
  6         13  
  6         155  
84 6     6   38 use base 'Chess::Piece';
  6         10  
  6         844  
85 6     6   46 use Carp;
  6         9  
  6         366  
86 6     6   30 use strict;
  6         9  
  6         2830  
87              
88             sub new {
89 69     69 1 159 my ($caller, $sq, $color, $desc) = @_;
90 69   33     279 my $class = ref($caller) || $caller;
91 69         257 my $self = $caller->SUPER::new($sq, $color, $desc);
92 69         233 return bless $self, $class;
93             }
94              
95             sub reachable_squares {
96 2018     2018 1 2288 my ($self) = @_;
97 2018         4902 my $color = $self->get_player();
98 2018         5215 my $csq = $self->get_current_square();
99 2018         2244 my $tsq1;
100 2018         2969 my @squares = ( );
101 2018 100       3995 if ($color eq 'white') {
102 997 50       4365 $tsq1 = Chess::Board->square_up_from($csq) if defined($csq);
103             }
104             else {
105 1021 50       3845 $tsq1 = Chess::Board->square_down_from($csq) if defined($csq);
106             }
107 2018 100       5264 push @squares, $tsq1 if defined($tsq1);
108 2018         2337 my $tsq2;
109 2018 100       3642 if ($color eq 'white') {
110 997 50       3440 $tsq2 = Chess::Board->square_up_from($tsq1) if defined($tsq1);
111             }
112             else {
113 1021 100       3669 $tsq2 = Chess::Board->square_down_from($tsq1) if defined($tsq1);
114             }
115 2018 100 100     5561 push @squares, $tsq2 if (!$self->moved() and defined($tsq2));
116 2018 100       8071 $tsq2 = Chess::Board->square_left_of($tsq1) if defined($tsq1);
117 2018 100       5319 push @squares, $tsq2 if (defined($tsq2));
118 2018 100       7124 $tsq2 = Chess::Board->square_right_of($tsq1) if defined($tsq1);
119 2018 100       5176 push @squares, $tsq2 if (defined($tsq2));
120 2018         23734 return @squares;
121             }
122              
123             sub promote {
124 3     3 1 7 my ($self, $new_rank) = @_;
125 3 50 33     87 unless (lc($new_rank) eq 'bishop' || lc($new_rank) eq 'knight' ||
      33        
      33        
126             lc($new_rank) eq 'rook' || lc($new_rank) eq 'queen') {
127 0         0 carp "Can't promote a pawn to a '$new_rank'";
128 0         0 return undef;
129             }
130 3         19 return bless $self, ('Chess::Piece::' . ucfirst($new_rank));
131             }
132              
133             1;