This commit is contained in:
2024-04-01 01:33:47 +02:00
parent d898a9b691
commit edb7409e7f
2 changed files with 66 additions and 0 deletions

56
src/board.h Normal file
View File

@@ -0,0 +1,56 @@
#pragma once
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <assert.h>
typedef uint64_t bitmap_t;
typedef uint_fast8_t index_t;
typedef int_fast8_t piece_t;
enum tile_info {
EMPTY = 0b0000,
KING = 0b0001,
QUEEN = 0b0010,
ROOK = 0b0011,
BISHOP = 0b0100,
KNIGHT = 0b0101,
PAWN = 0b0110,
COLOR_BIT = 0b1000,
PIECE_UNKNOWN=-1
};
struct board {
static_assert(sizeof(bitmap_t) == 64/8, "bitmap_t must be 64 bits long");
bitmap_t pieces[4]; // 4 bits correspond to 1 tile
};
static inline bitmap_t bit(index_t i)
{
return 0b111UL << 3*i;
}
static bool is_white(piece_t piece)
{
return piece % 2 == 0;
}
static bool is_black(piece_t piece)
{
return piece % 2 == 1;
}
static piece_t piece_at(struct board* board, index_t tile)
{
for (size_t i = 0; i < sizeof board->pieces / board->pieces[0]; i++) {
if (bit(tile) & board->pieces[i]) {
return i;
}
}
return PIECE_UNKNOWN;
}

View File

@@ -1,5 +1,15 @@
enum chess_piece {
EMPTY = 0,
KING = 1,
QUEEN = 2,
ROOK = 3,
BISHOP = 4,
KNIGHT = 5,
PAWN = 6,
PIECE_COUNT,
};
static bool board_equals(