Pawn captures

This commit is contained in:
k 2025-06-15 02:33:50 -04:00
parent 33592762a7
commit b2433993a6

74
main.c
View File

@ -23,10 +23,24 @@ typedef struct {
bool whiteToMove; bool whiteToMove;
} game; } game;
typedef struct {
int From;
int To;
} move;
game *fenGame(char *str); game *fenGame(char *str);
void playMoves(game *g, char *moves); void playMoves(game *g, char *moves);
long long *findSet(game *g, long long bit); long long *findSet(game *g, long long bit);
long long *charToSet(game *g, char c); long long *charToSet(game *g, char c);
int pawnMove(game *g, move *moves);
long long fullSet(sets *s) {
return s->bishops ^ s->king ^ s->knights ^ s->pawns ^ s->queen ^ s->rooks;
}
long long fullSetBoth(game *g) {
return fullSet(&g->white) ^ fullSet(&g->black);
}
void print_bitboard(long long bitboard) { void print_bitboard(long long bitboard) {
for (int i = 63; i >= 0; i--) { for (int i = 63; i >= 0; i--) {
@ -77,18 +91,30 @@ int main() {
} }
playMoves(g, lineRest); playMoves(g, lineRest);
printf("info pawns\n"); printf("info pawns\n");
print_bitboard(g->white.pawns | g->black.pawns); // print_bitboard(fullSetBoth(g));
} else if (!strcmp("ucinewgame", token)) { } else if (!strcmp("ucinewgame", token)) {
} else if (!strcmp("isready", token)) { } else if (!strcmp("isready", token)) {
printf("readyok\n"); printf("readyok\n");
} else if (!strcmp("go", token)) { } else if (!strcmp("go", token)) {
if (g->whiteToMove) {
printf("bestmove %c2%c4\n", ltz[cnt], ltz[cnt]); move mov[64];
int cnt = pawnMove(g, mov);
move *m = &mov[0];
char *end = "";
int yTo, xTo, yFrom, xFrom;
xTo = m->To % 8;
yTo = m->To / 8 + 1;
xFrom = m->From % 8;
yFrom = m->From / 8 + 1;
if ((g->whiteToMove && yTo == 8) || (!g->whiteToMove && yTo == 1)) {
end = "q\n";
} else { } else {
printf("bestmove %c7%c5\n", ltz[cnt], ltz[cnt]); end = "\n";
} }
cnt++; printf("bestmove %c%d%c%d%s", ltz[xFrom], yFrom, ltz[xTo], yTo, end);
} }
} }
} }
@ -179,6 +205,7 @@ void playMoves(game *g, char *moves) {
*tmp &= (~bit); *tmp &= (~bit);
if (strlen(move) == 5) { if (strlen(move) == 5) {
char c = move[4]; char c = move[4];
if (g->whiteToMove)
c = toupper(c); c = toupper(c);
set = charToSet(g, c); set = charToSet(g, c);
} }
@ -218,3 +245,40 @@ long long *findSet(game *g, long long bit) {
return NULL; return NULL;
} }
} }
int pawnMove(game *g, move *moves) {
long long pawns = g->whiteToMove ? g->white.pawns : g->black.pawns;
long long enemy = g->whiteToMove ? fullSet(&g->black) : fullSet(&g->white);
long long occupied = fullSetBoth(g);
int movdir = g->whiteToMove ? 8 : -8;
int capLeft = g->whiteToMove ? 7 : -9;
int capRight = g->whiteToMove ? 9 : -7;
size_t index = 0;
for (int i = 0; i < 64; ++i) {
long long bit = 1LL << i;
if (!(pawns & bit))
continue;
// forword
int to = i + movdir;
if (to >= 0 && to < 64 && !(occupied & (1LL << to))) {
moves[index++] = (move){.From = i, .To = to};
}
// left
to = i + capLeft;
if (i % 8 > 0 && (to >= 0 && to < 64) && (enemy & (1LL << to))) {
moves[index++] = (move){.From = i, .To = to};
}
// right
to = i + capRight;
if (i % 8 < 7 && (to >= 0 && to < 64) && (enemy & (1LL << to))) {
moves[index++] = (move){.From = i, .To = to};
}
}
return index;
}