-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathboard.h
122 lines (96 loc) · 3.66 KB
/
board.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
Weiss is a UCI compliant chess engine.
Copyright (C) 2023 Terje Kirstihagen
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "types.h"
typedef struct {
Key key;
Key materialKey;
Bitboard checkers;
Move move;
Square epSquare;
int rule50;
int castlingRights;
} History;
typedef struct Position {
uint8_t board[64];
Bitboard pieceBB[7];
Bitboard colorBB[COLOR_NB];
Bitboard checkers;
int nonPawnCount[COLOR_NB];
int material;
int phaseValue;
int phase;
Color stm;
Square epSquare;
int rule50;
int castlingRights;
uint16_t histPly;
uint16_t gameMoves;
Key key;
Key materialKey;
Key pawnKey;
Key minorKey;
Key majorKey;
Key nonPawnKey[COLOR_NB];
uint64_t nodes;
int trend;
History gameHistory[256];
} Position;
extern bool Chess960;
extern const int NonPawn[PIECE_NB];
// Zobrist keys
extern uint64_t PieceKeys[PIECE_NB][64];
extern uint64_t CastleKeys[16];
extern uint64_t SideKey;
extern uint8_t CastlePerm[64];
extern Bitboard CastlePath[16];
extern Square RookSquare[16];
void ParseFen(const char *fen, Position *pos);
Key KeyAfter(const Position *pos, Move move);
bool SEE(const Position *pos, const Move move, const int threshold);
bool HasCycle(const Position *pos, int ply);
char *BoardToFen(const Position *pos);
#ifndef NDEBUG
void PrintBoard(const Position *pos);
bool PositionOk(const Position *pos);
#endif
#ifdef DEV
void PrintBoard(const Position *pos);
#endif
INLINE bool ValidPiece(const Piece piece) { return (wP <= piece && piece <= wK) || (bP <= piece && piece <= bK); }
INLINE bool ValidCapture(const Piece capt) { return (wP <= capt && capt <= wQ) || (bP <= capt && capt <= bQ); }
INLINE bool ValidPromotion(const Piece promo) { return (wN <= promo && promo <= wQ) || (bN <= promo && promo <= bQ); }
INLINE Color ColorOf(Piece piece) { return piece >> 3;}
INLINE PieceType PieceTypeOf(Piece piece) { return piece & 7; }
INLINE Piece MakePiece(Color color, PieceType pt) { return (color << 3) + pt; }
int Distance(const Square sq1, const Square sq2);
INLINE Square MirrorSquare(const Square sq) { return sq ^ 56; } // Mirrors a square horizontally
INLINE Square RelativeSquare(const Color color, const Square sq) { return color == WHITE ? sq : MirrorSquare(sq); }
INLINE Square BlackRelativeSquare(const Color color, const Square sq) { return color == BLACK ? sq : MirrorSquare(sq); }
INLINE int FileOf(Square square) { return square & 7; }
INLINE int RankOf(Square square) { return square >> 3; }
INLINE int RelativeRank(Color color, int rank) { return color == WHITE ? rank : RANK_8 - rank; }
INLINE Square MakeSquare(int rank, int file) { return (rank * FILE_NB) + file; }
INLINE Square StrToSq(const char *str) { return MakeSquare(str[1] - '1', str[0] - 'a'); }
INLINE void SqToStr(Square sq, char *str) {
str[0] = 'a' + FileOf(sq);
str[1] = '1' + RankOf(sq);
}
INLINE bool IsRepetition(const Position *pos) {
for (int i = 4; i <= pos->rule50 && i <= pos->histPly; i += 2)
if (pos->key == history(-i).key)
return true;
return false;
}