From f7d65e8af8ae95a2583ea3441d9c41ea2d40d240 Mon Sep 17 00:00:00 2001 From: "rektdie@gmail.com" Date: Sun, 3 Mar 2024 15:43:56 +0100 Subject: [PATCH] moved imports from cpp files to headers + Makefile bugfix --- Makefile | 11 ++----- source/bitboards.cpp | 1 - source/bitboards.h | 1 + source/board.cpp | 2 -- source/board.h | 3 +- source/main.cpp | 2 +- source/movegen.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++ source/movegen.h | 17 ++++++---- source/types.h | 2 +- 9 files changed, 97 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 63e44b5..d2cc181 100644 --- a/Makefile +++ b/Makefile @@ -20,14 +20,9 @@ Chess-Test$(EXE_EXT): $(OBJS) g++ -o $@ $^ # Rule to compile source files to object files -$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(SRC_DIR)/types.h | $(OBJ_DIR) g++ -c -o $@ $< -# Dependencies -main.o: $(SRC_DIR)/main.cpp $(SRC_DIR)/types.h $(SRC_DIR)/board.h $(SRC_DIR)/movegen.h -bitboards.o: $(SRC_DIR)/bitboards.cpp $(SRC_DIR)/bitboards.h -board.o: $(SRC_DIR)/board.cpp $(SRC_DIR)/board.h -movegen.o: $(SRC_DIR)/movegen.cpp $(SRC_DIR)/movegen.h - # Create the object directory if it doesn't exist -$(shell mkdir $(OBJ_DIR)) +$(OBJ_DIR): + mkdir -p $@ diff --git a/source/bitboards.cpp b/source/bitboards.cpp index 7e70cf9..752970c 100644 --- a/source/bitboards.cpp +++ b/source/bitboards.cpp @@ -1,4 +1,3 @@ -#include #include "bitboards.h" void Bitboard::PrintBoard() const { diff --git a/source/bitboards.h b/source/bitboards.h index 7318a8c..5d994fa 100644 --- a/source/bitboards.h +++ b/source/bitboards.h @@ -1,4 +1,5 @@ #pragma once +#include #include "types.h" struct Bitboard { diff --git a/source/board.cpp b/source/board.cpp index d0940f8..2e9132c 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -1,6 +1,4 @@ #include "board.h" -#include "types.h" -#include "bitboards.h" #include void Board::Init() { diff --git a/source/board.h b/source/board.h index 875ab38..bdef468 100644 --- a/source/board.h +++ b/source/board.h @@ -1,6 +1,7 @@ #pragma once -#include "bitboards.h" #include +#include "bitboards.h" +#include "types.h" class Board { public: diff --git a/source/main.cpp b/source/main.cpp index b7ece75..1b1b0e2 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -8,7 +8,7 @@ int main() { board.Init(); initLeaperAttacks(); - maskRookAttacks(e4).PrintBoard(); + rookAttacksOnTheFly(e4, board.occupied).PrintBoard(); return 0; } \ No newline at end of file diff --git a/source/movegen.cpp b/source/movegen.cpp index fb47454..7f1cfff 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -118,6 +118,84 @@ Bitboard maskRookAttacks(int square) { return attacks; } +Bitboard bishopAttacksOnTheFly(int square, Bitboard block) { + Bitboard attacks; + + // target rank and files + int tr = square / 8; + int tf = square % 8; + + Bitboard currentSquare; + + // generate bishop attacks + + // North West + for (int r = tr + 1, f = tf - 1; r <= 7 && f >= 0; r++, f--) { + currentSquare = (1ULL << (r * 8 + f)); + if ((currentSquare & block).GetBoard()) break; + attacks |= currentSquare; + } + // North East + for (int r = tr + 1, f = tf + 1; r <= 7 && f <= 7; r++, f++) { + currentSquare = (1ULL << (r * 8 + f)); + if ((currentSquare & block).GetBoard()) break; + attacks |= currentSquare; + } + //South West + for (int r = tr - 1, f = tf - 1; r >= 0 && f >= 0; r--, f--) { + currentSquare = (1ULL << (r * 8 + f)); + if ((currentSquare & block).GetBoard()) break; + attacks |= currentSquare; + } + //South East + for (int r = tr - 1, f = tf + 1; r >= 0 && f <= 7; r--, f++) { + currentSquare = (1ULL << (r * 8 + f)); + if ((currentSquare & block).GetBoard()) break; + attacks |= currentSquare; + } + + return attacks; +} + +Bitboard rookAttacksOnTheFly(int square, Bitboard block) { + Bitboard attacks; + + // target rank and files + int tr = square / 8; + int tf = square % 8; + + Bitboard currentSquare; + + // generate rook attacks + + // North + for (int r = tr + 1; r <= 7; r++) { + currentSquare = (1ULL << (r * 8 + tf)); + if ((currentSquare & block).GetBoard()) break; + attacks |= currentSquare; + } + //South + for (int r = tr - 1; r >= 0; r--) { + currentSquare = (1ULL << (r * 8 + tf)); + if ((currentSquare & block).GetBoard()) break; + attacks |= currentSquare; + } + // East + for (int f = tf + 1; f <= 7; f++) { + currentSquare = (1ULL << (tr * 8 + f)); + if ((currentSquare & block).GetBoard()) break; + attacks |= currentSquare; + } + //West + for (int f = tf - 1; f >= 0; f--) { + currentSquare = (1ULL << (tr * 8 + f)); + if ((currentSquare & block).GetBoard()) break; + attacks |= currentSquare; + } + + return attacks; +} + void initLeaperAttacks() { for (int square = 0; square < 64; square++) { pawnAttacks[White][square] = maskPawnAttacks(White, square); diff --git a/source/movegen.h b/source/movegen.h index 260f280..9d10e02 100644 --- a/source/movegen.h +++ b/source/movegen.h @@ -1,5 +1,4 @@ #pragma once -#include #include "bitboards.h" // pawn attacks table [side][square] @@ -7,19 +6,25 @@ extern Bitboard pawnAttacks[2][64]; extern Bitboard knightAttacks[64]; extern Bitboard kingAttacks[64]; -// generate pawn attacks +// generate pawn masks Bitboard maskPawnAttacks(int square, int side); -// generate knight attacks +// generate knight masks Bitboard maskKnightAttacks(int square); -// generate king attacks +// generate king masks Bitboard maskKingAttacks(int square); -// generate bishop attacks +// generate bishop masks Bitboard maskBishopAttacks(int square); -// generate rook attacks +//generate bishop attacks +Bitboard bishopAttacksOnTheFly(int square, Bitboard block); + +// generate rook masks Bitboard maskRookAttacks(int square); +//generate rook attacks +Bitboard rookAttacksOnTheFly(int square, Bitboard block); + void initLeaperAttacks(); \ No newline at end of file diff --git a/source/types.h b/source/types.h index e002e48..a86fdb3 100644 --- a/source/types.h +++ b/source/types.h @@ -4,7 +4,7 @@ using U64 = uint64_t; -constexpr char StartingFen[] = "3b4/3P1N2/2b1P2K/n4r2/6Pk/3p2r1/2P1qQ2/6n1 w - - 0 1"; +constexpr char StartingFen[] = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; enum COLOR { White,