add sq_line array, bb_sq_aligned3, renamed sq_manh to sq_taxi
This commit is contained in:
8
Makefile
8
Makefile
@@ -205,7 +205,7 @@ cleanobjdir: cleanobj
|
|||||||
# "normal" ones, but do not imply to rebuild target.
|
# "normal" ones, but do not imply to rebuild target.
|
||||||
$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR) $(DEPDIR)
|
$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR) $(DEPDIR)
|
||||||
@echo compiling brchess module: $< "->" $@.
|
@echo compiling brchess module: $< "->" $@.
|
||||||
$(CC) -c $(ALL_CFLAGS) $< -o $@
|
@$(CC) -c $(ALL_CFLAGS) $< -o $@
|
||||||
|
|
||||||
##################################### brlib libraries
|
##################################### brlib libraries
|
||||||
.PHONY: cleanbrlib cleanallbrlib brlib
|
.PHONY: cleanbrlib cleanallbrlib brlib
|
||||||
@@ -231,7 +231,7 @@ cleanbindir:
|
|||||||
$(call rmdir,$(BINDIR),binaries)
|
$(call rmdir,$(BINDIR),binaries)
|
||||||
|
|
||||||
$(TARGET): libs $(OBJ) | $(BINDIR)
|
$(TARGET): libs $(OBJ) | $(BINDIR)
|
||||||
@echo generating $@ executable.
|
@echo generating $@.
|
||||||
$(CC) $(LDFLAGS) $(OBJ) $(LIBS) -o $@
|
$(CC) $(LDFLAGS) $(OBJ) $(LIBS) -o $@
|
||||||
|
|
||||||
##################################### pre-processed (.i) and assembler (.s) output
|
##################################### pre-processed (.i) and assembler (.s) output
|
||||||
@@ -241,11 +241,11 @@ cleanasmcpp:
|
|||||||
@$(call rmfiles,$(ASMFILES) $(CPPFILES),asm and pre-processed)
|
@$(call rmfiles,$(ASMFILES) $(CPPFILES),asm and pre-processed)
|
||||||
|
|
||||||
%.i: %.c
|
%.i: %.c
|
||||||
@echo generating $@
|
@echo generating $@ (cpp processed).
|
||||||
@$(CC) -E $(CPPFLAGS) $(CFLAGS) $< -o $@
|
@$(CC) -E $(CPPFLAGS) $(CFLAGS) $< -o $@
|
||||||
|
|
||||||
%.s: %.c
|
%.s: %.c
|
||||||
@echo generating $@
|
@echo generating $@ (asm).
|
||||||
@$(CC) -S -fverbose-asm $(CPPFLAGS) $(CFLAGS) $< -o $@
|
@$(CC) -S -fverbose-asm $(CPPFLAGS) $(CFLAGS) $< -o $@
|
||||||
|
|
||||||
##################################### LSP (ccls)
|
##################################### LSP (ccls)
|
||||||
|
@@ -25,6 +25,7 @@ bitboard_t bb_sq[64];
|
|||||||
bitboard_t bb_sqrank[64], bb_sqfile[64], bb_sqdiag[64], bb_sqanti[64];
|
bitboard_t bb_sqrank[64], bb_sqfile[64], bb_sqdiag[64], bb_sqanti[64];
|
||||||
bitboard_t bb_between_excl[64][64];
|
bitboard_t bb_between_excl[64][64];
|
||||||
bitboard_t bb_between[64][64];
|
bitboard_t bb_between[64][64];
|
||||||
|
bitboard_t bb_line[64][64];
|
||||||
|
|
||||||
bitboard_t bb_knight[64], bb_king[64];
|
bitboard_t bb_knight[64], bb_king[64];
|
||||||
|
|
||||||
@@ -139,6 +140,24 @@ void bitboard_init(void)
|
|||||||
bb_sqdiag[sq] = tmpbb[sq][2];
|
bb_sqdiag[sq] = tmpbb[sq][2];
|
||||||
bb_sqanti[sq] = tmpbb[sq][3];
|
bb_sqanti[sq] = tmpbb[sq][3];
|
||||||
}
|
}
|
||||||
|
for (square_t sq1 = 0; sq1 < 64; ++sq1) {
|
||||||
|
for (square_t sq2 = 0; sq2 < 64; ++sq2) {
|
||||||
|
if (sq1 != sq2) {
|
||||||
|
if (bb_sqfile[sq1] == bb_sqfile[sq2])
|
||||||
|
bb_line[sq1][sq2] = bb_sqfile[sq1];
|
||||||
|
else if (bb_sqrank[sq1] == bb_sqrank[sq2])
|
||||||
|
bb_line[sq1][sq2] = bb_sqrank[sq1];
|
||||||
|
else if (bb_sqdiag[sq1] == bb_sqdiag[sq2])
|
||||||
|
bb_line[sq1][sq2] = bb_sqdiag[sq1];
|
||||||
|
else if (bb_sqanti[sq1] == bb_sqanti[sq2])
|
||||||
|
bb_line[sq1][sq2] = bb_sqanti[sq1];
|
||||||
|
|
||||||
|
//if (bb_line[sq1][sq2]) {
|
||||||
|
// printf("bb_line[%d][%d] = %16lx\n", sq1, sq2, bb_line[sq1][sq2]);
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* 3) knight and king moves */
|
/* 3) knight and king moves */
|
||||||
for (square_t sq = A1; sq <= H8; ++sq) {
|
for (square_t sq = A1; sq <= H8; ++sq) {
|
||||||
|
@@ -21,8 +21,6 @@
|
|||||||
#include "board.h"
|
#include "board.h"
|
||||||
#include "piece.h"
|
#include "piece.h"
|
||||||
|
|
||||||
//typedef u64 bitboard_t;
|
|
||||||
|
|
||||||
/* mapping square -> bitboard */
|
/* mapping square -> bitboard */
|
||||||
extern bitboard_t bb_sq[64];
|
extern bitboard_t bb_sq[64];
|
||||||
/* squares between sq1 and sq2, exclusing both */
|
/* squares between sq1 and sq2, exclusing both */
|
||||||
@@ -30,12 +28,17 @@ extern bitboard_t bb_between_excl[64][64];
|
|||||||
/* squares between sq1 and sq2, including sq2 */
|
/* squares between sq1 and sq2, including sq2 */
|
||||||
extern bitboard_t bb_between[64][64];
|
extern bitboard_t bb_between[64][64];
|
||||||
|
|
||||||
/* bb_sqrank[64]: square to rank
|
/**
|
||||||
|
* bb_sqrank[64]: square to rank
|
||||||
* bb_sqfile[64]: square to file
|
* bb_sqfile[64]: square to file
|
||||||
* bb_sqdiag[64]: square to diagonal
|
* bb_sqdiag[64]: square to diagonal
|
||||||
* bb_sqanti[64]: square to antidiagonal
|
* bb_sqanti[64]: square to antidiagonal
|
||||||
*/
|
*/
|
||||||
extern bitboard_t bb_sqrank[64], bb_sqfile[64], bb_sqdiag[64], bb_sqanti[64];
|
extern bitboard_t bb_sqrank[64], bb_sqfile[64], bb_sqdiag[64], bb_sqanti[64];
|
||||||
|
|
||||||
|
/* line (rank, file, diagonal or anti-diagonal) between two squares */
|
||||||
|
extern bitboard_t bb_line[64][64];
|
||||||
|
|
||||||
/* knight and king moves */
|
/* knight and king moves */
|
||||||
extern bitboard_t bb_knight[64], bb_king[64];
|
extern bitboard_t bb_knight[64], bb_king[64];
|
||||||
|
|
||||||
@@ -192,17 +195,24 @@ static __always_inline bitboard_t bb_file(int file)
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* bb_sq_aligned() - check if two squares are aligned (same file or rank).
|
* bb_sq_aligned() - check if two squares are aligned (same file or rank).
|
||||||
* @sq1: square 1
|
* @sq1, @sq2: the two squares.
|
||||||
* @sq2: square 2
|
|
||||||
*
|
*
|
||||||
* @sq2 is included in return value, to be non zero if the two squares
|
* @return: true if @sq1 and @sq2 are on same line, false otherwise.
|
||||||
* are neighbors.
|
|
||||||
*
|
|
||||||
* @return: bitboard of squares between @sq1 and @sq2, including @sq2.
|
|
||||||
*/
|
*/
|
||||||
static __always_inline bitboard_t bb_sq_aligned(square_t sq1, square_t sq2)
|
static __always_inline bool bb_sq_aligned(square_t sq1, square_t sq2)
|
||||||
{
|
{
|
||||||
return bb_between[sq1][sq2];
|
return bb_line[sq1][sq2];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* bb_sq_aligned3() - check if 3 squares are aligned (same file or rank).
|
||||||
|
* @sq1, @sq2, @sq3: the three squares.
|
||||||
|
*
|
||||||
|
* @return: true if @sq1, @sq2, and @sq3 are aligned, false otherwise.
|
||||||
|
*/
|
||||||
|
static __always_inline bool bb_sq_aligned3(square_t sq1, square_t sq2, square_t sq3)
|
||||||
|
{
|
||||||
|
return bb_line[sq1][sq2] & mask(sq3);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
20
src/board.h
20
src/board.h
@@ -59,14 +59,28 @@ static __always_inline rank_t sq_rank(square_t square)
|
|||||||
#define sq_ok(sq) ((sq) >= A1 && (sq) <= H8)
|
#define sq_ok(sq) ((sq) >= A1 && (sq) <= H8)
|
||||||
#define sq_coord_ok(c) ((c) >= 0 && (c) < 8)
|
#define sq_coord_ok(c) ((c) >= 0 && (c) < 8)
|
||||||
|
|
||||||
/* Chebyshev distance: max( |r2 - r1|, |f2 - f1| )
|
/**
|
||||||
|
* sq_dist() - Chebyshev (king) distance between two squares (macro).
|
||||||
|
* @sq1, @sq2: The two squares.
|
||||||
|
*
|
||||||
* See: https://www.chessprogramming.org/Distance
|
* See: https://www.chessprogramming.org/Distance
|
||||||
|
* Distance is max( |r2 - r1|, |f2 - f1| )
|
||||||
|
*
|
||||||
|
* @Return: the Chebyshev distance.
|
||||||
*/
|
*/
|
||||||
#define sq_dist(sq1, sq2) (max(abs(sq_file(sq2) - sq_file(sq1)), \
|
#define sq_dist(sq1, sq2) (max(abs(sq_file(sq2) - sq_file(sq1)), \
|
||||||
abs(sq_rank(sq2) - sq_rank(sq1))))
|
abs(sq_rank(sq2) - sq_rank(sq1))))
|
||||||
/* Manhattan distance: |r2 - r1| + |f2 - f1|
|
|
||||||
|
/**
|
||||||
|
* sq_taxi() - Manhattan (taxi) distance between two squares (macro).
|
||||||
|
* @sq1, @sq2: The two squares.
|
||||||
|
*
|
||||||
|
* See: https://www.chessprogramming.org/Distance
|
||||||
|
* Distance is |r2 - r1| + |f2 - f1|.
|
||||||
|
*
|
||||||
|
* @Return: the Manhattan distance.
|
||||||
*/
|
*/
|
||||||
#define sq_manh(sq1, sq2) (abs(sq_file(sq2) - sq_file(sq1)) + \
|
#define sq_taxi(sq1, sq2) (abs(sq_file(sq2) - sq_file(sq1)) + \
|
||||||
abs(sq_rank(sq2) - sq_rank(sq1)))
|
abs(sq_rank(sq2) - sq_rank(sq1)))
|
||||||
|
|
||||||
extern const char *sq_to_string(const square_t sq);
|
extern const char *sq_to_string(const square_t sq);
|
||||||
|
50
test/attack-test.c
Normal file
50
test/attack-test.c
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
/* attack-test.c - basic square attack tests.
|
||||||
|
*
|
||||||
|
* 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 <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "chessdefs.h"
|
||||||
|
#include "fen.h"
|
||||||
|
#include "position.h"
|
||||||
|
#include "move-gen.h"
|
||||||
|
#include "attack.h"
|
||||||
|
|
||||||
|
#include "common-test.h"
|
||||||
|
|
||||||
|
int main(int __unused ac, __unused char**av)
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
char *fen;
|
||||||
|
pos_t *pos;//, *fishpos = pos_new();
|
||||||
|
|
||||||
|
setlinebuf(stdout); /* line-buffered stdout */
|
||||||
|
|
||||||
|
bitboard_init();
|
||||||
|
hyperbola_init();
|
||||||
|
|
||||||
|
while ((fen = next_fen(ATTACK))) {
|
||||||
|
//printf(">>>>> %s\n", test[i]);
|
||||||
|
printf("original fen %d: [%p][%s]\n", i, fen, fen);
|
||||||
|
if (!(pos = fen2pos(NULL, fen))) {
|
||||||
|
printf("wrong fen %d: [%s]\n", i, fen);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
pos_print(pos);
|
||||||
|
|
||||||
|
pos_del(pos);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
Reference in New Issue
Block a user