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/>.
26 // Initialize the board's squares
27 for (int i
= 0; i
< BOARD_SIZE
; ++i
) board
[i
] = Piece();
29 // Initialize the direction array
30 for (int i
= 0; i
< 240; ++i
) {
31 dir_array
[i
] = NO_DIR
;
34 // Initialize the attack array
35 for (int i
= 0; i
< 64; ++i
) {
36 Square from
= square(i
);
37 for (int j
= 0; j
< 64; ++j
) {
38 Square to
= square(j
);
39 int diff
= 0x77 + from
- to
;
40 for (const PieceType
& t
: NOT_PAWN_TYPES
) {
41 const Direction
* dirs
= PIECES_DIRS
[t
];
42 for (int d
= 0; d
< NB_DIRS
[t
]; ++d
) {
43 Square s
= Square(from
+ dirs
[d
]);
46 attack_array
[diff
].set(t
, true);
47 dir_array
[diff
] = dirs
[d
];
50 if (t
== KNIGHT
|| t
== KING
) break; // Leapers
51 s
= Square(s
+ dirs
[d
]); // Sliders
60 * Generate a string for pretty printing the content of a 0x88 string array.
61 * This could be the board but also any PST array.
63 std::string
Board::to_string(const std::string squares
[], const int sq_width
)
65 std::ostringstream stream
;
67 for (Square s
= A8
; s
< OUT
; s
= Square(s
+ 1)) {
68 if (is_out(s
)) continue;
69 if (file(s
) == FILE_A
) {
71 for (int i
= 0; i
< 8; ++i
) {
72 // Every string representing a square
73 // should have the same size.
75 assert(squares
[s
].size() ==
76 static_cast<unsigned int>(sq_width
));
77 for (int j
= 0; j
< sq_width
; ++j
) {
83 stream
<< " " << rank(s
) + 1 << " ";
87 if (file(s
) == FILE_H
) {
88 stream
<< "|" << std::endl
;
89 if (s
== H1
) break; // The loop ends here
94 // Bottom's border of the array
96 for (int i
= 0; i
< 8; ++i
) {
97 for (int j
= 0; j
< sq_width
; ++j
) {
102 stream
<< std::endl
<< " ";
104 // Output files names
105 for (char c
= 'a'; c
<= 'h'; ++c
) {
106 int l
= sq_width
/ 2;
107 int r
= sq_width
% 2;
108 stream
<< std::setw(l
+ 2) << c
<< std::setw(r
) << " ";
115 * Pretty print the board
117 std::ostream
& operator<<(std::ostream
& out
, const Board board
)
119 std::string squares
[BOARD_SIZE
];
120 for (int i
= 0; i
< BOARD_SIZE
; ++i
) {
121 Square s
= Square(i
);
123 if (!board
.is_empty(s
)) {
124 squares
[i
] += board
[s
].to_string();
125 } else if (board
.is_dark(s
)) {
132 out
<< board
.to_string(squares
, 3);