knight moves

This commit is contained in:
k 2025-06-15 13:45:04 -04:00
parent b2433993a6
commit 8624840700

53
main.c
View File

@ -33,6 +33,7 @@ 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); int pawnMove(game *g, move *moves);
int knightMove(game *g, move* moves);
long long fullSet(sets *s) { long long fullSet(sets *s) {
return s->bishops ^ s->king ^ s->knights ^ s->pawns ^ s->queen ^ s->rooks; return s->bishops ^ s->king ^ s->knights ^ s->pawns ^ s->queen ^ s->rooks;
@ -90,16 +91,13 @@ int main() {
g = fenGame("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); g = fenGame("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
} }
playMoves(g, lineRest); playMoves(g, lineRest);
printf("info pawns\n");
// 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)) {
move mov[64]; move mov[200];
int cnt = pawnMove(g, mov); int cnt = knightMove(g, mov);
move *m = &mov[0]; move *m = &mov[0];
char *end = ""; char *end = "";
@ -110,7 +108,8 @@ int main() {
yFrom = m->From / 8 + 1; yFrom = m->From / 8 + 1;
if ((g->whiteToMove && yTo == 8) || (!g->whiteToMove && yTo == 1)) { if ((g->whiteToMove && yTo == 8) || (!g->whiteToMove && yTo == 1)) {
end = "q\n"; // end = "q\n";
end = "\n";
} else { } else {
end = "\n"; end = "\n";
} }
@ -246,6 +245,48 @@ long long *findSet(game *g, long long bit) {
} }
} }
int knightMove(game *g, move *moves) {
long long knights = g->whiteToMove ? g->white.knights : g->black.knights;
long long occupied = g->whiteToMove ? fullSet(&g->white) : fullSet(&g->black);
int kMoves[] = {
-17, -15,
-10, -6,
6, 10,
15, 17
};
int index = 0;
for (int i = 0; i < 64; i++) {
long long bit = 1LL << i;
if (!(knights & bit))
continue;
for (int j = 0; j < 8; ++j) {
int to = i + kMoves[j];
if (to < 0 || to > 64)
continue;
int fromFile = i % 8;
int toFile = to % 8;
int fromRank = i / 8;
int toRank = to / 8;
int fileDiff = abs(fromFile - toFile);
int rankDiff = abs(fromRank - toRank);
if (!((fileDiff == 1 && rankDiff == 2) || (fileDiff == 2 && rankDiff == 1)))
continue;
long long destBit = 1LL << to;
if (occupied & destBit) // Skip if destination has friendly piece
continue;
moves[index++] = (move){.From = i, .To = to};
}
}
return index;
}
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;
long long enemy = g->whiteToMove ? fullSet(&g->black) : fullSet(&g->white); long long enemy = g->whiteToMove ? fullSet(&g->black) : fullSet(&g->white);