| 1 | # | ||
| 2 | # Games::Chess:Piece::Pawn | ||
| 3 | # | ||
| 4 | |||
| 5 | package Games::Chess::Piece::Pawn; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use vars qw(@ISA); | ||
| 9 | |||
| 10 | use Games::Chess::Piece::General; | ||
| 11 | use Games::Chess qw(:constants); | ||
| 12 | |||
| 13 | @ISA = qw(Games::Chess::Piece::General); | ||
| 14 | |||
| 15 | |||
| 16 | # | ||
| 17 | # ::new() | ||
| 18 | # | ||
| 19 | |||
| 20 | sub new ($) | ||
| 21 | { | ||
| 22 | my $class = shift; | ||
| 23 | |||
| 24 | die "Must specify color" unless @_; | ||
| 25 | |||
| 26 | my $this = Games::Chess::Piece::General->new(&PAWN, $_[0]); | ||
| 27 | |||
| 28 | bless($this, $class); | ||
| 29 | |||
| 30 | return $this; | ||
| 31 | } | ||
| 32 | |||
| 33 | |||
| 34 | # | ||
| 35 | # ::can_occupy() | ||
| 36 | # | ||
| 37 | |||
| 38 | sub can_occupy ($$$) | ||
| 39 | { | ||
| 40 | my ($this, $from, $to); | ||
| 41 | my $home_rank; | ||
| 42 | my $direction; | ||
| 43 | |||
| 44 | if ($this->color == Games::Chess::White) { | ||
| 45 | $home_rank = 2; | ||
| 46 | $direction = 1; | ||
| 47 | } else { | ||
| 48 | $home_rank = 7; | ||
| 49 | $direction = -1; | ||
| 50 | } | ||
| 51 | |||
| 52 | my @diff = Games::Chess::Square::diff($from, to); | ||
| 53 | |||
| 54 | return 0 unless ($diff[0] == 0); | ||
| 55 | return 0 unless (abs($diff[1]) <= 2); | ||
| 56 | return 0 unless (abs($diff[1]) >= 1); | ||
| 57 | return 0 if ($from->rank() != $home_rank) and (abs($diff[1]) == 2) | ||
| 58 | return 0 if (($diff[1] * $direction) < 0); | ||
| 59 | |||
| 60 | return 0; | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | # | ||
| 65 | # ::can_capture() | ||
| 66 | # | ||
| 67 | |||
| 68 | sub can_capture ($$$) | ||
| 69 | { | ||
| 70 | my ($this, $from, $to); | ||
| 71 | |||
| 72 | my @diff = Games::Chess::Square::diff($from, $to); | ||
| 73 | |||
| 74 | return 0 unless (($diff[0] == 1) or ($diff[0] == -1)); | ||
| 75 | |||
| 76 | if ($this->color == Games::Chess::White) { | ||
| 77 | return 0 unless ($diff[1] == 1); | ||
| 78 | } else { | ||
| 79 | return 0 unless ($diff[1] == -1); | ||
| 80 | } | ||
| 81 | |||
| 82 | return 1; | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | |||
| 87 | ##################################################################################### | ||
| 88 | # | ||
| 89 | # Additions to Games::Chess::Piece | ||
| 90 | # | ||
| 91 | ##################################################################################### | ||
| 92 | |||
| 93 | package Games::Chess::Piece; | ||
| 94 | |||
| 95 | |||
| 96 | # | ||
| 97 | # can_capture() | ||
| 98 | # | ||
| 99 | # Default implementation of can_capture() calls can_move() since most pieces have | ||
| 100 | # the same rules for captures and moves. Pawns, in particular, will override | ||
| 101 | # can_capture() so that their different capabilities are represented. | ||
| 102 | # | ||
| 103 | |||
| 104 | sub can_capture ($$$) | ||
| 105 | { | ||
| 106 | return can_move(@_); | ||
| 107 | } | ||
| 108 | |||
| 109 | |||
| 110 | # | ||
| 111 | # End of file. | ||
| 112 | # | ||
| 113 |