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)) {
74 if (is_out(s
)) continue;
75 if (file(s
) == FILE_A
) {
77 for (int i
= 0; i
< 8; ++i
) {
78 // Every string representing a square
79 // should have the same size.
81 assert(squares
[s
].size() ==
82 static_cast<unsigned int>(sq_width
));
83 for (int j
= 0; j
< sq_width
; ++j
) {
89 stream
<< " " << rank(s
) + 1 << " ";
93 if (file(s
) == FILE_H
) {
94 stream
<< "|" << std::endl
;
95 if (s
== H1
) break; // The loop ends here
100 // Bottom's border of the array
102 for (int i
= 0; i
< 8; ++i
) {
103 for (int j
= 0; j
< sq_width
; ++j
) {
108 stream
<< std::endl
<< " ";
110 // Output files names
111 for (char c
= 'a'; c
<= 'h'; ++c
) {
112 int l
= sq_width
/ 2;
113 int r
= sq_width
% 2;
114 stream
<< std::setw(l
+ 2) << c
<< std::setw(r
) << " ";
121 * Pretty print the board
123 std::ostream
& operator<<(std::ostream
& out
, const Board board
)
125 std::string squares
[BOARD_SIZE
];
126 for (int i
= 0; i
< BOARD_SIZE
; ++i
) {
127 Square s
= Square(i
);
129 if (!board
.is_empty(s
)) {
130 squares
[i
] += board
[s
].to_string();
131 } else if (board
.is_dark(s
)) {
138 out
<< board
.to_string(squares
, 3);