Fix TT lookup after search test case
[purplehaze.git] / src / board.h
blob297a93d17d530440884026d87acd0697b2b8692a
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 #ifndef BOARD_H
18 #define BOARD_H
20 #include <bitset>
21 #include <string>
23 #include "common.h"
24 #include "pieces.h"
26 class Board
28 private:
29 Piece board[BOARD_SIZE];
30 std::bitset<7> attack_array[240];
31 Direction dir_array[240];
33 public:
34 Board();
35 static std::string to_string(const std::string squares[],
36 const int sq_width);
38 Piece& operator[] (Square s) {
39 return board[s];
41 const Piece& operator[] (Square s) const {
42 return board[s];
45 bool is_empty(Square s) const {
46 return board[s].type() == EMPTY;
48 static bool is_out(Square s) {
49 return s & 0x88;
51 static bool is_dark(Square s) {
52 return (s & 7) % 2 == (s >> 4) % 2;
54 static bool is_same_color(Square a, Square b) {
55 return (is_dark(a) && is_dark(b)) || (!is_dark(a) && !is_dark(b));
57 static File file(Square s) {
58 return File(s & 7);
60 static Rank rank(Square s) {
61 return Rank(s >> 4);
63 static bool is_pawn_begin(Color c, Square s) {
64 return (rank(s) - RANK_6 * c) == RANK_2;
66 static bool is_pawn_end(Color c, Square s) {
67 return (rank(s) + RANK_8 * c) == RANK_8;
69 static bool is_border(Square s) {
70 File f = file(s);
71 Rank r = rank(s);
72 if (f == FILE_A || f == FILE_H) return true;
73 if (r == RANK_1 || r == RANK_8) return true;
74 return false;
76 static Square square(int i) {
77 return Square(i + (i & ~7));
79 static Square square(File f, Rank r) {
80 return Square(16 * r + f);
82 static Square square(Square s, Direction d) {
83 return Square(s + d);
86 // Used for PST with black's point of view
87 static Square flip(Square s) {
88 return square(file(s), Rank(RANK_8 - rank(s)));
91 // Theoretical answer by array lookup
92 bool can_attack(PieceType t, Square from, Square to) const {
93 return static_cast<bool>(attack_array[0x77 + from - to][t]);
95 Direction direction_to(Square from, Square to) const {
96 return dir_array[0x77 + from - to];
99 // Practical answer
100 bool is_attacked_by(Color c, Square s, const Pieces& pieces) const;
101 bool can_go(Piece p, Square from, Square to) const;
103 friend std::ostream& operator<<(std::ostream& out, const Board board);
106 #endif /* !BOARD_H */