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/>.
24 Board::Board() : board(), dir_array()
27 // Initialize attack and direction arrays
28 for (const Square
&from
: SQUARES
) {
32 for (const Square
&to
: SQUARES
) {
36 int diff
= 0x77 + from
- to
;
37 for (const PieceType
& t
: NOT_PAWN_TYPES
) {
38 for (const Direction
&d
: PIECES_DIRS
[t
]) {
42 Square s
= static_cast<Square
>(from
+ d
);
45 attack_array
[diff
][t
] = true;
55 s
= static_cast<Square
>(s
+ d
);
66 * Generate a string for pretty printing the content of a 0x88 string array.
67 * This could be the board but also any PST array.
69 std::string
Board::to_string(const std::string squares
[], const int sq_width
)
71 std::ostringstream stream
;
73 for (Square s
= A8
; s
< OUT
; s
= Square(s
+ 1)) {
77 if (file(s
) == FILE_A
) {
79 for (int i
= 0; i
< 8; ++i
) {
80 // Every string representing a square
81 // should have the same size.
83 assert(squares
[s
].size() ==
84 static_cast<unsigned int>(sq_width
));
85 for (int j
= 0; j
< sq_width
; ++j
) {
91 stream
<< " " << rank(s
) + 1 << " ";
95 if (file(s
) == FILE_H
) {
96 stream
<< "|" << std::endl
;
98 break; // The loop ends here
100 s
= Square(s
- 0x18);
104 // Bottom's border of the array
106 for (int i
= 0; i
< 8; ++i
) {
107 for (int j
= 0; j
< sq_width
; ++j
) {
112 stream
<< std::endl
<< " ";
114 // Output files names
115 for (char c
= 'a'; c
<= 'h'; ++c
) {
116 int l
= sq_width
/ 2;
117 int r
= sq_width
% 2;
118 stream
<< std::setw(l
+ 2) << c
<< std::setw(r
) << " ";
125 * Pretty print the board
127 std::ostream
& operator<<(std::ostream
& out
, const Board board
)
129 std::string squares
[BOARD_SIZE
];
130 for (int i
= 0; i
< BOARD_SIZE
; ++i
) {
131 Square s
= Square(i
);
133 if (!board
.is_empty(s
)) {
134 squares
[i
] += board
[s
].to_string();
135 } else if (board
.is_dark(s
)) {
142 out
<< board
.to_string(squares
, 3);