-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpieces.h
174 lines (149 loc) · 6.62 KB
/
pieces.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
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
#include<iostream>
#include <cmath>
#include <map>
using namespace std;
typedef uint64_t U64;
U64 whitePawn(int rank,U64 squareIndex,const char selectedpiece[],map<char, int> filemapping,U64 blackPieces){
U64 lookup=0;
if (!(squareIndex << 8 & blackPieces)) {
lookup |= squareIndex << 8;
cout << selectedpiece[0] << rank + 2 << ' ';
if (rank == 1 && !(squareIndex << 16 & blackPieces)) {
lookup |= squareIndex << 16;
cout << selectedpiece[0] << rank + 3 << ' ';
}
}
if (squareIndex << 9 & blackPieces && int(log2(squareIndex << 9)) >> 3 == rank + 1) {
lookup |= squareIndex << 9;
auto it = filemapping.find(selectedpiece[0] + 1);
if (it != filemapping.end()) {
cout << "Pawn can capture on: " << it->first << rank + 2 << endl;
}
}
if (squareIndex << 7 & blackPieces && int(log2(squareIndex << 7)) >> 3 == rank + 1) {
lookup |= squareIndex << 7;
auto it = filemapping.find(selectedpiece[0] - 1);
if (it != filemapping.end()) {
cout << "Pawn can capture on: " << it->first << rank + 2 << endl;
}
}
return lookup;
}
U64 blackPawn(int rank,U64 squareIndex,const char selectedpiece[],map<char, int> filemapping,U64 whitePieces){
U64 lookup=0;
if (!(squareIndex >> 8 & whitePieces)) {
lookup |= squareIndex >> 8;
cout << selectedpiece[0] << rank << ' ';
if (rank == 6 && !(squareIndex >> 16 & whitePieces)) {
lookup |= squareIndex >> 16;
cout << selectedpiece[0] << rank - 1 << ' ';
}
}
if (squareIndex >> 9 & whitePieces && int(log2(squareIndex >> 9)) >> 3 == rank - 1) {
lookup |= squareIndex >> 9;
auto it = filemapping.find(selectedpiece[0] - 1);
if (it != filemapping.end()) {
cout << "Pawn can capture on: " << it->first << rank << endl;
}
}
if (squareIndex >> 7 & whitePieces && int(log2(squareIndex >> 7)) >> 3 == rank - 1) {
lookup |= squareIndex >> 7;
auto it = filemapping.find(selectedpiece[0] + 1);
if (it != filemapping.end()) {
cout << "Pawn can capture on: " << it->first << rank << endl;
}
}
return lookup;
}
U64 Knight(int rank,int file, U64 squareIndex,map<string,U64> pieces,U64 whitePieces,U64 blackPieces){
U64 lookup=0;
int knightMoves[8][2] = {
{2, 1}, {2, -1}, {-2, 1}, {-2, -1},
{1, 2}, {1, -2}, {-1, 2}, {-1, -2}
};
for (auto &move : knightMoves) {
int newRank = rank + move[0];
int newFile = file + move[1];
if (newRank >= 0 && newRank < 8 && newFile >= 0 && newFile < 8) {
U64 newSquareIndex = 1ULL << (8 * newRank + newFile);
if ((squareIndex & pieces["whiteKnights"] && !(newSquareIndex & whitePieces)) ||
(squareIndex & pieces["blackKnights"] && !(newSquareIndex & blackPieces))) {
lookup |= newSquareIndex;
cout << (char)('a' + newFile) << newRank + 1 << ' ';
}
}
}
return lookup;
}
U64 King(int rank,int file,U64 squareIndex,map<string,U64> pieces,U64 whitePieces,U64 blackPieces){
U64 lookup=0;
int kingMoves[8][2] = {
{1, 0}, {-1, 0}, {0, 1}, {0, -1},
{1, 1}, {1, -1}, {-1, 1}, {-1, -1}
};
for (auto &move : kingMoves) {
int newRank = rank + move[0];
int newFile = file + move[1];
if (newRank >= 0 && newRank < 8 && newFile >= 0 && newFile < 8) {
U64 newSquareIndex = 1ULL << (8 * newRank + newFile);
if ((squareIndex & pieces["whiteKing"] && !(newSquareIndex & whitePieces)) ||
(squareIndex & pieces["blackKing"] && !(newSquareIndex & blackPieces))) {
lookup |= newSquareIndex;
cout << (char)('a' + newFile) << newRank + 1 << ' ';
}
}
}
return lookup;
}
U64 Rook(int rank, int file, U64 squareIndex, map<string, U64> pieces, U64 whitePieces, U64 blackPieces) {
U64 lookup = 0;
int rookDirections[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool isWhite = (squareIndex & pieces["whiteRooks"]) || (squareIndex & pieces["whiteQueens"]);
for (auto &direction : rookDirections) {
int newRank = rank;
int newFile = file;
while (true) {
newRank += direction[0];
newFile += direction[1];
if (newRank < 0 || newRank >= 8 || newFile < 0 || newFile >= 8) break;
U64 newSquareIndex = 1ULL << (8 * newRank + newFile);
if (isWhite) {
if (newSquareIndex & whitePieces) break;
} else {
if (newSquareIndex & blackPieces) break;
}
lookup |= newSquareIndex;
cout << (char)('a' + newFile) << newRank + 1 << ' ';
if (isWhite && (newSquareIndex & blackPieces)) break;
if (!isWhite && (newSquareIndex & whitePieces)) break;
}
}
cout << endl;
return lookup;
}
U64 Bishop(int rank, int file, U64 squareIndex, map<string, U64> pieces, U64 whitePieces, U64 blackPieces) {
U64 lookup = 0;
int bishopDirections[4][2] = {{1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
bool isWhite = (squareIndex & pieces["whiteBishops"]) || (squareIndex & pieces["whiteQueens"]);
for (auto &direction : bishopDirections) {
int newRank = rank;
int newFile = file;
while (true) {
newRank += direction[0];
newFile += direction[1];
if (newRank < 0 || newRank >= 8 || newFile < 0 || newFile >= 8) break;
U64 newSquareIndex = 1ULL << (8 * newRank + newFile);
if (isWhite) {
if (newSquareIndex & whitePieces) break;
} else {
if (newSquareIndex & blackPieces) break;
}
lookup |= newSquareIndex;
cout << (char)('a' + newFile) << newRank + 1 << ' ';
if (isWhite && (newSquareIndex & blackPieces)) break;
if (!isWhite && (newSquareIndex & whitePieces)) break;
}
}
cout << endl;
return lookup;
}