Add Game::is_dangerous() for Futility Pruning
[purplehaze.git] / src / board.h
blobc5748699e7b964b73e07290e7f2c8ab57e5990c5
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);
37 Piece get_piece(Square s) const {
38 return board[s];
40 void set_piece(Piece p, Square s) {
41 board[s] = p;
43 bool is_empty(Square s) const {
44 return board[s].get_type() == EMPTY;
46 static bool is_out(Square s) {
47 return s & 0x88;
49 static bool is_dark(Square s) {
50 return (s & 7) % 2 == (s >> 4) % 2;
52 static bool is_same_color(Square a, Square b) {
53 return (is_dark(a) && is_dark(b)) || (!is_dark(a) && !is_dark(b));
55 static File get_file(Square s) {
56 return File(s & 7);
58 static Rank get_rank(Square s) {
59 return Rank(s >> 4);
61 static bool is_pawn_begin(Color c, Square s) {
62 return (get_rank(s) - RANK_6 * c) == RANK_2;
64 static bool is_pawn_end(Color c, Square s) {
65 return (get_rank(s) + RANK_8 * c) == RANK_8;
67 static bool is_border(Square s) {
68 File f = get_file(s);
69 Rank r = get_rank(s);
70 if (f == FILE_A || f == FILE_H) return true;
71 if (r == RANK_1 || r == RANK_8) return true;
72 return false;
74 static Square get_square(int i) {
75 return Square(i + (i & ~7));
77 static Square get_square(File f, Rank r) {
78 return Square(16 * r + f);
80 static Square get_square(Square s, Direction d) {
81 return Square(s + d);
84 // Used for PST with black's point of view
85 static Square flip(Square s) {
86 return get_square(get_file(s), Rank(RANK_8 - get_rank(s)));
89 // Theoretical answer by array lookup
90 bool can_attack(PieceType t, Square from, Square to) const {
91 return static_cast<bool>(attack_array[0x77 + from - to][t]);
93 Direction get_direction_to(Square from, Square to) const {
94 return dir_array[0x77 + from - to];
97 // Practical answer
98 bool is_attacked_by(Color c, Square s, const Pieces& pieces) const;
99 bool can_go(Piece p, Square from, Square to) const;
101 friend std::ostream& operator<<(std::ostream& out, const Board board);
104 #endif /* !BOARD_H */