-
Notifications
You must be signed in to change notification settings - Fork 0
/
swap.c
220 lines (217 loc) · 9.63 KB
/
swap.c
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "chess.h"
#include "data.h"
/* last modified 09/23/09 */
/*
*******************************************************************************
* *
* Swap() is used to analyze capture moves to see whether or not they appear *
* to be profitable. The basic algorithm is extremely fast since it uses the*
* bitmaps to determine which squares are attacking the [target] square. *
* *
* The algorithm is quite simple. Using the attack bitmaps, we enumerate all*
* the pieces that are attacking [target] for either side. Then we simply *
* use the lowest piece (value) for the correct side to capture on [target]. *
* we continually "flip" sides taking the lowest piece each time. *
* *
* As a piece is used, if it is a sliding piece (pawn, bishop, rook or queen)*
* we remove the piece, then generate moves of bishop/queen or rook/queen *
* and then add those in to the attackers, removing any attacks that have *
* already been used. *
* *
*******************************************************************************
*/
int Swap(TREE * RESTRICT tree, int move, int wtm) {
register BITBOARD attacks, temp = 0, toccupied = OccupiedSquares;
register BITBOARD bsliders =
Bishops(white) | Bishops(black) | Queens(white) | Queens(black);
register BITBOARD rsliders =
Rooks(white) | Rooks(black) | Queens(white) | Queens(black);
register int attacked_piece;
register int piece;
register int color, nc = 1;
register int source = From(move);
register int target = To(move);
int swap_list[32];
/*
************************************************************
* *
* Determine which squares attack <target> for each side. *
* initialize by placing the piece on <target> first in *
* the list as it is being captured to start things off. *
* *
************************************************************
*/
attacks = AttacksTo(tree, target);
attacked_piece = pc_values[Captured(move)];
/*
************************************************************
* *
* The first piece to capture on <target> is the piece *
* standing on <source>. *
* *
************************************************************
*/
color = Flip(wtm);
swap_list[0] = attacked_piece;
piece = Piece(move);
attacked_piece = pc_values[piece];
Clear(source, toccupied);
if (piece != knight && piece != king) {
if (piece & 1)
attacks |= AttacksBishop(target, toccupied) & bsliders;
if (piece == pawn || piece & 4)
attacks |= AttacksRook(target, toccupied) & rsliders;
}
/*
************************************************************
* *
* Now pick out the least valuable piece for the correct *
* side that is bearing on <target>. As we find one, we *
* update the attacks (if this is a sliding piece) to get *
* the attacks for any sliding piece that is lined up *
* behind the attacker we are removing. *
* *
* Once we know there is a piece attacking the last *
* capturing piece, add it to the swap list and repeat *
* until one side has no more captures. *
* *
************************************************************
*/
for (attacks &= toccupied; attacks; attacks &= toccupied) {
for (piece = pawn; piece <= king; piece++)
if ((temp = Pieces(color, piece) & attacks))
break;
if (piece > king)
break;
toccupied ^= (temp & -temp);
if (piece != knight && piece != king) {
if (piece & 1)
attacks |= AttacksBishop(target, toccupied) & bsliders;
if (piece & 4)
attacks |= AttacksRook(target, toccupied) & rsliders;
}
swap_list[nc] = -swap_list[nc - 1] + attacked_piece;
attacked_piece = pc_values[piece];
if (swap_list[nc++] - attacked_piece > 0)
break;
color = Flip(color);
}
/*
************************************************************
* *
* Starting at the end of the sequence of values, use a *
* "minimax" like procedure to decide where the captures *
* will stop. *
* *
************************************************************
*/
while (--nc)
swap_list[nc - 1] = -Max(-swap_list[nc - 1], swap_list[nc]);
return (swap_list[0]);
}
/* last modified 06/08/10 */
/*
*******************************************************************************
* *
* SwapO() is used to analyze a move already made to see if it appears to be *
* safe. It is similar to Swap() except that the move has already been made *
* and we are checking to see whether the opponent can gain material by *
* capturing the piece just moved. *
* *
*******************************************************************************
*/
int SwapO(TREE * RESTRICT tree, int move, int wtm) {
register BITBOARD attacks, temp = 0, toccupied = OccupiedSquares;
register BITBOARD bsliders =
Bishops(white) | Bishops(black) | Queens(white) | Queens(black);
register BITBOARD rsliders =
Rooks(white) | Rooks(black) | Queens(white) | Queens(black);
register int attacked_piece;
register int piece;
register int color, nc = 1;
// register int source = From(move);
register int target = To(move);
int swap_list[32];
/*
************************************************************
* *
* Determine which squares attack <target> for each side. *
* initialize by placing the piece on <target> first in *
* the list as it is being captured to start things off. *
* *
************************************************************
*/
attacks = AttacksTo(tree, target);
attacked_piece = pc_values[Piece(move)];
/*
************************************************************
* *
* The first piece to capture on <target> is the piece *
* standing on that square. We have to find out the *
* least valuable attacker for that square first. *
* *
************************************************************
*/
color = Flip(wtm);
swap_list[0] = attacked_piece;
for (piece = pawn; piece <= king; piece++)
if ((temp = Pieces(color, piece) & attacks))
break;
if (piece > king)
return (0);
toccupied ^= (temp & -temp);
if (piece != knight && piece != king) {
if (piece & 1)
attacks |= AttacksBishop(target, toccupied) & bsliders;
if (piece & 4)
attacks |= AttacksRook(target, toccupied) & rsliders;
}
attacked_piece = pc_values[piece];
color = Flip(color);
/*
************************************************************
* *
* Now pick out the least valuable piece for the correct *
* side that is bearing on <target>. As we find one, we *
* update the attacks (if this is a sliding piece) to get *
* the attacks for any sliding piece that is lined up *
* behind the attacker we are removing. *
* *
* Once we know there is a piece attacking the last *
* capturing piece, add it to the swap list and repeat *
* until one side has no more captures. *
* *
************************************************************
*/
for (attacks &= toccupied; attacks; attacks &= toccupied) {
for (piece = pawn; piece <= king; piece++)
if ((temp = Pieces(color, piece) & attacks))
break;
if (piece > king)
break;
toccupied ^= (temp & -temp);
if (piece != knight && piece != king) {
if (piece & 1)
attacks |= AttacksBishop(target, toccupied) & bsliders;
if (piece & 4)
attacks |= AttacksRook(target, toccupied) & rsliders;
}
swap_list[nc] = -swap_list[nc - 1] + attacked_piece;
attacked_piece = pc_values[piece];
if (swap_list[nc++] - attacked_piece > 0)
break;
color = Flip(color);
}
/*
************************************************************
* *
* Starting at the end of the sequence of values, use a *
* "minimax" like procedure to decide where the captures *
* will stop. *
* *
************************************************************
*/
while (--nc)
swap_list[nc - 1] = -Max(-swap_list[nc - 1], swap_list[nc]);
return (swap_list[0]);
}