aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-09-14 04:25:14 +0300
committereug-vs <eugene@eug-vs.xyz>2022-09-14 04:25:14 +0300
commitefa8671eecdabcdccecde87c1d05acbc89295f49 (patch)
treef195b50fd97de340d3a594f953bde6dff04a691d
parentf7901374649cd76ac52a6c881c79dc8d64e00981 (diff)
downloadj1chess-efa8671eecdabcdccecde87c1d05acbc89295f49.tar.gz
feat: parse FEN to generate a board
-rw-r--r--src/board.c90
-rw-r--r--src/board.h2
2 files changed, 92 insertions, 0 deletions
diff --git a/src/board.c b/src/board.c
index 59b6043..83fe8ce 100644
--- a/src/board.c
+++ b/src/board.c
@@ -1,4 +1,5 @@
#include <stdio.h>
+#include <string.h>
#include "board.h"
int pop_count(Bitboard bb){
@@ -30,3 +31,92 @@ void precompute_knight_attack_table(Bitboard attacks[64]) {
((position & notAFile) >> 17);
}
}
+
+Board parse_FEN(char* FEN) {
+ Board board;
+ for (int i = 0; i < 12; i++) board.pieces[i] = 0;
+
+ int rank = 7;
+ int file = 0;
+
+ for (int k = 0; k < strlen(FEN); k++) {
+ int c = FEN[k];
+ int index = rank * 8 + file;
+ Bitboard position = (Bitboard)1 << index;
+
+ if (c > '0' && c <= '8') file += c - '0';
+ else {
+ switch (c) {
+ case 'R':
+ board.pieces[ROOK] |= position;
+ break;
+ case 'N':
+ board.pieces[KNIGHT] |= position;
+ break;
+ case 'B':
+ board.pieces[BISHOP] |= position;
+ break;
+ case 'Q':
+ board.pieces[QUEEN] |= position;
+ break;
+ case 'K':
+ board.pieces[KING] |= position;
+ break;
+ case 'P':
+ board.pieces[PAWN] |= position;
+ break;
+ case 'r':
+ board.pieces[ROOK | BLACK] |= position;
+ break;
+ case 'n':
+ board.pieces[KNIGHT | BLACK] |= position;
+ break;
+ case 'b':
+ board.pieces[BISHOP | BLACK] |= position;
+ break;
+ case 'q':
+ board.pieces[QUEEN | BLACK] |= position;
+ break;
+ case 'k':
+ board.pieces[KING | BLACK] |= position;
+ break;
+ case 'p':
+ board.pieces[PAWN | BLACK] |= position;
+ break;
+ case '/':
+ rank--;
+ file = -1; // So that it becomes 0
+ break;
+ case ' ': // TODO: parse everything after
+ return board;
+ }
+ file++;
+ }
+ }
+ return board;
+}
+
+
+void print_board(Board board) {
+ printf("\n");
+ for (int rank = 7; rank >= 0; rank--) {
+ printf("%i|", rank + 1);
+ for (int file = 0; file < 8; file++) {
+ int index = rank * 8 + file;
+ Bitboard position = (Bitboard)1 << index;
+
+ int found = 0;
+ for (int piece = 0; piece < 12; piece++) {
+
+ if (board.pieces[piece] & position) {
+ found = 1;
+ printf("%s ", pieces[piece]);
+ break;
+ }
+ }
+ if (!found) printf(". ");
+ }
+ printf("\n");
+ }
+ printf(" a b c d e f g h\n");
+}
diff --git a/src/board.h b/src/board.h
index 154878c..c9d8153 100644
--- a/src/board.h
+++ b/src/board.h
@@ -58,3 +58,5 @@ static const char* pieces[] = {
void precompute_knight_attack_table();
void print_bitboard(Bitboard bb);
int pop_count(Bitboard bb);
+void print_board(Board board);
+Board parse_FEN(char* FEN);