king move

This commit is contained in:
k 2025-06-19 04:33:40 -04:00
parent 76c69dbf18
commit 9f06b9d86c
3 changed files with 39 additions and 1 deletions

View File

@ -5,4 +5,5 @@ int pawnMove(game *g, move *moves);
int knightMove(game *g, move* moves); int knightMove(game *g, move* moves);
int rookMove(game *g, move *moves); int rookMove(game *g, move *moves);
int bishopMove(game *g, move *moves); int bishopMove(game *g, move *moves);
int kingMove(game *g, move *moves);
#endif #endif

View File

@ -54,12 +54,13 @@ int main() {
srand(time(NULL)); srand(time(NULL));
printf("readyok\n"); printf("readyok\n");
} else if (!strcmp("go", token)) { } else if (!strcmp("go", token)) {
move mov[1500];//big dumb buffer static move mov[1500];//big dumb buffer
int cnt = 0; int cnt = 0;
cnt += pawnMove(g,mov); cnt += pawnMove(g,mov);
cnt += knightMove(g,(mov+cnt)); cnt += knightMove(g,(mov+cnt));
cnt += rookMove(g,(mov+cnt)); cnt += rookMove(g,(mov+cnt));
cnt += bishopMove(g,(mov+cnt)); cnt += bishopMove(g,(mov+cnt));
cnt += kingMove(g,(mov+cnt));
move *m = &mov[rand() % cnt]; move *m = &mov[rand() % cnt];
char *end = ""; char *end = "";

View File

@ -6,6 +6,7 @@
int rookScan(game *g, move *moves, long long w, long long b); int rookScan(game *g, move *moves, long long w, long long b);
int bishopScan(game *g, move *moves, long long w, long long b); int bishopScan(game *g, move *moves, long long w, long long b);
int kingScan(game *g, move *moves, long long w, long long b);
int pawnMove(game *g, move *moves) { int pawnMove(game *g, move *moves) {
long long pawns = g->whiteToMove ? g->white.pawns : g->black.pawns; long long pawns = g->whiteToMove ? g->white.pawns : g->black.pawns;
@ -236,3 +237,38 @@ int bishopScan(game *g, move *moves, long long w, long long b) {
} }
return index; return index;
} }
int kingMove(game *g, move *moves){
return kingScan(g,moves,g->white.king,g->black.king);
}
int kingScan(game *g, move *moves, long long w, long long b) {
long long king = g->whiteToMove ? w : b;
long long occupied = g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
int index = 0;
for (int i = 0; i < 64; i++){
long long bit = 1LL << i;
int x = i % 8;
int y = i / 8;
int movesX[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int movesY[] = {-1, -1, -1, 0, 0, 1, 1, 1};
if(!(king & bit))
continue;
for (int j = 0; j < 8; ++j) {
int toX = x + movesX[j];
int toY = y + movesY[j];
int to = (toY * 8) + toX;
long long destBit = 1LL << to;
if (toX >= 0 && toX < 8 && toY >= 0 && toY < 8) {
if (!(occupied & destBit)) {
moves[index++] = (move){.From = i, .To = to};
}
}
}
}
return index;
}