add square attackers fct

This commit is contained in:
2024-02-29 09:18:19 +01:00
parent f1a081a7b6
commit a8e3ec70f8
3 changed files with 109 additions and 7 deletions

View File

@@ -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

67
src/attack.c Normal file
View File

@@ -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 <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#include <stdio.h>
#include <stdarg.h>
#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;
}

22
src/attack.h Normal file
View File

@@ -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 <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#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