-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsee.cpp
More file actions
96 lines (69 loc) · 2.23 KB
/
Copy pathsee.cpp
File metadata and controls
96 lines (69 loc) · 2.23 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
#include "see.h"
#include "bitboard.h"
#include <algorithm>
static inline int see_value(Piece p) {
static const int val[6] = { 100, 300, 300, 500, 900, 20000 };
return val[p];
}
static Bitboard attackers_to(const Board& pos, int sq, Bitboard occ, Color side) {
Bitboard atk = 0;
atk |= attacks_pawn(Color(side ^ 1), sq) & pos.pieceBB[side][PAWN];
atk |= attacks_knight(sq) & pos.pieceBB[side][KNIGHT];
atk |= bishop_attack(sq, occ) & (pos.pieceBB[side][BISHOP] | pos.pieceBB[side][QUEEN]);
atk |= rook_attack(sq, occ) & (pos.pieceBB[side][ROOK] | pos.pieceBB[side][QUEEN]);
atk |= attacks_king(sq) & pos.pieceBB[side][KING];
return atk;
}
int see(const Board& pos, Move m) {
uint64_t t0 = now_ns();
prof.see_calls++;
// -------------------------
// YOUR ORIGINAL SEE CODE
// -------------------------
int from = from_sq(m);
int to = to_sq(m);
int flag = flags_of(m);
Color side = pos.color_at(from);
if (side == NO_COLOR)
return 0;
Piece attacker = pos.piece_at(from);
Piece victim;
if (flag == FLAG_ENPASSANT) {
int capSq = (side == WHITE ? to - 8 : to + 8);
victim = pos.piece_at(capSq);
}
else {
victim = pos.piece_at(to);
}
if (victim == NO_PIECE)
return 0;
int gain[32];
int depth = 0;
if (victim < PAWN || victim > KING)
return 0;
gain[0] = see_value(victim);
Bitboard occ = pos.occupiedBB;
occ &= ~(1ULL << from);
Piece current = attacker;
while (true) {
depth++;
side = Color(side ^ 1);
Bitboard atk = attackers_to(pos, to, occ, side) & occ;
if (!atk)
break;
int sq = lsb(atk);
current = pos.piece_at(sq);
gain[depth] = see_value(current) - gain[depth - 1];
occ &= ~(1ULL << sq);
if (gain[depth] < 0)
break;
}
while (--depth)
gain[depth - 1] = -std::max(-gain[depth - 1], gain[depth]);
int result = gain[0];
// -------------------------
// END OF YOUR CODE
// -------------------------
prof.see_ns += now_ns() - t0;
return result;
}