-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.cpp
More file actions
121 lines (99 loc) · 3.47 KB
/
board.cpp
File metadata and controls
121 lines (99 loc) · 3.47 KB
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
#include "Board.h"
#include <sstream>
#include <cctype>
#include "zobrist.h"
std::bitset<64> Board::getWhitePieces() const
{
return whitePawns | whiteKnights | whiteBishops | whiteRooks | whiteQueen | whiteKing;
}
std::bitset<64> Board::getBlackPieces() const
{
return blackPawns | blackKnights | blackBishops | blackRooks | blackQueen | blackKing;
}
std::bitset<64> Board::getAllPieces() const
{
return getWhitePieces() | getBlackPieces();
}
std::bitset<64> Board::getOwnPieces(bool whiteToMove) const
{
return whiteToMove
? getWhitePieces()
: getBlackPieces();
}
std::bitset<64> Board::getOpponentPieces(bool whiteToMove) const {
return whiteToMove
? getBlackPieces()
: getWhitePieces();
}
char Board::getPieceAt(int index) const {
if (index < 0 || index >= 64) return '.';
if (whitePawns[index]) return 'P';
if (whiteKnights[index]) return 'N';
if (whiteBishops[index]) return 'B';
if (whiteRooks[index]) return 'R';
if (whiteQueen[index]) return 'Q';
if (whiteKing[index]) return 'K';
if (blackPawns[index]) return 'p';
if (blackKnights[index]) return 'n';
if (blackBishops[index]) return 'b';
if (blackRooks[index]) return 'r';
if (blackQueen[index]) return 'q';
if (blackKing[index]) return 'k';
return '.';
}
bool Board::isEnemyPiece(char piece, bool white) const {
if (piece == '.') return false;
if (white) {
return (piece >= 'a' && piece <= 'z');
}
else {
return (piece >= 'A' && piece <= 'Z');
}
}
void Board::setFromFEN(const std::string& fen) {
whitePawns = whiteKnights = whiteBishops = whiteRooks = whiteQueen = whiteKing = 0;
blackPawns = blackKnights = blackBishops = blackRooks = blackQueen = blackKing = 0;
std::istringstream iss(fen);
std::string boardPart, activeColor, castling, enPassant;
iss >> boardPart >> activeColor >> castling >> enPassant;
int rank = 7;
int file = 0;
for (char c : boardPart) {
if (c == '/') {
rank--;
file = 0;
} else if (isdigit(c)) {
file += c - '0';
} else {
int index = rank * 8 + file;
switch(c) {
case 'P': whitePawns.set(index); break;
case 'N': whiteKnights.set(index); break;
case 'B': whiteBishops.set(index); break;
case 'R': whiteRooks.set(index); break;
case 'Q': whiteQueen.set(index); break;
case 'K': whiteKing.set(index); break;
case 'p': blackPawns.set(index); break;
case 'n': blackKnights.set(index); break;
case 'b': blackBishops.set(index); break;
case 'r': blackRooks.set(index); break;
case 'q': blackQueen.set(index); break;
case 'k': blackKing.set(index); break;
}
file++;
}
}
whiteToMove = (activeColor == "w");
whiteCanCastleKingside = castling.find('K') != std::string::npos;
whiteCanCastleQueenside = castling.find('Q') != std::string::npos;
blackCanCastleKingside = castling.find('k') != std::string::npos;
blackCanCastleQueenside = castling.find('q') != std::string::npos;
if (enPassant == "-") {
en_passant = -1;
} else {
char fileChar = enPassant[0];
char rankChar = enPassant[1];
en_passant = (rankChar - '1') * 8 + (fileChar - 'a');
}
hash = Zobrist::computeHash(*this);
}