Change testsuite output
[purplehaze.git] / src / board.cpp
blob9452c2c24d23f0bc8b02300c05926dce22670503
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/>.
17 #include <assert.h>
18 #include <iostream>
19 #include <sstream>
20 #include <iomanip>
22 #include "board.h"
24 Board::Board() : board(), dir_array()
27 // Initialize attack and direction arrays
28 for (const Square &from : SQUARES) {
29 if (from == OUT) {
30 break;
32 for (const Square &to : SQUARES) {
33 if (to == OUT) {
34 break;
36 int diff = 0x77 + from - to;
37 for (const PieceType& t : NOT_PAWN_TYPES) {
38 for (const Direction &d : PIECES_DIRS[t]) {
39 if (d == NO_DIR) {
40 break;
42 Square s = static_cast<Square>(from + d);
43 while (!is_out(s)) {
44 if (s == to) {
45 attack_array[diff][t] = true;
46 dir_array[diff] = d;
47 break;
49 switch (t) {
50 case KNIGHT:
51 case KING:
52 s = OUT;
53 break;
54 default:
55 s = static_cast<Square>(s + d);
56 break;
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;
72 stream << std::endl;
73 for (Square s = A8; s < OUT; s = Square(s + 1)) {
74 if (is_out(s)) continue;
75 if (file(s) == FILE_A) {
76 stream << " +";
77 for (int i = 0; i < 8; ++i) {
78 // Every string representing a square
79 // should have the same size.
80 assert(sq_width > 0);
81 assert(squares[s].size() ==
82 static_cast<unsigned int>(sq_width));
83 for (int j = 0; j < sq_width; ++j) {
84 stream << "-";
86 stream << "+";
88 stream << std::endl;
89 stream << " " << rank(s) + 1 << " ";
91 stream << "|";
92 stream << squares[s];
93 if (file(s) == FILE_H) {
94 stream << "|" << std::endl;
95 if (s == H1) break; // The loop ends here
96 s = Square(s - 0x18);
100 // Bottom's border of the array
101 stream << " +";
102 for (int i = 0; i < 8; ++i) {
103 for (int j = 0; j < sq_width; ++j) {
104 stream << "-";
106 stream << "+";
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) << " ";
116 stream << std::endl;
117 return stream.str();
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);
128 squares[i] = " ";
129 if (!board.is_empty(s)) {
130 squares[i] += board[s].to_string();
131 } else if (board.is_dark(s)) {
132 squares[i] += ".";
133 } else {
134 squares[i] += " ";
136 squares[i] += " ";
138 out << board.to_string(squares, 3);
139 return out;