diff options
Diffstat (limited to 'src/engine')
| -rw-r--r-- | src/engine/bitboard.c | 58 | ||||
| -rw-r--r-- | src/engine/bitboard.h | 87 | ||||
| -rw-r--r-- | src/engine/fen.c | 197 | ||||
| -rw-r--r-- | src/engine/fen.h | 12 | ||||
| -rw-r--r-- | src/engine/ints.h | 20 | ||||
| -rw-r--r-- | src/engine/moves.c | 2 | ||||
| -rw-r--r-- | src/engine/moves.h | 4 |
7 files changed, 3 insertions, 377 deletions
diff --git a/src/engine/bitboard.c b/src/engine/bitboard.c deleted file mode 100644 index 565f425..0000000 --- a/src/engine/bitboard.c +++ /dev/null @@ -1,58 +0,0 @@ -#include "bitboard.h" -#include <stdio.h> -#include <inttypes.h> -#include <assert.h> - -position_t position_starting() { - return (position_t) { - .bitboards = { - 16ULL, - 8ULL, - 129ULL, - 36ULL, - 66ULL, - 65280ULL, - 1152921504606846976ULL, - 576460752303423488ULL, - 9295429630892703744ULL, - 2594073385365405696ULL, - 4755801206503243776ULL, - 71776119061217280ULL, - }, - .castling = - WHITE_SHORT_CASTLE | WHITE_LONG_CASTLE | - BLACK_SHORT_CASTLE | BLACK_LONG_CASTLE, - .turn = WHITE_TURN, - .passantable_file = 0, - .halfmove_clock = 0, - .fullmove_clock = 1 - }; -} - -void assert_valid_position(position_t position) { - assert(check_valid_position(position) == true); -} - -bool check_valid_position(position_t position) { - if (position.castling > - ( - WHITE_SHORT_CASTLE | WHITE_LONG_CASTLE | - BLACK_SHORT_CASTLE | BLACK_LONG_CASTLE - ) - ) return false; - if (position.turn != WHITE_TURN && position.turn != BLACK_TURN) return false; - if (position.passantable_file > 8) return false; // 0 means no passant - if (position.bitboards[WHITE_KING] == 0) return false; - if (position.bitboards[BLACK_KING] == 0) return false; - return true; -} - -void print_bitboard(bitboard_t bitboard) { - printf("Bitboard(%" PRIu64 ")\n", bitboard); - for (int i = 7; i >= 0; i--) { - for (int j = 0; j < 8; j++) { - printf("%" PRIu64 "", (bitboard >> (8 * i + j)) & 1); - } - printf("\n"); - } -} diff --git a/src/engine/bitboard.h b/src/engine/bitboard.h deleted file mode 100644 index ecd8c35..0000000 --- a/src/engine/bitboard.h +++ /dev/null @@ -1,87 +0,0 @@ -#ifndef BITBOARD_H -#define BITBOARD_H - -#include "ints.h" -#include <stdbool.h> - -typedef u64 bitboard_t; - -enum { - WHITE_KING, - WHITE_QUEEN, - WHITE_ROOK, - WHITE_BISHOP, - WHITE_KNIGHT, - WHITE_PAWN, - BLACK_KING, - BLACK_QUEEN, - BLACK_ROOK, - BLACK_BISHOP, - BLACK_KNIGHT, - BLACK_PAWN, - PIECE_TYPE_COUNT, -}; - -enum { - WHITE_SHORT_CASTLE = 1 << 0, - WHITE_LONG_CASTLE = 1 << 1, - BLACK_SHORT_CASTLE = 1 << 2, - BLACK_LONG_CASTLE = 1 << 3, -}; - -enum { - WHITE_TURN, - BLACK_TURN, -}; - -#define BITMASK_RANK_1 255ULL -#define BITMASK_RANK_2 65280ULL -#define BITMASK_RANK_3 16711680ULL -#define BITMASK_RANK_4 4278190080ULL -#define BITMASK_RANK_5 1095216660480ULL -#define BITMASK_RANK_6 280375465082880ULL -#define BITMASK_RANK_7 71776119061217280ULL -#define BITMASK_RANK_8 18374686479671623680ULL -#define BITMASK_FILE_A 72340172838076673ULL -#define BITMASK_FILE_B 144680345676153346ULL -#define BITMASK_FILE_C 289360691352306692ULL -#define BITMASK_FILE_D 578721382704613384ULL -#define BITMASK_FILE_E 1157442765409226768ULL -#define BITMASK_FILE_F 2314885530818453536ULL -#define BITMASK_FILE_G 4629771061636907072ULL -#define BITMASK_FILE_H 9259542123273814144ULL - -#define occupied_by(bitboard, index) (bitboard & ((bitboard_t)1 << index)) - -typedef struct { - bitboard_t bitboards[PIECE_TYPE_COUNT]; - u8 castling; - u8 turn; - u8 passantable_file; - u16 halfmove_clock; - u16 fullmove_clock; -} position_t; - -position_t position_starting(); -void assert_valid_position(position_t position); -bool check_valid_position(position_t position); -#define whites(position) \ - position.bitboards[WHITE_KING] | \ - position.bitboards[WHITE_QUEEN] | \ - position.bitboards[WHITE_ROOK] | \ - position.bitboards[WHITE_BISHOP] | \ - position.bitboards[WHITE_KNIGHT] | \ - position.bitboards[WHITE_PAWN] - -#define blacks(position) \ - position.bitboards[BLACK_KING] | \ - position.bitboards[BLACK_QUEEN] | \ - position.bitboards[BLACK_ROOK] | \ - position.bitboards[BLACK_BISHOP] | \ - position.bitboards[BLACK_KNIGHT] | \ - position.bitboards[BLACK_PAWN] - - -void print_bitboard(bitboard_t bitboard); - -#endif // BITBOARD_H diff --git a/src/engine/fen.c b/src/engine/fen.c deleted file mode 100644 index aa258c9..0000000 --- a/src/engine/fen.c +++ /dev/null @@ -1,197 +0,0 @@ -#include <assert.h> -#include <stdbool.h> -#include "fen.h" -#include "bitboard.h" - -int get_castling(char c) { - if (c == 'K') return WHITE_SHORT_CASTLE; - if (c == 'Q') return WHITE_LONG_CASTLE; - if (c == 'k') return BLACK_SHORT_CASTLE; - if (c == 'q') return BLACK_LONG_CASTLE; - return -1; -} - -int get_turn(char c) { - if (c == 'w') return WHITE_TURN; - if (c == 'b') return BLACK_TURN; - return -1; -} - -int get_piece_enum_item(char c) { - if (c == 'P') return WHITE_PAWN; - if (c == 'N') return WHITE_KNIGHT; - if (c == 'B') return WHITE_BISHOP; - if (c == 'R') return WHITE_ROOK; - if (c == 'Q') return WHITE_QUEEN; - if (c == 'K') return WHITE_KING; - - if (c == 'p') return BLACK_PAWN; - if (c == 'n') return BLACK_KNIGHT; - if (c == 'b') return BLACK_BISHOP; - if (c == 'r') return BLACK_ROOK; - if (c == 'q') return BLACK_QUEEN; - if (c == 'k') return BLACK_KING; - - return -1; -} - -/* - * from Wikipedia - * Piece placement data - * Each rank is described, starting with rank 8 and ending with rank 1, - * with a "/" between each one; within each rank, the contents of the - * squares are described in order from the a-file to the h-file. - * Each piece is identified by a single letter taken from the standard - * English names in algebraic notation - * (pawn = "P", knight = "N", bishop = "B", - * rook = "R", queen = "Q", and king = "K"). - * White pieces are designated using uppercase letters ("PNBRQK"), - * while black pieces use lowercase letters ("pnbrqk"). - * A set of one or more consecutive empty squares within a rank is denoted - * by a digit from "1" to "8", corresponding to the number of squares. - * Active color - * "w" means that White is to move; "b" means that Black is to move. - * Castling availability - * If neither side has the ability to castle, this field uses the character - * "-". Otherwise, this field contains one or more letters: - * "K" if White can castle kingside, - * "Q" if White can castle queenside, - * "k" if Black can castle kingside, - * and "q" if Black can castle queenside. - * A situation that temporarily prevents castling does not - * prevent the use of this notation. - * En passant target square - * This is a square over which a pawn has just passed while moving two - * squares; it is given in algebraic notation. - * If there is no en passant target square, this field uses the character "-". - * This is recorded regardless of whether there is a pawn in position to - * capture en passant. An updated version of the spec has since made it - * so the target square is recorded only if a legal en passant capture is - * possible, but the old version of the standard - * is the one most commonly used. - * Halfmove clock - * The number of halfmoves since the last capture or pawn advance, - * used for the fifty-move rule. - * Fullmove number - * The number of the full moves. - * It starts at 1 and is incremented after Black's move. - * - * e.g. 1B6/2n5/p1N1P2R/P1K3N1/4Pk2/1Q2p2p/6nP/1B4R1 w - - 0 1 - */ -struct fen_load load_fen(const char* fen) { - position_t position = {0}; - - int square = 56; - int i = -1; - char c; - while ((c = fen[++i]) != ' ') { - if (c >= '0' && c <= '9') { - if (c == '9') return (struct fen_load) { true, position }; - square += c - '0'; - continue; - } - if (c == '/') { - square -= 16; - continue; - } - - int piece_type = get_piece_enum_item(c); - if (piece_type == -1) return (struct fen_load) { true, position }; - position.bitboards[piece_type] |= (bitboard_t)1 << square++; - } - - c = fen[++i]; - int turn = get_turn(c); - if (turn == -1) return (struct fen_load) { true, position }; - position.turn = turn; - if (fen[++i] != ' ') return (struct fen_load) { true, position }; - - while ((c = fen[++i]) != ' ') { - if (c == '-') continue; - int castling = get_castling(c); - if (castling == -1) return (struct fen_load) { true, position }; - position.castling |= castling; - } - - c = fen[++i]; - if (c >= 'a' && c <= 'z') { - if (c > 'h') return (struct fen_load) { true, position }; - position.passantable_file = c - 'a' + 1; - c = fen[++i]; - if (c < '1' && c > '8') return (struct fen_load) { true, position }; - } else { - position.passantable_file = 0; - } - if (fen[++i] != ' ') return (struct fen_load) { true, position }; - - while ((c = fen[++i]) != ' ') { - if (c < '0' || c > '9') return (struct fen_load) { true, position }; - position.halfmove_clock *= 10; - position.halfmove_clock += c - '0'; - } - - while ((c = fen[++i]) != 0) { - if (c < '0' || c > '9') return (struct fen_load) { true, position }; - position.fullmove_clock *= 10; - position.fullmove_clock += c - '0'; - } - - bool error = !check_valid_position(position); - return (struct fen_load) { error, position }; -} - -#ifdef TEST_MOD -#include "bitboard.c" - -bool test_fen_no_passant() { - struct fen_load r = load_fen("1B6/2n5/p1N1P2R/P1K3N1/4Pk2/1Q2p2p/6nP/1B4R1 w - - 0 1"); - if (r.failed) return false; - - position_t p = r.position; - if (p.castling != 0) return false; - if (p.passantable_file != 0) return false; - return true; -} -bool test_fen_passant() { - struct fen_load r = load_fen("rnbqkbnr/ppp1p1pp/8/3pPp2/8/8/PPPP1PPP/RNBQKBNR w KQkq f6 0 3"); - if (r.failed) return false; - - position_t p = r.position; - if ( - p.castling != ( - WHITE_LONG_CASTLE | WHITE_SHORT_CASTLE | - BLACK_LONG_CASTLE | BLACK_SHORT_CASTLE - ) - ) return false; - if (p.passantable_file != 6) return false; - return true; -} - -bool test_starting_position() { - struct fen_load r = load_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); - if (r.failed) return false; - - position_t position = r.position; - position_t expected = position_starting(); - - if (position.castling != expected.castling) return false; - if (position.passantable_file != expected.passantable_file) return false; - if (position.turn != expected.turn) return false; - if (position.fullmove_clock != expected.fullmove_clock) return false; - if (position.halfmove_clock != expected.halfmove_clock) return false; - if (position.bitboards[0] != expected.bitboards[0]) return false; - if (position.bitboards[1] != expected.bitboards[1]) return false; - if (position.bitboards[2] != expected.bitboards[2]) return false; - if (position.bitboards[3] != expected.bitboards[3]) return false; - if (position.bitboards[4] != expected.bitboards[4]) return false; - if (position.bitboards[5] != expected.bitboards[5]) return false; - if (position.bitboards[6] != expected.bitboards[6]) return false; - if (position.bitboards[7] != expected.bitboards[7]) return false; - if (position.bitboards[8] != expected.bitboards[8]) return false; - if (position.bitboards[9] != expected.bitboards[9]) return false; - if (position.bitboards[10] != expected.bitboards[10]) return false; - if (position.bitboards[11] != expected.bitboards[11]) return false; - - return true; -} -#endif diff --git a/src/engine/fen.h b/src/engine/fen.h deleted file mode 100644 index d45240e..0000000 --- a/src/engine/fen.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef FEN_H -#define FEN_H - -#include "bitboard.h" - -struct fen_load { - bool failed; - position_t position; -}; -struct fen_load load_fen(const char* fen); - -#endif // FEN_H diff --git a/src/engine/ints.h b/src/engine/ints.h deleted file mode 100644 index 4a0cfe3..0000000 --- a/src/engine/ints.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef INTS_H -#define INTS_H - -#include <stdint.h> -#include <inttypes.h> - -typedef uint8_t u8; -typedef uint16_t u16; -typedef uint32_t u32; -typedef uint64_t u64; -typedef __uint128_t u128; -typedef int8_t i8; -typedef int16_t i16; -typedef int32_t i32; -typedef int64_t i64; -typedef __int128_t i128; -typedef float f32; -typedef double f64; - -#endif // !INTS_H diff --git a/src/engine/moves.c b/src/engine/moves.c index d338cc2..797e770 100644 --- a/src/engine/moves.c +++ b/src/engine/moves.c @@ -1,5 +1,5 @@ #include "moves.h" -#include "bitboard.h" +#include "../bitboard.h" #include "moves/vec.c" #include "moves/king.c" #include "moves/knight.c" diff --git a/src/engine/moves.h b/src/engine/moves.h index bd88eeb..86c4844 100644 --- a/src/engine/moves.h +++ b/src/engine/moves.h @@ -1,8 +1,8 @@ #ifndef MOVES_H #define MOVES_H -#include "ints.h" -#include "bitboard.h" +#include "../ints.h" +#include "../bitboard.h" typedef u8 square_t; |
