1 /* Copyright (C) 2007-2011 Vincent Ollivier
3 * Purple Haze is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * Purple Haze is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * Return true if the square s is attacked by one of the c color's pieces.
24 bool Board::is_attacked_by(Color c
, Square s
, const Pieces
& pieces
) const
26 for (const PieceType
& t
: NOT_PAWN_TYPES
) {
27 int n
= pieces
.count(c
, t
);
28 for (int i
= 0; i
< n
; ++i
) {
29 Square from
= pieces
.position(c
, t
, i
);
30 if (!can_attack(t
, from
, s
)) continue;
31 if (t
== KNIGHT
|| t
== KING
) return true;
32 Direction d
= direction_to(from
, s
);
33 Square to
= Square(from
+ d
);
34 while (to
!= s
&& is_empty(to
)) {
37 if (to
== s
) return true;
41 // Specific code for pawns
42 for (int i
= 0; i
< 2; ++i
) {
43 Square from
= Square(s
+ PAWN_CAPTURE_DIRS
[!c
][i
]);
44 if (board
[from
].type() == PAWN
&& board
[from
].color() == c
) {
53 * Return true if a piece p can do a capture or a quiet move from square from
54 * to square to on the board. This method does not say if the move is a legal
57 bool Board::can_go(Piece p
, Square from
, Square to
) const
59 PieceType t
= p
.type();
61 Direction d
= direction_to(from
, to
);
63 // A piece cannot capture another piece of the same color
64 if (!is_empty(to
) && board
[to
].color() == c
) return false;
70 push_dir
= (c
== WHITE
? UP
: DOWN
);
71 if (!is_empty(to
)) { // Capture
72 if (to
== Square(from
+ push_dir
+ LEFT
)) return true;
73 if (to
== Square(from
+ push_dir
+ RIGHT
)) return true;
74 } else { // Pawn push (and double push)
75 if (to
== Square(from
+ push_dir
)) return true;
76 if (to
== Square(from
+ 2 * push_dir
) &&
77 is_empty(Square(from
+ push_dir
)) &&
78 is_pawn_begin(c
, from
)) return true;
82 if (!can_attack(t
, from
, to
)) return false;
83 if (t
== KNIGHT
|| t
== KING
) return true;
85 while (s
!= to
&& is_empty(s
)) { // Search for a blocker
89 if (s
== to
) return true;