basic bit board with fen loading

Missing moves loading
This commit is contained in:
k 2025-06-11 11:14:43 -04:00
parent c2d539e6a9
commit 5c58c86fba
2 changed files with 134 additions and 1 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/a.out

134
main.c
View File

@ -1,11 +1,50 @@
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFF_SIZE 4096
typedef struct {
long long pawns;
long long knights;
long long bishops;
long long rooks;
long long queen;
long long king;
} sets;
typedef struct {
sets white;
sets black;
bool whiteToMove;
} game;
game *fenGame(char *str);
void print_bitboard(long long bitboard) {
for (int i = 63; i >= 0; i--) {
// Check if the i-th bit is set
if ((bitboard >> i) & 1) {
printf("1 ");
} else {
printf("0 ");
}
// Print a newline every 8 bits (for rows)
if (i % 8 == 0) {
printf("\n");
}
}
printf("\n");
}
int main() {
setbuf(stdin, NULL);
setbuf(stdout, NULL);
game *g = NULL;
char line[BUFF_SIZE];
char *lineRest, *token;
@ -19,12 +58,28 @@ int main() {
token = strtok_r(line, " ", &lineRest);
if (!strcmp("uci", token)) {
printf("id name RatChess 0.0\n");
printf("id author rat<3\n\n");
printf("id author rat<3\n");
printf("uciok\n");
} else if (!strcmp("quit", token)) {
return 0;
} else if (!strcmp("setoption", token)) {
} else if (!strcmp("position", token)) {
game *g;
token = strtok_r(lineRest, " ", &lineRest);
if (!strcmp("fen", token)) {
g = fenGame(lineRest + 1);
for (int i = 0; i < 6; i++)
token = strtok_r(lineRest, " ", &lineRest);
} else {
g = fenGame("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
}
token = strtok_r(lineRest, " ", &lineRest);
if (!strcmp("moves", token)) {
// TODO
}
print_bitboard(g->white.pawns);
print_bitboard(g->black.pawns);
} else if (!strcmp("ucinewgame", token)) {
} else if (!strcmp("isready", token)) {
printf("readyok\n");
@ -34,3 +89,80 @@ int main() {
}
}
}
game *fenGame(char *str) {
int rank = 7;
int file = 0;
size_t pos = 0;
game *g;
g = malloc(sizeof(game));
memset(g, 0, sizeof(game));
while (str[pos] != '\0' && str[pos] != ' ') {
char current_char = *str;
if (isdigit(*str)) {
int empty_squares = *str - '0';
file += empty_squares;
str++;
continue;
}
if (current_char == '/') {
rank--;
file = 0;
str++;
continue;
}
long long bit = 1LL << (rank * 8 + file);
switch (*str) {
case 'P':
g->white.pawns |= bit;
break;
case 'N':
g->white.knights |= bit;
break;
case 'B':
g->white.bishops |= bit;
break;
case 'R':
g->white.rooks |= bit;
break;
case 'Q':
g->white.queen |= bit;
break;
case 'K':
g->white.king |= bit;
break;
case 'p':
g->black.pawns |= bit;
break;
case 'n':
g->black.knights |= bit;
break;
case 'b':
g->black.bishops |= bit;
break;
case 'r':
g->black.rooks |= bit;
break;
case 'q':
g->black.queen |= bit;
break;
case 'k':
g->black.king |= bit;
break;
default:
fprintf(stderr,
"Error: Unknown piece character in FEN: %c at position %zu\n",
*str, pos);
break;
}
file++;
str++;
}
str++;
g->whiteToMove = (*str == 'w') ? true : false;
return g;
}