Remove unused part of Search code
[purplehaze.git] / src / board.cpp
blob1a4a48e9674e9bc8f92c23fac65b6bdebea484bf
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 <cassert>
18 #include <iomanip>
19 #include <iostream>
20 #include <sstream>
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)) {
75 continue;
77 if (file(s) == FILE_A) {
78 stream << " +";
79 for (int i = 0; i < 8; ++i) {
80 // Every string representing a square
81 // should have the same size.
82 assert(sq_width > 0);
83 assert(squares[s].size() ==
84 static_cast<unsigned int>(sq_width));
85 for (int j = 0; j < sq_width; ++j) {
86 stream << "-";
88 stream << "+";
90 stream << std::endl;
91 stream << " " << rank(s) + 1 << " ";
93 stream << "|";
94 stream << squares[s];
95 if (file(s) == FILE_H) {
96 stream << "|" << std::endl;
97 if (s == H1) {
98 break; // The loop ends here
100 s = Square(s - 0x18);
104 // Bottom's border of the array
105 stream << " +";
106 for (int i = 0; i < 8; ++i) {
107 for (int j = 0; j < sq_width; ++j) {
108 stream << "-";
110 stream << "+";
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) << " ";
120 stream << std::endl;
121 return stream.str();
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);
132 squares[i] = " ";
133 if (!board.is_empty(s)) {
134 squares[i] += board[s].to_string();
135 } else if (board.is_dark(s)) {
136 squares[i] += ".";
137 } else {
138 squares[i] += " ";
140 squares[i] += " ";
142 out << board.to_string(squares, 3);
143 return out;