commit 87e142b2ea66019107aa1f58db1aac2142c7ee44
parent 77b823ca04f4f0d674730586ad5f119cad4d256d
Author: benjamin paul <bpaul@bpaul.xyz>
Date: Sun, 10 Oct 2021 17:11:25 +1000
Started work on board representation
Diffstat:
6 files changed, 114 insertions(+), 13 deletions(-)
diff --git a/Makefile b/Makefile
@@ -5,7 +5,7 @@ NAME = bpaul-chess
CC = cc
CFLAGS = -march=native -O3 -flto
-OBJS = src/main.o
+OBJS = src/main.o src/bb.o src/gen.c
all: $(NAME)
diff --git a/src/bb.c b/src/bb.c
@@ -0,0 +1,20 @@
+#include <stdbool.h>
+
+#include "bb.h"
+
+bool
+testbit(bb b, unsigned char sq) {
+ return b & 1ULL << sq;
+}
+
+void
+setbit(bb *b, unsigned char sq) {
+ *b |= 1ULL << sq;
+}
+
+void
+popbit(bb *b, unsigned char sq) {
+ /* If the bit is known to be set, it would be one instruction shorter to
+ * subtract */
+ *b &= ~(1ULL << sq);
+}
diff --git a/src/bb.h b/src/bb.h
@@ -1,4 +1,6 @@
-#pragma once
-
/* Bitboard */
typedef unsigned long long bb;
+
+bool testbit(bb, unsigned char);
+void setbit(bb *, unsigned char);
+void popbit(bb *, unsigned char);
diff --git a/src/gen.c b/src/gen.c
@@ -0,0 +1,15 @@
+#include <stdbool.h>
+
+#include "bb.h"
+#include "pos.h"
+
+void
+gen_pseudo(pos *p) {
+ bb our_pieces[PIECE_CNT];
+
+ for (char pc = PAWN; pc <= KING; pc++) {
+ our_pieces[pc] = p->pieces[pc] & p->sides[p->turn];
+ }
+
+ return;
+}
diff --git a/src/pos.c b/src/pos.c
@@ -0,0 +1,60 @@
+#include "bb.h"
+#include "pos.h"
+
+unsigned char
+side(unsigned char p) {
+ return p >> 3;
+}
+
+unsigned char
+piece(unsigned char p) {
+ return p & 7;
+}
+
+void
+setpiece(pos *p, unsigned char piece, unsigned char sq) {
+ setbit(p->sides[side(piece)], sq);
+ setbit(p->pieces[piece(piece)], sq);
+}
+
+void
+parsefen(pos *p, char *fen) {
+ char phase, sq;
+
+ phase = 0;
+ sq = 63;
+
+ while (*fen) {
+ if (phase == 0) {
+ switch (*fen) {
+ case 'P': setpiece(p, wP, sq); break;
+ case 'N': setpiece(p, wN, sq); break;
+ case 'B': setpiece(p, wB, sq); break;
+ case 'R': setpiece(p, wR, sq); break;
+ case 'Q': setpiece(p, wQ, sq); break;
+ case 'K': setpiece(p, wK, sq); break;
+ case 'p': setpiece(p, bP, sq); break;
+ case 'n': setpiece(p, bN, sq); break;
+ case 'b': setpiece(p, bB, sq); break;
+ case 'r': setpiece(p, bR, sq); break;
+ case 'q': setpiece(p, bQ, sq); break;
+ case 'k': setpiece(p, bK, sq); break;
+
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ sq -= *fen - '0';
+
+ case '/':
+ sq = (sq & ~7) - 1;
+ }
+ }
+
+ fen++;
+ }
+}
diff --git a/src/pos.h b/src/pos.h
@@ -2,20 +2,24 @@
#include "bb.h"
-enum sides { WHITE, BLACK, SIDE_CNT };
+enum { WHITE, BLACK, SIDE_CNT };
-enum pieces { PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING, PIECE_CNT };
+enum { PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING, PIECE_CNT };
+
+/* Please format this better! */
+enum { wP, wN, wB, wR, wQ, wK,
+ bP = 8, bN, bB, bR, bQ, bK };
+
+typedef struct pos pos;
struct pos {
bb sides[SIDE_CNT];
bb pieces[PIECE_CNT];
- /* Castling permissions flags */
- char castle;
- /* En passant square */
- char enpas;
- /* Tracker for 50 move rule */
- unsigned char fifty;
- /* Track game length (hence the name) */
- unsigned short game_length;
+ char castle; /* Castling permissions flags */
+ char enpas; /* En passant square */
+ unsigned char fifty; /* Tracker for 50 move rule */
+ unsigned short game_length; /* Track game length (hence the name) */
char turn;
};
+
+void parsefen(pos *, char);