-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.cpp
More file actions
52 lines (42 loc) · 1.39 KB
/
helpers.cpp
File metadata and controls
52 lines (42 loc) · 1.39 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
#include <vector>
#include "position.h"
#include "movegen.h"
std::vector<Square> legalDestinations(Position& pos, Square from) {
MoveList list;
generateMoves(pos, list);
std::vector<Square> dests;
Colour us = pos.sideToMove;
for (int i = 0; i < list.count; i++) {
Move m = list.moves[i];
if (m.from() != from) continue;
Piece moving = pos.pieceOn(m.from());
uint8_t prevC = pos.castlingRights;
Square prevE = pos.enPassantSquare;
int prevH = pos.halfMoveClock;
Piece captured = pos.makeMove(m);
bool illegal = pos.isInCheck(us);
pos.unmakeMove(m, moving, captured, prevC, prevE, prevH);
if (!illegal) dests.push_back(m.to());
}
return dests;
}
bool tryMove(Position& pos, Square from, Square to) {
MoveList list;
generateMoves(pos, list);
Colour us = pos.sideToMove;
for (int i = 0; i < list.count; i++) {
Move m = list.moves[i];
if (m.from() != from || m.to() != to) continue;
Piece moving = pos.pieceOn(m.from());
uint8_t prevC = pos.castlingRights;
Square prevE = pos.enPassantSquare;
int prevH = pos.halfMoveClock;
Piece captured = pos.makeMove(m);
if (pos.isInCheck(us)) {
pos.unmakeMove(m, moving, captured, prevC, prevE, prevH);
continue;
}
return true;
}
return false;
}