From a8e3ec70f8dcb9a8549efffeda19801bf937de1f Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Thu, 29 Feb 2024 09:18:19 +0100 Subject: [PATCH] add square attackers fct --- Makefile | 27 +++++++++++++++------ src/attack.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/attack.h | 22 +++++++++++++++++ 3 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 src/attack.c create mode 100644 src/attack.h diff --git a/Makefile b/Makefile index e73b8e4..76f193e 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,7 @@ TSTSRC := $(wildcard $(TSTDIR)/*.c) LIB := br_$(shell uname -m) # library name -DEP_FN := $(SRC_FN) $(LIBSRC_FN) +DEP_FN := $(SRC_FN) DEP := $(addprefix $(DEPDIR)/,$(DEP_FN:.c=.d)) TARGET_FN := brchess @@ -66,6 +66,7 @@ CPPFLAGS += -DDEBUG_DEBUG # enable log() funct #CPPFLAGS += -DDEBUG_DEBUG_C # enable log() settings CPPFLAGS += -DDEBUG_POOL # memory pools management #CPPFLAGS += -DDEBUG_FEN # FEN decoding +#CPPFLAGS += -DDEBUG_POS # position.c CPPFLAGS += -DDEBUG_MOVE # move generation CPPFLAGS += -DDEBUG_EVAL # eval functions CPPFLAGS += -DDEBUG_PIECE # piece list management @@ -76,7 +77,7 @@ CPPFLAGS := $(strip $(CPPFLAGS)) ##################################### compiler flags CFLAGS := -std=gnu11 -CFLAGS += -O3 +CFLAGS += -O1 CFLAGS += -g CFLAGS += -Wall CFLAGS += -Wextra @@ -263,23 +264,35 @@ memcheck: targets @$(VALGRIND) $(VALGRINDFLAGS) $(BINDIR)/brchess ##################################### test binaries -TEST = bin/fen-test bin/bitboard-test +TEST = bin/fen-test bin/bitboard-test bin/movegen-test \ + bin/mktestdata FENTESTOBJS = obj/fen.o obj/position.o obj/piece.o obj/bitboard.o \ obj/board.o obj/util.o BITBOARDOBJS = obj/fen.o obj/position.o obj/piece.o obj/bitboard.o \ - obj/board.o obj/hyperbola-quintessence.o + obj/board.o obj/hyperbola-quintessence.o +MOVEGENOBJS = obj/fen.o obj/position.o obj/piece.o obj/bitboard.o \ + obj/board.o obj/hyperbola-quintessence.o obj/move.o obj/movegen.o +MKTESTDATAOBJS = obj/fen.o obj/position.o obj/piece.o obj/bitboard.o \ + obj/board.o obj/hyperbola-quintessence.o obj/move.o testing: $(TEST) bin/fen-test: test/fen-test.c $(FENTESTOBJS) - $(CC) $(DEPFLAGS) $(CPPFLAGS) $(CFLAGS) $< $(FENTESTOBJS) $(LDFLAGS) $(LIBS) -o $@ + @echo compiling $@ executable. + @$(CC) $(DEPFLAGS) $(CPPFLAGS) $(CFLAGS) $< $(FENTESTOBJS) $(LDFLAGS) $(LIBS) -o $@ bin/bitboard-test: test/bitboard-test.c $(BITBOARDOBJS) - echo all=$^ - $(CC) $(DEPFLAGS) $(CPPFLAGS) $(CFLAGS) $< $(BITBOARDOBJS) $(LDFLAGS) $(LIBS) -o $@ + @echo compiling $@ executable. + @$(CC) $(DEPFLAGS) $(CPPFLAGS) $(CFLAGS) $< $(BITBOARDOBJS) $(LDFLAGS) $(LIBS) -o $@ +bin/movegen-test: test/movegen-test.c $(MOVEGENOBJS) + @echo compiling $@ executable. + @$(CC) $(DEPFLAGS) $(CPPFLAGS) $(CFLAGS) $< $(MOVEGENOBJS) $(LDFLAGS) $(LIBS) -o $@ +bin/mktestdata: test/mktestdata.c $(MKTESTDATAOBJS) + @echo compiling $@ executable. + @$(CC) $(DEPFLAGS) $(CPPFLAGS) $(CFLAGS) $< $(MKTESTDATAOBJS) $(LDFLAGS) $(LIBS) -o $@ ##################################### Makefile debug .PHONY: showflags wft diff --git a/src/attack.c b/src/attack.c new file mode 100644 index 0000000..d2cb225 --- /dev/null +++ b/src/attack.c @@ -0,0 +1,67 @@ +/* attack.c - attack functions. + * + * Copyright (C) 2024 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include +#include + +#include "chessdefs.h" +#include "bitboard.h" +#include "position.h" +#include "hyperbola-quintessence.h" + +/** + * square_attackers() - find attackers on a square + * @pos: position + * @sq: square to test + * @c: attacker color + * + * Find all @c attacks on @sq. En-passant is not considered. + * + * Algorithm: We perform a reverse attack, and check if any given + * piece type on @sq can attack a @c piece of same type. + * + * For example, if @c is white, we test for all T in P,N,B,R,Q,K if + * T on @sq could attack a white T piece. + * + * @Return: a bitboard of attackers. + * + */ +bitboard_t sq_attackers(pos_t *pos, square_t sq, color_t c) +{ + bitboard_t attackers = 0; + bitboard_t from = mask(sq); + bitboard_t c_pieces = pos->bb[c][ALL_PIECES]; + bitboard_t occ = c_pieces | pos->bb[OPPONENT(c)][ALL_PIECES]; + bitboard_t to; + + /* pawn */ + to = pos->bb[c][PAWN]; + attackers |= pawn_push_upleft(from, c) & to; + attackers |= pawn_push_upright(from, c) & to; + + /* knight & king */ + to = pos->bb[c][KNIGHT]; + attackers |= bb_knight_moves(c_pieces, from); + to = pos->bb[c][KING]; + attackers |= bb_king_moves(c_pieces, from); + + /* bishop / queen */ + to = pos->bb[c][BISHOP] | pos->bb[c][QUEEN]; + attackers |= hyperbola_bishop_moves(occ, from) & to; + + /* rook / queen */ + to = pos->bb[c][ROOK] | pos->bb[c][QUEEN]; + attackers |= hyperbola_rook_moves(occ, from) & to; + + return attackers; +} diff --git a/src/attack.h b/src/attack.h new file mode 100644 index 0000000..70ddbb1 --- /dev/null +++ b/src/attack.h @@ -0,0 +1,22 @@ +/* attack.h - attack functions. + * + * Copyright (C) 2024 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#ifndef _ATTACK_H +#define _ATTACK_H + +#include "chessdefs.h" +#include "bitboard.h" + +extern bitboard_t sq_attackers(pos_t *pos, square_t sq, color_t c); + +#endif