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/>.
29 Piece board
[BOARD_SIZE
];
30 std::bitset
<7> attack_array
[240];
31 Direction dir_array
[240];
35 static std::string
to_string(const std::string squares
[],
38 Piece
& operator[] (const Square s
) {
41 const Piece
& operator[] (const Square s
) const {
45 bool is_empty(const Square s
) const {
46 return board
[s
].type() == EMPTY
;
48 static bool is_out(const Square s
) {
51 static bool is_dark(const Square s
) {
52 return (s
& 7) % 2 == (s
>> 4) % 2;
54 static bool is_same_color(const Square a
, const Square b
) {
55 return (is_dark(a
) && is_dark(b
)) || (!is_dark(a
) && !is_dark(b
));
57 static File
file(const Square s
) {
60 static Rank
rank(const Square s
) {
63 static bool is_pawn_begin(const Color c
, const Square s
) {
64 return (rank(s
) - RANK_6
* c
) == RANK_2
;
66 static bool is_pawn_end(const Color c
, const Square s
) {
67 return (rank(s
) + RANK_8
* c
) == RANK_8
;
69 static bool is_border(const Square s
) {
70 const File f
= file(s
);
71 const Rank r
= rank(s
);
72 return f
== FILE_A
|| f
== FILE_H
|| r
== RANK_1
|| r
== RANK_8
;
74 static Square
square(const int i
) {
75 return static_cast<Square
>(i
+ (i
& ~7));
77 static Square
square(const File f
, const Rank r
) {
78 return static_cast<Square
>(16 * r
+ f
);
80 static Square
square(const Square s
, const Direction d
) {
81 return static_cast<Square
>(s
+ d
);
84 static Square
flip(const Square s
) {
85 return square(file(s
), Rank(RANK_8
- rank(s
)));
87 static Square
flip(const Square s
, const Color c
) {
88 return static_cast<Square
>(s
+ A8
* c
);
91 // Theoretical answer by array lookup
92 bool can_attack(const PieceType t
,
93 const Square from
, const Square to
) const {
94 return static_cast<bool>(attack_array
[0x77 + from
- to
][t
]);
96 Direction
direction_to(const Square from
, const Square to
) const {
97 return dir_array
[0x77 + from
- to
];
100 // Practical answer (implemented in 'attack.cpp')
101 bool is_attacked_by(const Color c
, const Square s
,
102 const Pieces
& pieces
) const;
103 bool can_go(const Piece p
, const Square from
, const Square to
) const;
105 friend std::ostream
& operator<<(std::ostream
& out
, const Board board
);
108 #endif /* !BOARD_H */