Compare commits
6 Commits
f1a081a7b6
...
beaa7bfcbc
Author | SHA1 | Date | |
---|---|---|---|
beaa7bfcbc | |||
be32bae86a | |||
8f0840fc2f | |||
6d412e7fce | |||
7859228f89 | |||
a8e3ec70f8 |
27
Makefile
27
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
|
||||
|
||||
|
67
src/attack.c
Normal file
67
src/attack.c
Normal 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
22
src/attack.h
Normal 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
|
@@ -20,7 +20,6 @@
|
||||
#include "piece.h"
|
||||
#include "board.h"
|
||||
#include "bitboard.h"
|
||||
#include "position.h"
|
||||
|
||||
/* vectors are clockwise from N */
|
||||
static int knight_vector[] = {
|
||||
@@ -141,26 +140,6 @@ void bitboard_init(void)
|
||||
bb_anti[sq] = tmpbb[sq][3];
|
||||
}
|
||||
|
||||
/*
|
||||
* for (int i = 0; i < 64; ++i) {
|
||||
* for (int j = 0; j < 64; ++j) {
|
||||
* if (bb_between[i][j] != bb_between_excl[i][j]) {
|
||||
* bitboard_t diff = bb_between_excl[i][j] ^ bb_between[i][j];
|
||||
* int k = popcount64(bb_between[i][j]) -
|
||||
* popcount64(bb_between_excl[i][j]);
|
||||
* printf("%s-%s diff=%d excl=%s ",
|
||||
* sq_string(i), sq_string(j),
|
||||
* k,
|
||||
* sq_string(ctz64(diff)));
|
||||
* if (k == 1 && ctz64(diff) == j)
|
||||
* printf("OK\n");
|
||||
* else
|
||||
* printf("NOK\n");
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
|
||||
/* 3) knight and king moves */
|
||||
for (square_t sq = A1; sq <= H8; ++sq) {
|
||||
//rank_t r1 = sq_rank(sq);
|
||||
@@ -182,10 +161,28 @@ void bitboard_init(void)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* bb_knight_moves() - get bitboard of knight pseudo-moves
|
||||
* @notmine: bitboard_t of squares not occupied by own pieces
|
||||
* @sq: knight square
|
||||
*
|
||||
* @Return: bitboard of available moves.
|
||||
*/
|
||||
bitboard_t bb_knight_moves(bitboard_t notmine, square_t sq)
|
||||
{
|
||||
//bitboard_print("bb_knight_move mask", bb_knight[sq]);
|
||||
//bitboard_print("bb_knight_move res", bb_knight[sq] & notmine);
|
||||
return bb_knight[sq] & notmine;
|
||||
}
|
||||
|
||||
/**
|
||||
* bb_king_moves() - get bitboard of king pseudo-moves
|
||||
* @notmine: bitboard_t of squares not occupied by own pieces
|
||||
* @sq: king square
|
||||
*
|
||||
* @Return: bitboard of available moves.
|
||||
*/
|
||||
bitboard_t bb_king_moves(bitboard_t notmine, square_t sq)
|
||||
{
|
||||
return bb_king[sq] & notmine;
|
||||
@@ -258,12 +255,16 @@ void bitboard_print_multi(const char *title, int n, ...)
|
||||
* bitboard_rank_sprint() - print an u8 rank binary representation
|
||||
* @str: the destination string
|
||||
* @bb8: the uchar to print
|
||||
*
|
||||
* @return: @str, filled with ascii representation
|
||||
*/
|
||||
char *bitboard_rank_sprint(char *str, const uchar bb8)
|
||||
{
|
||||
for (file_t f = FILE_A; f <= FILE_H; ++f) {
|
||||
*(str+f) = bb8 & mask(f) ? '1': '.';
|
||||
file_t f;
|
||||
for (f = FILE_A; f <= FILE_H; ++f) {
|
||||
*(str + f) = bb8 & mask(f) ? '1': '.';
|
||||
}
|
||||
*(str + f) = 0;
|
||||
//printf(" 0 1 2 3 4 5 6 7\n");
|
||||
//printf("\n");
|
||||
return str;
|
||||
|
@@ -40,25 +40,34 @@ extern bitboard_t bb_rank[64], bb_file[64], bb_diag[64], bb_anti[64];
|
||||
extern bitboard_t bb_knight[64], bb_king[64];
|
||||
|
||||
enum {
|
||||
FILE_Abb = 0x0101010101010101ULL,
|
||||
FILE_Bbb = 0x0202020202020202ULL,
|
||||
FILE_Cbb = 0x0404040404040404ULL,
|
||||
FILE_Dbb = 0x0808080808080808ULL,
|
||||
FILE_Ebb = 0x1010101010101010ULL,
|
||||
FILE_Fbb = 0x2020202020202020ULL,
|
||||
FILE_Gbb = 0x4040404040404040ULL,
|
||||
FILE_Hbb = 0x8080808080808080ULL,
|
||||
A1bb = mask(A1), A2bb = mask(A2), A3bb = mask(A3), A4bb = mask(A4),
|
||||
A5bb = mask(A5), A6bb = mask(A6), A7bb = mask(A7), A8bb = mask(A8),
|
||||
B1bb = mask(B1), B2bb = mask(B2), B3bb = mask(B3), B4bb = mask(B4),
|
||||
B5bb = mask(B5), B6bb = mask(B6), B7bb = mask(B7), B8bb = mask(B8),
|
||||
C1bb = mask(C1), C2bb = mask(C2), C3bb = mask(C3), C4bb = mask(C4),
|
||||
C5bb = mask(C5), C6bb = mask(C6), C7bb = mask(C7), C8bb = mask(C8),
|
||||
D1bb = mask(D1), D2bb = mask(D2), D3bb = mask(D3), D4bb = mask(D4),
|
||||
D5bb = mask(D5), D6bb = mask(D6), D7bb = mask(D7), D8bb = mask(D8),
|
||||
E1bb = mask(E1), E2bb = mask(E2), E3bb = mask(E3), E4bb = mask(E4),
|
||||
E5bb = mask(E5), E6bb = mask(E6), E7bb = mask(E7), E8bb = mask(E8),
|
||||
F1bb = mask(F1), F2bb = mask(F2), F3bb = mask(F3), F4bb = mask(F4),
|
||||
F5bb = mask(F5), F6bb = mask(F6), F7bb = mask(F7), F8bb = mask(F8),
|
||||
G1bb = mask(G1), G2bb = mask(G2), G3bb = mask(G3), G4bb = mask(G4),
|
||||
G5bb = mask(G5), G6bb = mask(G6), G7bb = mask(G7), G8bb = mask(G8),
|
||||
H1bb = mask(H1), H2bb = mask(H2), H3bb = mask(H3), H4bb = mask(H4),
|
||||
H5bb = mask(H5), H6bb = mask(H6), H7bb = mask(H7), H8bb = mask(H8),
|
||||
};
|
||||
|
||||
enum {
|
||||
RANK_1bb = 0x00000000000000ffULL,
|
||||
RANK_2bb = 0x000000000000ff00ULL,
|
||||
RANK_3bb = 0x0000000000ff0000ULL,
|
||||
RANK_4bb = 0x00000000ff000000ULL,
|
||||
RANK_5bb = 0x000000ff00000000ULL,
|
||||
RANK_6bb = 0x0000ff0000000000ULL,
|
||||
RANK_7bb = 0x00ff000000000000ULL,
|
||||
RANK_8bb = 0xff00000000000000ULL
|
||||
FILE_Abb = 0x0101010101010101ULL, FILE_Bbb = 0x0202020202020202ULL,
|
||||
FILE_Cbb = 0x0404040404040404ULL, FILE_Dbb = 0x0808080808080808ULL,
|
||||
FILE_Ebb = 0x1010101010101010ULL, FILE_Fbb = 0x2020202020202020ULL,
|
||||
FILE_Gbb = 0x4040404040404040ULL, FILE_Hbb = 0x8080808080808080ULL,
|
||||
|
||||
RANK_1bb = 0x00000000000000ffULL, RANK_2bb = 0x000000000000ff00ULL,
|
||||
RANK_3bb = 0x0000000000ff0000ULL, RANK_4bb = 0x00000000ff000000ULL,
|
||||
RANK_5bb = 0x000000ff00000000ULL, RANK_6bb = 0x0000ff0000000000ULL,
|
||||
RANK_7bb = 0x00ff000000000000ULL, RANK_8bb = 0xff00000000000000ULL
|
||||
};
|
||||
|
||||
/* https://www.chessprogramming.org/Flipping_Mirroring_and_Rotating#Rotation
|
||||
@@ -102,10 +111,15 @@ static __always_inline bitboard_t shift_nw(const bitboard_t bb)
|
||||
return (bb & ~FILE_Abb) << NORTH_WEST;
|
||||
}
|
||||
|
||||
/* pawn moves/attacks */
|
||||
#define pawn_push(bb, c) ((c) == WHITE ? shift_n(bb): shift_s(bb))
|
||||
#define pawn_take_left(bb, c) ((c) == WHITE ? shift_nw(bb): shift_se(bb))
|
||||
#define pawn_take_right(bb, c) ((c) == WHITE ? shift_ne(bb): shift_sw(bb))
|
||||
#define pawn_up_value(c) ((c) == WHITE ? 8: -8)
|
||||
/* pawn moves/attacks (for bitboards) */
|
||||
#define pawn_shift_up(bb, c) ((c) == WHITE ? shift_n(bb): shift_s(bb))
|
||||
#define pawn_shift_upleft(bb, c) ((c) == WHITE ? shift_nw(bb): shift_se(bb))
|
||||
#define pawn_shift_upright(bb, c) ((c) == WHITE ? shift_ne(bb): shift_sw(bb))
|
||||
/* pawn move (for single pawn) */
|
||||
#define pawn_push_up(sq, c) ((sq) + ((c) == WHITE ? NORTH: SOUTH))
|
||||
#define pawn_push_upleft(sq, c) ((sq) + ((c) == WHITE ? NORTH_WEST: SOUTH_EAST))
|
||||
#define pawn_push_upright(sq, c) ((sq) + ((c) == WHITE ? NORTH_EAST: SOUTH_WEST))
|
||||
|
||||
extern bitboard_t bitboard_between_excl(square_t sq1, square_t sq2);
|
||||
extern void bitboard_init(void);
|
||||
|
19
src/board.c
19
src/board.c
@@ -11,6 +11,8 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
|
||||
#include "brlib.h"
|
||||
#include "board.h"
|
||||
|
||||
@@ -26,12 +28,25 @@ static const char *sq_strings[] = {
|
||||
};
|
||||
|
||||
/**
|
||||
* sq_string() - return a square string
|
||||
* sq_to_string() - return a square string
|
||||
* @square: square (0-64)
|
||||
*
|
||||
* @Return: Pointer to @square string representation ("a1"-"h8").
|
||||
*/
|
||||
const char *sq_string(const square_t square)
|
||||
const char *sq_to_string(const square_t square)
|
||||
{
|
||||
return sq_strings[square];
|
||||
}
|
||||
|
||||
/**
|
||||
* sq_from_string() - return a square from a string
|
||||
* @sqstr: the square representation (a1-h8)
|
||||
*
|
||||
* @Return: square_t representation of str.
|
||||
*/
|
||||
square_t sq_from_string(const char *sqstr)
|
||||
{
|
||||
file_t f = C2FILE(sqstr[0]);
|
||||
rank_t r = C2RANK(sqstr[1]);
|
||||
return sq_coord_ok(f) && sq_coord_ok(r) ? sq_make(f, r): SQUARE_NONE;
|
||||
}
|
||||
|
@@ -97,6 +97,7 @@ static __always_inline rank_t sq_rank(square_t square)
|
||||
#define sq_manh(sq1, sq2) (abs(sq_file(sq2) - sq_file(sq1)) + \
|
||||
abs(sq_rank(sq2) - sq_rank(sq1)))
|
||||
|
||||
extern const char *sq_string(const square_t sq);
|
||||
extern const char *sq_to_string(const square_t sq);
|
||||
extern square_t sq_from_string(const char *sq_string);
|
||||
|
||||
#endif /* BOARD_H */
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/* chessdefs.h - generic chess definitions.
|
||||
/* chessdefs.h - generic/catchall chess definitions.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
@@ -48,7 +48,9 @@ typedef enum {
|
||||
#define ENDGAME 2
|
||||
|
||||
/* forward defs */
|
||||
typedef struct _pos_s pos_t;
|
||||
typedef struct __pos_s pos_t;
|
||||
typedef struct __movelist_s movelist_t;
|
||||
|
||||
/* bitboard
|
||||
*/
|
||||
//typedef u64 bitboard_t;
|
||||
|
23
src/fen.c
23
src/fen.c
@@ -229,30 +229,31 @@ pos_t *fen2pos(pos_t *pos, const char *fen)
|
||||
if (*cur == '-') {
|
||||
cur++;
|
||||
} else {
|
||||
tmppos.en_passant = sq_make(C2FILE(*cur), C2RANK(*(cur+1)));
|
||||
tmppos.en_passant = sq_from_string(cur);
|
||||
cur += 2;
|
||||
}
|
||||
SKIP_BLANK(cur);
|
||||
|
||||
/* 5) half moves since last capture or pawn move (50 moves rule)
|
||||
*/
|
||||
sscanf(cur, "%hd%n", &tmppos.clock_50, &consumed);
|
||||
tmppos.clock_50 = 0;
|
||||
tmppos.plycount = 1;
|
||||
if (sscanf(cur, "%hd%n", &tmp, &consumed) != 1)
|
||||
goto end; /* early end, ignore w/o err */
|
||||
|
||||
tmppos.clock_50 = tmp;
|
||||
cur += consumed;
|
||||
SKIP_BLANK(cur);
|
||||
|
||||
/* 6) current full move number, starting with 1
|
||||
*/
|
||||
sscanf(cur, "%hd", &tmp);
|
||||
if (sscanf(cur, "%hd", &tmp) != 1)
|
||||
goto end;
|
||||
if (tmp <= 0) /* fix faulty numbers*/
|
||||
tmp = 1;
|
||||
tmp = 2 * (tmp - 1) + (tmppos.turn == BLACK); /* plies, +1 if black turn */
|
||||
|
||||
tmppos.plycount = tmp;
|
||||
|
||||
# ifdef DEBUG_FEN
|
||||
raw_board_print(&tmppos);
|
||||
# endif
|
||||
|
||||
end:
|
||||
if (warn(err_line, "FEN error line %d: charpos=%d char=%#x(%c)\n",
|
||||
err_line, err_pos, err_char, err_char)) {
|
||||
@@ -263,6 +264,10 @@ end:
|
||||
pos = pos_new();
|
||||
*pos = tmppos;
|
||||
}
|
||||
# ifdef DEBUG_FEN
|
||||
pos_print_board_raw(&tmppos, 1);
|
||||
# endif
|
||||
|
||||
return pos;
|
||||
}
|
||||
|
||||
@@ -300,7 +305,7 @@ char *pos2fen(const pos_t *pos, char *fen)
|
||||
len++;
|
||||
fen[cur++] = '0' + len;
|
||||
} else {
|
||||
fen[cur++] = piece_to_char_color(piece);
|
||||
fen[cur++] = *piece_to_char_color(piece);
|
||||
f++;
|
||||
}
|
||||
}
|
||||
|
@@ -48,7 +48,6 @@ uchar bb_rank_attacks[64 * 8];
|
||||
* to save one operation in hyperbola_moves().
|
||||
* TODO ? replace rank attack with this idea, mapping rank to diagonal ?
|
||||
* See http://timcooijmans.blogspot.com/2014/04/
|
||||
*
|
||||
*/
|
||||
void hyperbola_init()
|
||||
{
|
||||
@@ -56,7 +55,8 @@ void hyperbola_init()
|
||||
*/
|
||||
for (int occ = 0; occ < 64; ++occ) {
|
||||
for (int file = 0; file < 8; ++file) {
|
||||
uchar attacks = 0;
|
||||
int attacks = 0;
|
||||
//int o = mask << 1; /* skip right square */
|
||||
|
||||
/* set f left attacks */
|
||||
for (int slide = file - 1; slide >= 0; --slide) {
|
||||
@@ -69,10 +69,18 @@ void hyperbola_init()
|
||||
for (int slide = file + 1; slide < 8; ++slide) {
|
||||
int b = bb_sq[slide];
|
||||
attacks |= b;
|
||||
if ((occ << 1) & b)
|
||||
if ((occ << 1) & b) /* piece on b, we stop */
|
||||
//if ((o & b) == b)
|
||||
break;
|
||||
}
|
||||
bb_rank_attacks[occ * 8 + file] = attacks;
|
||||
bb_rank_attacks[(occ << 3) + file] = attacks;
|
||||
|
||||
//if (((occ << 3) + file) == 171) {
|
||||
//char str[64], str2[64];
|
||||
//printf("mask=%x=%s file=%d att=%x=%s\n",
|
||||
// occ, bitboard_rank_sprint(str, occ), file,
|
||||
// attacks, bitboard_rank_sprint(str2, attacks));
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -88,11 +96,16 @@ void hyperbola_init()
|
||||
*/
|
||||
static bitboard_t hyperbola_rank_moves(bitboard_t occ, square_t sq)
|
||||
{
|
||||
uint32_t rank = sq & SQ_FILEMASK;
|
||||
uint32_t file = sq & SQ_RANKMASK;
|
||||
uint64_t o = (occ >> rank) & 0x7e; /* 01111110 clear bits 0 & 7 */
|
||||
|
||||
return ((bitboard_t)bb_rank_attacks[o * 4 + file]) << rank;
|
||||
u32 rank = sq & SQ_RANKMASK;
|
||||
u32 file = sq & SQ_FILEMASK;
|
||||
u64 o = (occ >> rank) & 0176; /* 01111110 clear bits 0 & 7 */
|
||||
//char zob[128], zob2[128];
|
||||
//printf("rank_moves: occ=%lx=%s file=%d o=%lx=%s index=%ld=%ld attack=%lx=%s\n", occ,
|
||||
// bitboard_rank_sprint(zob, occ), file, o,
|
||||
// bitboard_rank_sprint(zob, o), (o << 2) + file, (o * 4) + file,
|
||||
// (bitboard_t)bb_rank_attacks[(o << 2) + file] << rank,
|
||||
// bitboard_rank_sprint(zob2, (bitboard_t)bb_rank_attacks[(o << 2) + file] << rank));
|
||||
return ((bitboard_t)bb_rank_attacks[(o << 2) + file]) << rank;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,30 +132,30 @@ static bitboard_t hyperbola_moves(const bitboard_t pieces, const square_t sq,
|
||||
|
||||
static bitboard_t hyperbola_file_moves(bitboard_t occ, square_t sq)
|
||||
{
|
||||
return hyperbola_moves(occ, bb_file[sq], sq);
|
||||
return hyperbola_moves(occ, sq, bb_file[sq]);
|
||||
}
|
||||
|
||||
static bitboard_t hyperbola_diag_moves(bitboard_t occ, square_t sq)
|
||||
{
|
||||
return hyperbola_moves(occ, bb_diag[sq], sq);
|
||||
return hyperbola_moves(occ, sq, bb_diag[sq]);
|
||||
}
|
||||
|
||||
static bitboard_t hyperbola_anti_moves(bitboard_t occ, square_t sq)
|
||||
{
|
||||
return hyperbola_moves(occ, bb_anti[sq], sq);
|
||||
return hyperbola_moves(occ, sq, bb_anti[sq]);
|
||||
}
|
||||
|
||||
bitboard_t hyperbola_bishop_moves(bitboard_t occ, square_t sq)
|
||||
{
|
||||
return hyperbola_diag_moves(occ, sq) + hyperbola_anti_moves(occ, sq);
|
||||
return hyperbola_diag_moves(occ, sq) | hyperbola_anti_moves(occ, sq);
|
||||
}
|
||||
|
||||
bitboard_t hyperbola_rook_moves(bitboard_t occ, square_t sq)
|
||||
{
|
||||
return hyperbola_file_moves(occ, sq) + hyperbola_rank_moves(occ, sq);
|
||||
return hyperbola_file_moves(occ, sq) | hyperbola_rank_moves(occ, sq);
|
||||
}
|
||||
|
||||
bitboard_t hyperbola_queen_moves(bitboard_t occ, square_t sq)
|
||||
{
|
||||
return hyperbola_bishop_moves(occ, sq) + hyperbola_rook_moves(occ, sq);
|
||||
return hyperbola_bishop_moves(occ, sq) | hyperbola_rook_moves(occ, sq);
|
||||
}
|
||||
|
1644
src/move.c
1644
src/move.c
File diff suppressed because it is too large
Load Diff
26
src/move.h
26
src/move.h
@@ -15,9 +15,8 @@
|
||||
#define MOVE_H
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "position.h"
|
||||
//#include "pool.h"
|
||||
#include "piece.h"
|
||||
#include "board.h"
|
||||
|
||||
/* move structure:
|
||||
* 3 2 2 1 1 1 1 1 1
|
||||
@@ -52,7 +51,7 @@
|
||||
|
||||
static inline move_t move_make(square_t from, square_t to)
|
||||
{
|
||||
return (to << 3) | from;
|
||||
return (to << 6) | from;
|
||||
}
|
||||
|
||||
static inline move_t move_make_promote(square_t from, square_t to, piece_type_t piece)
|
||||
@@ -60,9 +59,13 @@ static inline move_t move_make_promote(square_t from, square_t to, piece_type_t
|
||||
return move_make(from, to) | M_ENPASSANT | (piece << 15);
|
||||
}
|
||||
|
||||
static inline move_t move_from(move_t move)
|
||||
static inline square_t move_from(move_t move)
|
||||
{
|
||||
return move & 56;
|
||||
return move & 077;
|
||||
}
|
||||
static inline square_t move_to(move_t move)
|
||||
{
|
||||
return (move >> 6) & 077;
|
||||
}
|
||||
|
||||
/* moves_print flags
|
||||
@@ -75,7 +78,7 @@ static inline move_t move_from(move_t move)
|
||||
#define M_PR_SEPARATE 0x40 /* separate captures */
|
||||
#define M_PR_LONG 0x80
|
||||
|
||||
typedef struct {
|
||||
typedef struct __movelist_s {
|
||||
move_t move[MOVES_MAX];
|
||||
int nmoves; /* total moves (fill) */
|
||||
int curmove; /* current move (use) */
|
||||
@@ -83,19 +86,20 @@ typedef struct {
|
||||
|
||||
//pool_t *moves_pool_init();
|
||||
//void moves_pool_stats();
|
||||
|
||||
//int move_print(int movenum, move_t *move, move_flags_t flags);
|
||||
//void moves_print(pos_t *move, move_flags_t flags);
|
||||
|
||||
//void move_del(struct list_head *ptr);
|
||||
//int moves_del(pos_t *pos);
|
||||
extern void moves_print(pos_t *pos, int flags);
|
||||
extern void move_sort_by_sq(pos_t *pos);
|
||||
|
||||
int pseudo_moves_castle(pos_t *pos, bool color, bool doit, bool do_king);
|
||||
//extern int pseudo_moves_castle(pos_t *pos, bool color, bool doit, bool do_king);
|
||||
//int pseudo_moves_gen(pos_t *pos, piece_list_t *piece, bool doit, bool do_king);
|
||||
//int pseudo_moves_pawn(pos_t *pos, piece_list_t *piece, bool doit);
|
||||
//extern int moves_gen(pos_t *pos, bool color, bool doit, bool do_king);
|
||||
//extern int moves_gen_king_moves(pos_t *pos, bool color, bool doit);
|
||||
|
||||
//extern void moves_sort(pos_t *pos);
|
||||
// extern void moves_sort(pos_t *pos);
|
||||
|
||||
//extern void moves_gen_eval_sort(pos_t *pos);
|
||||
|
||||
//extern void moves_gen_all(pos_t *pos);
|
||||
|
159
src/movegen.c
159
src/movegen.c
@@ -11,12 +11,18 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "bitops.h"
|
||||
|
||||
#include "board.h"
|
||||
#include "bitboard.h"
|
||||
#include "hyperbola-quintessence.h"
|
||||
#include "piece.h"
|
||||
#include "position.h"
|
||||
#include "move.h"
|
||||
#include "hyperbola-quintessence.h"
|
||||
#include "movegen.h"
|
||||
|
||||
|
||||
/**
|
||||
* gen_pawn_push() - generate pawn push
|
||||
@@ -40,115 +46,164 @@
|
||||
int gen_all_pseudomoves(pos_t *pos)
|
||||
{
|
||||
color_t me = pos->turn, enemy = OPPONENT(me);
|
||||
bitboard_t my_pieces = pos->bb[me][ALL_PIECES], not_my_pieces = ~my_pieces,
|
||||
enemy_pieces = pos->bb[enemy][ALL_PIECES];
|
||||
bitboard_t occ = my_pieces | enemy_pieces, empty = ~occ;
|
||||
bitboard_t movebits, from_pawns;
|
||||
|
||||
int tmp1, tmp2, from, to;
|
||||
movelist_t moves = pos->moves;
|
||||
bitboard_t my_pieces = pos->bb[me][ALL_PIECES];
|
||||
bitboard_t not_my_pieces = ~my_pieces;
|
||||
bitboard_t enemy_pieces = pos->bb[enemy][ALL_PIECES];
|
||||
|
||||
bitboard_t occ = my_pieces | enemy_pieces;
|
||||
bitboard_t empty = ~occ;
|
||||
|
||||
bitboard_t movebits, from_pawns;
|
||||
bitboard_t tmp1, tmp2;
|
||||
|
||||
move_t *moves = pos->moves.move;
|
||||
int nmoves = pos->moves.nmoves;
|
||||
|
||||
int from, to;
|
||||
|
||||
/* sliding pieces */
|
||||
bit_for_each64(from, tmp1, pos->bb[me][BISHOP]) {
|
||||
movebits = hyperbola_bishop_moves(occ, from) & not_my_pieces;
|
||||
bit_for_each64(to, tmp2, movebits) {
|
||||
moves.move[moves.nmoves++] = move_make(from, to);
|
||||
moves[nmoves++] = move_make(from, to);
|
||||
}
|
||||
|
||||
}
|
||||
bit_for_each64(from, tmp1, pos->bb[me][ROOK]) {
|
||||
// printf("rook=%d/%s\n", from, sq_to_string(from));
|
||||
movebits = hyperbola_rook_moves(occ, from) & not_my_pieces;
|
||||
bit_for_each64(to, tmp2, movebits) {
|
||||
moves.move[moves.nmoves++] = move_make(from, to);
|
||||
moves[nmoves++] = move_make(from, to);
|
||||
}
|
||||
}
|
||||
/* TODO: remove this one */
|
||||
bit_for_each64(from, tmp1, pos->bb[me][QUEEN]) {
|
||||
movebits = hyperbola_queen_moves(occ, from) & not_my_pieces;
|
||||
//bitboard_print("bishop", movebits);
|
||||
//movebits = hyperbola_rook_moves(occ, from);
|
||||
//bitboard_print("rook", movebits);
|
||||
//bitboard_print("diag", bb_diag[] movebits);
|
||||
// & not_my_pieces;
|
||||
bit_for_each64(to, tmp2, movebits) {
|
||||
moves.move[moves.nmoves++] = move_make(from, to);
|
||||
moves[nmoves++] = move_make(from, to);
|
||||
}
|
||||
}
|
||||
|
||||
/* knight */
|
||||
//bitboard_print("not_my_pieces", not_my_pieces);
|
||||
bit_for_each64(from, tmp1, pos->bb[me][KNIGHT]) {
|
||||
movebits = bb_knight_moves(occ, from) & not_my_pieces;
|
||||
//printf("Nfrom=%d=%s\n", from, sq_to_string(from));
|
||||
movebits = bb_knight_moves(not_my_pieces, from);
|
||||
//printf("%lx\n", movebits);
|
||||
//bitboard_print("knight_moves", movebits);
|
||||
|
||||
bit_for_each64(to, tmp2, movebits) {
|
||||
moves.move[moves.nmoves++] = move_make(from, to);
|
||||
//printf("Nto=%d=%s\n", to, sq_to_string(to));
|
||||
moves[nmoves++] = move_make(from, to);
|
||||
}
|
||||
}
|
||||
|
||||
/* king */
|
||||
movebits = bb_king_moves(occ, pos->king[me]) & not_my_pieces;
|
||||
from = pos->king[me];
|
||||
movebits = bb_king_moves(not_my_pieces, from);
|
||||
bit_for_each64(to, tmp2, movebits) {
|
||||
moves.move[moves.nmoves++] = move_make(from, to);
|
||||
moves[nmoves++] = move_make(from, to);
|
||||
}
|
||||
|
||||
/* pawn: relative rank and files */
|
||||
bitboard_t rel_rank7 = (me == WHITE ? RANK_7bb : RANK_2bb);
|
||||
bitboard_t rel_rank3 = (me == WHITE ? RANK_3bb : RANK_6bb);
|
||||
bitboard_t rel_filea = (me == WHITE ? FILE_Abb : FILE_Hbb);
|
||||
bitboard_t rel_fileh = (me == WHITE ? FILE_Hbb : FILE_Abb);
|
||||
//bitboard_t rel_filea = (me == WHITE ? FILE_Abb : FILE_Hbb);
|
||||
//bitboard_t rel_fileh = (me == WHITE ? FILE_Hbb : FILE_Abb);
|
||||
int en_passant = pos->en_passant == SQUARE_NONE? 0: pos->en_passant;
|
||||
bitboard_t enemy_avail = bb_sq[en_passant] | enemy_pieces;
|
||||
|
||||
/* pawn: ranks 2-6 push 1 and 2 squares */
|
||||
movebits = pawn_push(pos->bb[me][PAWN] & ~rel_rank7, me) & empty;
|
||||
movebits = pawn_shift_up(pos->bb[me][PAWN] & ~rel_rank7, me) & empty;
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
from = pawn_push(to, enemy); /* reverse push */
|
||||
moves.move[moves.nmoves++] = move_make(from, to);
|
||||
from = pawn_push_up(to, enemy); /* reverse push */
|
||||
//printf("push %d->%d %s->%s", from, to, sq_to_string(from), sq_to_string(to));
|
||||
moves[nmoves++] = move_make(from, to);
|
||||
}
|
||||
movebits = pawn_push(movebits & rel_rank3, me) & empty;
|
||||
movebits = pawn_shift_up(movebits & rel_rank3, me) & empty;
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
from = pawn_push(pawn_push(to, enemy), enemy);
|
||||
moves.move[moves.nmoves++] = move_make(from, to);
|
||||
from = pawn_push_up(pawn_push_up(to, enemy), enemy);
|
||||
moves[nmoves++] = move_make(from, to);
|
||||
}
|
||||
/* pawn: rank 7 push */
|
||||
movebits = pawn_push(pos->bb[me][PAWN] & rel_rank7, me) & empty;
|
||||
movebits = pawn_shift_up(pos->bb[me][PAWN] & rel_rank7, me) & empty;
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
from = pawn_push(to, enemy); /* reverse push */
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, QUEEN);
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, ROOK);
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, BISHOP);
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, KNIGHT);
|
||||
from = pawn_push_up(to, enemy); /* reverse push */
|
||||
moves[nmoves++] = move_make_promote(from, to, QUEEN);
|
||||
moves[nmoves++] = move_make_promote(from, to, ROOK);
|
||||
moves[nmoves++] = move_make_promote(from, to, BISHOP);
|
||||
moves[nmoves++] = move_make_promote(from, to, KNIGHT);
|
||||
}
|
||||
|
||||
/* pawn: ranks 2-6 captures left, including en-passant */
|
||||
from_pawns = pos->bb[me][PAWN] & ~rel_rank7 & ~rel_filea;
|
||||
movebits = pawn_take_left(from_pawns, me) & enemy_avail;
|
||||
from_pawns = pos->bb[me][PAWN] & ~rel_rank7; // & ~rel_filea;
|
||||
movebits = pawn_shift_upleft(from_pawns, me) & enemy_avail;
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
from = pawn_take_left(to, enemy); /* reverse capture */
|
||||
moves.move[moves.nmoves++] = move_make(from, to);
|
||||
from = pawn_push_upleft(to, enemy); /* reverse capture */
|
||||
moves[nmoves++] = move_make(from, to);
|
||||
}
|
||||
/* pawn: rank 7 captures left */
|
||||
from_pawns = pos->bb[me][PAWN] & rel_rank7 & ~rel_filea;
|
||||
movebits = pawn_take_left(from_pawns, me) & enemy_avail;
|
||||
from_pawns = pos->bb[me][PAWN] & rel_rank7; // & ~rel_filea;
|
||||
movebits = pawn_shift_upleft(from_pawns, me) & enemy_avail;
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
from = pawn_take_left(to, enemy); /* reverse capture */
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, QUEEN);
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, ROOK);
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, BISHOP);
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, KNIGHT);
|
||||
from = pawn_push_upleft(to, enemy); /* reverse capture */
|
||||
moves[nmoves++] = move_make_promote(from, to, QUEEN);
|
||||
moves[nmoves++] = move_make_promote(from, to, ROOK);
|
||||
moves[nmoves++] = move_make_promote(from, to, BISHOP);
|
||||
moves[nmoves++] = move_make_promote(from, to, KNIGHT);
|
||||
}
|
||||
|
||||
/* pawn: ranks 2-6 captures right, including en-passant */
|
||||
from_pawns = pos->bb[me][PAWN] & ~rel_rank7 & ~rel_fileh;
|
||||
movebits = pawn_take_right(from_pawns, me) & enemy_avail;
|
||||
from_pawns = pos->bb[me][PAWN] & ~rel_rank7; // & ~rel_fileh;
|
||||
movebits = pawn_shift_upright(from_pawns, me) & enemy_avail;
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
from = pawn_take_right(to, enemy);
|
||||
moves.move[moves.nmoves++] = move_make(from, to);
|
||||
from = pawn_push_upright(to, enemy);
|
||||
moves[nmoves++] = move_make(from, to);
|
||||
}
|
||||
/* pawn: rank 7 captures right */
|
||||
from_pawns = pos->bb[me][PAWN] & rel_rank7 & ~rel_fileh;
|
||||
movebits = pawn_take_right(from_pawns, me) & enemy_pieces;
|
||||
from_pawns = pos->bb[me][PAWN] & rel_rank7; // & ~rel_fileh;
|
||||
movebits = pawn_shift_upright(from_pawns, me) & enemy_pieces;
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
from = pawn_take_right(to, enemy); /* reverse capture */
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, QUEEN);
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, ROOK);
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, BISHOP);
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, KNIGHT);
|
||||
from = pawn_push_upright(to, enemy); /* reverse capture */
|
||||
moves[nmoves++] = move_make_promote(from, to, QUEEN);
|
||||
moves[nmoves++] = move_make_promote(from, to, ROOK);
|
||||
moves[nmoves++] = move_make_promote(from, to, BISHOP);
|
||||
moves[nmoves++] = move_make_promote(from, to, KNIGHT);
|
||||
}
|
||||
|
||||
/* castle - Attention ! We consider that castle flags are correct,
|
||||
* only empty squares between K ans R are tested.
|
||||
*/
|
||||
static struct {
|
||||
bitboard_t ooo;
|
||||
bitboard_t oo;
|
||||
} castle_empty[2] = {
|
||||
{ B1bb | C1bb | D1bb, F1bb | G1bb },
|
||||
{ B8bb | C8bb | D8bb, F8bb | G8bb }
|
||||
};
|
||||
static struct {
|
||||
square_t from;
|
||||
square_t ooo_to;
|
||||
square_t oo_to;
|
||||
} king_move[2] = {
|
||||
{ .from=E1, .ooo_to=C1, .oo_to=G1 },
|
||||
{ .from=E8, .ooo_to=C8, .oo_to=G8 },
|
||||
};
|
||||
/* so that bit0 is K castle flag, bit1 is Q castle flag */
|
||||
int castle_msk = pos->castle >> (2 * me);
|
||||
|
||||
//int castle_msk = pos->castle >> (2 * color);
|
||||
if (castle_msk & CASTLE_WK && !(occ & castle_empty[me].oo)) {
|
||||
moves[nmoves++] = move_make(king_move[me].from, king_move[me].oo_to);
|
||||
}
|
||||
if (castle_msk & CASTLE_WQ && !(occ & castle_empty[me].ooo)) {
|
||||
moves[nmoves++] = move_make(king_move[me].from, king_move[me].ooo_to);
|
||||
}
|
||||
/* TODO
|
||||
* DONE. pawn ranks 2-6 advance (1 push, + 2 squares for rank 2)
|
||||
* DONE. pawns rank 7 advance + promotions
|
||||
@@ -160,5 +215,5 @@ int gen_all_pseudomoves(pos_t *pos)
|
||||
* add function per piece, and type, for easier debug
|
||||
*
|
||||
*/
|
||||
return moves.nmoves;
|
||||
return (pos->moves.nmoves = nmoves);
|
||||
}
|
||||
|
40
src/piece.c
40
src/piece.c
@@ -25,22 +25,22 @@
|
||||
* piece_details
|
||||
*/
|
||||
const struct piece_details piece_details[PIECE_MAX] = {
|
||||
/* Abb AbbC Sym SymC Name start value */
|
||||
[EMPTY] = { ' ', ' ', " ", " ", " ", 0, 0, 0 },
|
||||
[W_PAWN] = { 'P', 'P', "♟", "♙", "Pawn", P_VAL_OPN, P_VAL_MID, P_VAL_END },
|
||||
[W_KNIGHT] = { 'N', 'N', "♞", "♘", "Knight", N_VAL_OPN, N_VAL_MID, N_VAL_END },
|
||||
[W_BISHOP] = { 'B', 'B', "♝", "♗", "Bishop", B_VAL_OPN, B_VAL_MID, B_VAL_END },
|
||||
[W_ROOK] = { 'R', 'R', "♜", "♖", "Rook", R_VAL_OPN, R_VAL_MID, R_VAL_END },
|
||||
[W_QUEEN] = { 'Q', 'Q', "♛", "♕", "Queen", Q_VAL_OPN, Q_VAL_MID, Q_VAL_END },
|
||||
[W_KING] = { 'K', 'K', "♚", "♔", "King", K_VAL_OPN, K_VAL_MID, K_VAL_END },
|
||||
[7] = { '7', '7', "<EFBFBD>", "<EFBFBD>", "Inv 7", 0, 0, 0 },
|
||||
[8] = { '8', '8', "<EFBFBD>", "<EFBFBD>", "Inv 8", 0, 0, 0 },
|
||||
[B_PAWN] = { 'P', 'p', "♟", "♟", "Pawn", P_VAL_OPN, P_VAL_MID, P_VAL_END },
|
||||
[B_KNIGHT] = { 'N', 'n', "♞", "♞", "Knight", P_VAL_OPN, N_VAL_MID, N_VAL_END },
|
||||
[B_BISHOP] = { 'B', 'b', "♝", "♝", "Bishop", P_VAL_OPN, B_VAL_MID, B_VAL_END },
|
||||
[B_ROOK] = { 'R', 'r', "♜", "♜", "Rook", P_VAL_OPN, R_VAL_MID, R_VAL_END },
|
||||
[B_QUEEN] = { 'Q', 'q', "♛", "♛", "Queen", P_VAL_OPN, Q_VAL_MID, Q_VAL_END },
|
||||
[B_KING] = { 'K', 'k', "♚", "♚", "King", P_VAL_OPN, K_VAL_MID, K_VAL_END },
|
||||
/* Abb Fen Sym SymC Name start value */
|
||||
[EMPTY] = { "", "", "", "", "", 0, 0, 0 },
|
||||
[W_PAWN] = { "", "P", "♟", "♙", "Pawn", P_VAL_OPN, P_VAL_MID, P_VAL_END },
|
||||
[W_KNIGHT] = { "N", "N", "♞", "♘", "Knight", N_VAL_OPN, N_VAL_MID, N_VAL_END },
|
||||
[W_BISHOP] = { "B", "B", "♝", "♗", "Bishop", B_VAL_OPN, B_VAL_MID, B_VAL_END },
|
||||
[W_ROOK] = { "R", "R", "♜", "♖", "Rook", R_VAL_OPN, R_VAL_MID, R_VAL_END },
|
||||
[W_QUEEN] = { "Q", "Q", "♛", "♕", "Queen", Q_VAL_OPN, Q_VAL_MID, Q_VAL_END },
|
||||
[W_KING] = { "K", "K", "♚", "♔", "King", K_VAL_OPN, K_VAL_MID, K_VAL_END },
|
||||
[7] = { "", "", "", "", "", 0, 0, 0 },
|
||||
[8] = { "", "", "", "", "", 0, 0, 0 },
|
||||
[B_PAWN] = { "", "p", "♟", "♟", "Pawn", P_VAL_OPN, P_VAL_MID, P_VAL_END },
|
||||
[B_KNIGHT] = { "N", "n", "♞", "♞", "Knight", P_VAL_OPN, N_VAL_MID, N_VAL_END },
|
||||
[B_BISHOP] = { "B", "b", "♝", "♝", "Bishop", P_VAL_OPN, B_VAL_MID, B_VAL_END },
|
||||
[B_ROOK] = { "R", "r", "♜", "♜", "Rook", P_VAL_OPN, R_VAL_MID, R_VAL_END },
|
||||
[B_QUEEN] = { "Q", "q", "♛", "♛", "Queen", P_VAL_OPN, Q_VAL_MID, Q_VAL_END },
|
||||
[B_KING] = { "K", "k", "♚", "♚", "King", P_VAL_OPN, K_VAL_MID, K_VAL_END },
|
||||
};
|
||||
|
||||
const char pieces_str[6+6+1] = "PNBRQKpnbrqk";
|
||||
@@ -51,14 +51,14 @@ bool piece_ok(piece_t p)
|
||||
return !(p & ~(MASK_COLOR | MASK_PIECE)) && pt && (pt <= KING);
|
||||
}
|
||||
|
||||
char piece_to_char(piece_t p)
|
||||
char *piece_to_char(piece_t p)
|
||||
{
|
||||
return piece_details[p].abbr;
|
||||
}
|
||||
|
||||
char piece_to_char_color(piece_t p)
|
||||
char *piece_to_char_color(piece_t p)
|
||||
{
|
||||
return piece_details[p].abbr_color;
|
||||
return piece_details[p].c_abbr;
|
||||
}
|
||||
|
||||
char *piece_to_sym(piece_t p)
|
||||
@@ -68,7 +68,7 @@ char *piece_to_sym(piece_t p)
|
||||
|
||||
char *piece_to_sym_color(piece_t p)
|
||||
{
|
||||
return piece_details[p].sym_color;
|
||||
return piece_details[p].c_sym;
|
||||
}
|
||||
|
||||
char *piece_to_name(piece_t p)
|
||||
|
22
src/piece.h
22
src/piece.h
@@ -70,13 +70,21 @@ typedef enum {
|
||||
#define K_VAL_END 20000
|
||||
|
||||
/* some default values for pieces
|
||||
* @abbr: char, piece capital letter (used for game notation)
|
||||
* @abbr_c: char, capital for white, lowercase for black
|
||||
* char *sym;
|
||||
* char *sym_c;
|
||||
* char *name;
|
||||
* s64 opn_value;
|
||||
* s64 mid_value;
|
||||
* s64 end_value;
|
||||
*/
|
||||
extern const struct piece_details {
|
||||
char abbr; /* used for game notation */
|
||||
char abbr_color; /* lowercase = black */
|
||||
char *abbr; /* used for game notation */
|
||||
char *c_abbr; /* lowercase = black */
|
||||
char *sym; /* used for game notation */
|
||||
char *sym_color; /* different W & B */
|
||||
char *name;
|
||||
char *c_sym; /* different W & B */
|
||||
char *name; /* piece name */
|
||||
s64 opn_value; /* value opening */
|
||||
s64 mid_value; /* value midgame */
|
||||
s64 end_value; /* value endgame */
|
||||
@@ -84,7 +92,7 @@ extern const struct piece_details {
|
||||
|
||||
extern const char pieces_str[6+6+1]; /* to search from fen/user input */
|
||||
|
||||
#define OPPONENT(p) !(p)
|
||||
#define OPPONENT(color) !(color)
|
||||
|
||||
#define MASK_PIECE 0x07 /* 00000111 */
|
||||
#define MASK_COLOR 0x08 /* 00001000 */
|
||||
@@ -102,8 +110,8 @@ extern const char pieces_str[6+6+1]; /* to search from fen/user inp
|
||||
|
||||
extern bool piece_ok(piece_t p);
|
||||
|
||||
extern char piece_to_char(piece_t p);
|
||||
extern char piece_to_char_color(piece_t p);
|
||||
extern char *piece_to_char(piece_t p);
|
||||
extern char *piece_to_char_color(piece_t p);
|
||||
extern char *piece_to_sym(piece_t p);
|
||||
extern char *piece_to_sym_color(piece_t p);
|
||||
extern char *piece_to_name(piece_t p);
|
||||
|
@@ -103,7 +103,9 @@ void pos_del(pos_t *pos)
|
||||
*/
|
||||
pos_t *pos_clear(pos_t *pos)
|
||||
{
|
||||
# ifdef DEBUG_POS
|
||||
printf("size(pos_board=%lu elt=%lu\n", sizeof(pos->board), sizeof(int));
|
||||
# endif
|
||||
//for (square square = A1; square <= H8; ++square)
|
||||
// pos->board[square] = EMPTY;
|
||||
|
||||
@@ -114,7 +116,7 @@ pos_t *pos_clear(pos_t *pos)
|
||||
pos->plycount = 0;
|
||||
pos->en_passant = SQUARE_NONE;
|
||||
pos->castle = 0;
|
||||
memset(pos->board, 0, sizeof(pos->board));
|
||||
memset(pos->board, EMPTY, sizeof(pos->board));
|
||||
//pos->curmove = 0;
|
||||
//pos->eval = 0;
|
||||
//pos->occupied[WHITE] = 0;
|
||||
@@ -124,7 +126,9 @@ pos_t *pos_clear(pos_t *pos)
|
||||
pos->bb[color][piece] = 0;
|
||||
pos->controlled[WHITE] = 0;
|
||||
pos->controlled[BLACK] = 0;
|
||||
pos->king[color] = SQUARE_NONE;
|
||||
}
|
||||
pos->moves.curmove = pos->moves.nmoves = 0;
|
||||
//pos->mobility[WHITE] = 0;
|
||||
//pos->mobility[BLACK] = 0;
|
||||
//pos->moves_generated = false;
|
||||
@@ -156,12 +160,12 @@ void pos_print(pos_t *pos)
|
||||
printf("%c |", rank + '1');
|
||||
for (file = 0; file < 8; ++file) {
|
||||
pc = board[sq_make(file, rank)];
|
||||
printf(" %s |", piece_to_sym_color(pc));
|
||||
printf(" %s |", pc? piece_to_sym_color(pc): " ");
|
||||
}
|
||||
printf("\n +---+---+---+---+---+---+---+---+\n");
|
||||
}
|
||||
printf(" A B C D E F G H\n");
|
||||
printf("%s\n", pos2fen(pos, fen));
|
||||
printf("fen %s\n", pos2fen(pos, fen));
|
||||
//printf("Turn: %s.\n", IS_WHITE(pos->turn) ? "white" : "black");
|
||||
/*
|
||||
* printf("Kings: W:%c%c B:%c%c\n",
|
||||
@@ -185,7 +189,7 @@ void pos_print(pos_t *pos)
|
||||
void pos_pieces_print(pos_t *pos)
|
||||
{
|
||||
int bit, count, cur;
|
||||
char pname;
|
||||
char *pname;
|
||||
u64 tmp;
|
||||
bitboard_t p;
|
||||
for (int color = WHITE; color <= BLACK; ++color) {
|
||||
@@ -194,7 +198,7 @@ void pos_pieces_print(pos_t *pos)
|
||||
count = popcount64(p);
|
||||
cur = 0;
|
||||
pname = piece_to_char(piece);
|
||||
printf("%c(0)%s", pname, count? ":": "");
|
||||
printf("%s(0)%s", pname, count? ":": "");
|
||||
if (count) {
|
||||
bit_for_each64(bit, tmp, p) {
|
||||
char cf = sq_file(bit), cr = sq_rank(bit);
|
||||
@@ -229,16 +233,29 @@ inline void bitboard_print2_raw(bitboard_t bb1, bitboard_t bb2, char *title)
|
||||
*/
|
||||
|
||||
/**
|
||||
* raw_board_print - print simple board (hex values)
|
||||
* pos_print_board_raw - print simple position board (octal/FEN symbol values)
|
||||
* @bb: the bitboard
|
||||
* @type: int, 0 for octal, 1 for fen symbol
|
||||
*/
|
||||
void raw_board_print(const pos_t *pos)
|
||||
void pos_print_board_raw(const pos_t *pos, int type)
|
||||
{
|
||||
|
||||
for (rank_t r = RANK_8; r >= RANK_1; --r) {
|
||||
for (file_t f = FILE_A; f <= FILE_H; ++f)
|
||||
printf("%02o ", pos->board[sq_make(f, r)]);
|
||||
printf(" \n");
|
||||
if (type == 0) {
|
||||
for (rank_t r = RANK_8; r >= RANK_1; --r) {
|
||||
for (file_t f = FILE_A; f <= FILE_H; ++f)
|
||||
printf("%02o ", pos->board[sq_make(f, r)]);
|
||||
printf(" \n");
|
||||
}
|
||||
} else {
|
||||
for (rank_t r = RANK_8; r >= RANK_1; --r) {
|
||||
for (file_t f = FILE_A; f <= FILE_H; ++f) {
|
||||
square_t sq = sq_make(f, r);
|
||||
if (pos->board[sq] == EMPTY)
|
||||
printf(". ");
|
||||
else
|
||||
printf("%s ", piece_to_char_color(pos->board[sq]));
|
||||
}
|
||||
printf(" \n");
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
@@ -19,16 +19,18 @@
|
||||
#include "brlib.h"
|
||||
#include "bitops.h"
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "chessdefs.h"
|
||||
#include "bitboard.h"
|
||||
#include "piece.h"
|
||||
#include "move.h"
|
||||
|
||||
typedef struct _pos_s {
|
||||
typedef struct __pos_s {
|
||||
u64 node_count; /* evaluated nodes */
|
||||
int turn; /* WHITE or BLACK */
|
||||
u16 clock_50;
|
||||
u16 plycount; /* plies so far, start is 0 */
|
||||
|
||||
square_t king[2]; /* dup with bb, faster retrieval */
|
||||
square_t en_passant;
|
||||
castle_rights_t castle;
|
||||
|
||||
@@ -41,14 +43,13 @@ typedef struct _pos_s {
|
||||
//bool moves_counted;
|
||||
|
||||
bitboard_t bb[2][PIECE_TYPE_MAX]; /* bb[0][PAWN], bb[1][ALL_PIECES] */
|
||||
square_t king[2]; /* dup with bb, faster retrieval */
|
||||
bitboard_t controlled[2];
|
||||
//u16 mobility[2];
|
||||
//struct list_head pieces[2]; /* pieces list, King is first */
|
||||
//struct list_head moves[2];
|
||||
piece_t board[BOARDSIZE];
|
||||
movelist_t moves;
|
||||
int nmoves;
|
||||
//int nmoves;
|
||||
} pos_t;
|
||||
|
||||
/**
|
||||
@@ -98,7 +99,7 @@ extern pos_t *pos_clear(pos_t *pos);
|
||||
extern void pos_print(pos_t *pos);
|
||||
extern void pos_pieces_print(pos_t *pos);
|
||||
|
||||
extern void raw_board_print(const pos_t *pos);
|
||||
extern void pos_print_board_raw(const pos_t *pos, int type);
|
||||
|
||||
//extern pos_t *pos_startpos(pos_t *pos);
|
||||
|
||||
|
@@ -41,13 +41,12 @@ int main(int __unused ac, __unused char**av)
|
||||
bb_between[C4][F3], bb_between[C4][E1],
|
||||
bb_between[C4][C1], bb_between[C4][A1],
|
||||
bb_between[C4][A3], bb_between[C4][A5]);
|
||||
/*
|
||||
* for (square_t sq = 0; sq < 64; ++sq) {
|
||||
* sprintf(str, "%2d %#lx %#lx knight", sq, bb_sq[sq], bb_knight[sq]);
|
||||
* bitboard_print(str, bb_knight[sq]);
|
||||
* sprintf(str, "%2d %#lx %#lx knight", sq, bb_sq[sq], bb_king[sq]);
|
||||
* bitboard_print(str, bb_king[sq]);
|
||||
* }
|
||||
*/
|
||||
|
||||
for (square_t sq = 0; sq < 64; ++sq) {
|
||||
sprintf(str, "%2d %#lx %#lx knight", sq, bb_sq[sq], bb_knight[sq]);
|
||||
bitboard_print(str, bb_knight[sq]);
|
||||
//sprintf(str, "%2d %#lx %#lx knight", sq, bb_sq[sq], bb_king[sq]);
|
||||
//bitboard_print(str, bb_king[sq]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@@ -26,8 +26,11 @@ int main(int ac, char**av)
|
||||
fen2pos(pos, fen);
|
||||
}
|
||||
pos_print(pos);
|
||||
|
||||
pos2fen(pos, revfen);
|
||||
//printf("reverse fen=[%s]\n", pos2fen(pos, NULL));
|
||||
comp = strcmp(fen, revfen);
|
||||
printf("compare=%d - %s\n", comp, comp? "NOK": "OK");
|
||||
pos_print_board_raw(pos, 0);
|
||||
pos_print_board_raw(pos, 1);
|
||||
}
|
||||
|
@@ -2,37 +2,309 @@
|
||||
//#include "pool.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../src/position.h"
|
||||
#include "../src/piece.h"
|
||||
#include "../src/bitboard.h"
|
||||
#include "../src/hyperbola-quintessence.h"
|
||||
#include "position.h"
|
||||
#include "piece.h"
|
||||
#include "bitboard.h"
|
||||
#include "fen.h"
|
||||
#include "move.h"
|
||||
#include "movegen.h"
|
||||
|
||||
static char *fentest[] = {
|
||||
/* tests rank movegen bug - FIXED */
|
||||
// "4k3/pppppppp/8/8/8/8/PPPPPPPP/2BRK3 w - - 0 1",
|
||||
// "4k3/pppppppp/8/8/8/8/PPPPPPPP/1B1R1K2 w - - 0 1",
|
||||
|
||||
// illegal positions (en-prise king)
|
||||
"4k3/8/8/8/7b/8/8/4K3 b - - 0 1",
|
||||
"2r1k3/3P4/8/8/8/8/8/4K3 w - - 0 1",
|
||||
|
||||
// initial pos
|
||||
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
|
||||
// position after 1.e4
|
||||
"rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1",
|
||||
// position after 1.Nh3
|
||||
"rnbqkbnr/pppppppp/8/8/8/7N/PPPPPPPP/RNBQKB1R b KQkq - 1 1",
|
||||
// after e4 e5 Nf3 Nc6
|
||||
"r1bqkbnr/pp1ppppp/2n5/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 0 1",
|
||||
|
||||
//
|
||||
"4k3/4p3/8/b7/1BR1p2p/1Q3P2/5N2/4K3 w - - 0 1",
|
||||
"r1bq1rk1/pppp1ppp/2n2n2/4p3/2B1P3/3PPN2/PPP3PP/RN1QK2R b KQ - 1 7",
|
||||
"6k1/6pp/R2p4/p1p5/8/1P1r3P/6P1/6K1 b - - 3 3",
|
||||
// both can castle queen only
|
||||
"r3k2r/8/3B4/8/8/3b4/8/R3K2R w KQkq - 0 1",
|
||||
"r3k2r/8/3BB3/8/8/3bb3/8/R3K2R w KQkq - 0 1",
|
||||
"r2bkb1r/8/8/8/8/3bb3/8/R2BKB1R w KQkq - 0 1",
|
||||
// 4 castle possible, only K+R
|
||||
"r3k2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1",
|
||||
// only kings on A1/A8, white to play
|
||||
"k7/8/8/8/8/8/8/K7 w - - 0 1",
|
||||
// only one move possible (Pf2xBg3)
|
||||
"k7/8/8/1p1p4/pPpPp3/P1PpPpb1/NBNP1P2/KBB1B3 w - - 0 1",
|
||||
// only 2 moves possible (Ph5xg6 e.p., Ph5-h6)
|
||||
"k7/8/8/1p1p2pP/pPpPp3/P1PpPp2/NBNP1P2/KBB1B3 w - g6 0 1",
|
||||
// 2 Kings, W/B/ pawns on 7th for promotion
|
||||
"k4n2/4P3/8/8/8/8/4p3/K4N2 w - - 0 1",
|
||||
// white castled, and can e.p. on c6 black can castle
|
||||
// white is a pawn down
|
||||
// white has 36 moves: P=11 + 1 e.p. N=6+3 B=5+5 R=1 Q=3 K=1 + 1 e.p.
|
||||
// black has 33 moves: P=11 N=2+7 B=5 R=3 Q=3 K=1 + castle
|
||||
"rnbqk2r/pp1pbpp1/7p/2pPp3/4n3/3B1N2/PPP2PPP/RNBQ1RK1 w kq c6 0 7",
|
||||
|
||||
// below tests are from:
|
||||
// - Rodent IV
|
||||
// - https://www.chessprogramming.net/perfect-perft/
|
||||
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1",
|
||||
"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1",
|
||||
"4rrk1/pp1n3p/3q2pQ/2p1pb2/2PP4/2P3N1/P2B2PP/4RRK1 b - - 7 19",
|
||||
"rq3rk1/ppp2ppp/1bnpb3/3N2B1/3NP3/7P/PPPQ1PP1/2KR3R w - - 7 14",
|
||||
"r1bq1r1k/1pp1n1pp/1p1p4/4p2Q/4Pp2/1BNP4/PPP2PPP/3R1RK1 w - - 2 14",
|
||||
"r3r1k1/2p2ppp/p1p1bn2/8/1q2P3/2NPQN2/PPP3PP/R4RK1 b - - 2 15",
|
||||
"1rbqk1nr/p3ppbp/2np2p1/2p5/1p2PP2/3PB1P1/PPPQ2BP/R2NK1NR b KQk - 0 1",
|
||||
"r1bqk2r/pp1p1ppp/2n1pn2/2p5/1bPP4/2NBP3/PP2NPPP/R1BQK2R b KQkq - 0 1",
|
||||
"rnb1kb1r/ppp2ppp/1q2p3/4P3/2P1Q3/5N2/PP1P1PPP/R1B1KB1R b KQkq - 0 1",
|
||||
"r1b2rk1/pp2nppp/1b2p3/3p4/3N1P2/2P2NP1/PP3PBP/R3R1K1 b - - 0 1",
|
||||
"n1q1r1k1/3b3n/p2p1bp1/P1pPp2p/2P1P3/2NBB2P/3Q1PK1/1R4N1 b - - 0 1",
|
||||
"r1bq1r1k/b1p1npp1/p2p3p/1p6/3PP3/1B2NN2/PP3PPP/R2Q1RK1 w - - 1 16",
|
||||
"3r1rk1/p5pp/bpp1pp2/8/q1PP1P2/b3P3/P2NQRPP/1R2B1K1 b - - 6 22",
|
||||
"r1q2rk1/2p1bppp/2Pp4/p6b/Q1PNp3/4B3/PP1R1PPP/2K4R w - - 2 18",
|
||||
"4k2r/1pb2ppp/1p2p3/1R1p4/3P4/2r1PN2/P4PPP/1R4K1 b - - 3 22",
|
||||
"3q2k1/pb3p1p/4pbp1/2r5/PpN2N2/1P2P2P/5PP1/Q2R2K1 b - - 4 26",
|
||||
"2r5/8/1n6/1P1p1pkp/p2P4/R1P1PKP1/8/1R6 w - - 0 1",
|
||||
"r2q1rk1/1b1nbppp/4p3/3pP3/p1pP4/PpP2N1P/1P3PP1/R1BQRNK1 b - - 0 1",
|
||||
"6k1/5pp1/7p/p1p2n1P/P4N2/6P1/1P3P1K/8 w - - 0 35",
|
||||
"r4rk1/1pp1q1pp/p2p4/3Pn3/1PP1Pp2/P7/3QB1PP/2R2RK1 b - - 0 1",
|
||||
|
||||
"r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1",
|
||||
"1k6/1b6/8/8/7R/8/8/4K2R b K - 0 1",
|
||||
"3k4/3p4/8/K1P4r/8/8/8/8 b - - 0 1", // Illegal ep move #1
|
||||
"8/8/4k3/8/2p5/8/B2P2K1/8 w - - 0 1", // Illegal ep move #2
|
||||
"8/8/1k6/2b5/2pP4/8/5K2/8 b - d3 0 1", // EP Capture Checks Opponent
|
||||
"5k2/8/8/8/8/8/8/4K2R w K - 0 1", // Short Castling Gives Check
|
||||
"3k4/8/8/8/8/8/8/R3K3 w Q - 0 1", // Long Castling Gives Check
|
||||
"r3k2r/1b4bq/8/8/8/8/7B/R3K2R w KQkq - 0 1", // Castle Rights
|
||||
"r3k2r/8/3Q4/8/8/5q2/8/R3K2R b KQkq - 0 1", // Castling Prevented
|
||||
"2K2r2/4P3/8/8/8/8/8/3k4 w - - 0 1", // Promote out of Check
|
||||
"8/8/1P2K3/8/2n5/1q6/8/5k2 b - - 0 1", // Discovered Check
|
||||
"4k3/1P6/8/8/8/8/K7/8 w - - 0 1", // Promote to give check
|
||||
"8/P1k5/K7/8/8/8/8/8 w - - 0 1", // Under Promote to give check
|
||||
"K1k5/8/P7/8/8/8/8/8 w - - 0 1", // Self Stalemate
|
||||
"8/k1P5/8/1K6/8/8/8/8 w - - 0 1", // Stalemate & Checkmate
|
||||
"8/8/2k5/5q2/5n2/8/5K2/8 b - - 0 1", // Stalemate & Checkmate
|
||||
|
||||
NULL
|
||||
};
|
||||
|
||||
#define RD 0
|
||||
#define WR 1
|
||||
|
||||
/**
|
||||
* return write pipd fd
|
||||
*/
|
||||
static FILE *open_stockfish()
|
||||
{
|
||||
int rpipe[2], wpipe[2];
|
||||
FILE *out_desc;
|
||||
pid_t pid;
|
||||
|
||||
if ((pipe(rpipe) < 0) || (pipe(wpipe) < 0)) {
|
||||
perror("pipe");
|
||||
return NULL;
|
||||
}
|
||||
if ((pid = fork()) < 0) {
|
||||
perror("fork");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!pid) { /* stockfish */
|
||||
setvbuf(stdin, NULL, _IOLBF, 0);
|
||||
setvbuf(stdout, NULL, _IOLBF, 0);
|
||||
setvbuf(stderr, NULL, _IOLBF, 0);
|
||||
|
||||
dup2(wpipe[RD], STDIN_FILENO);
|
||||
dup2(rpipe[WR], STDOUT_FILENO);
|
||||
dup2(rpipe[WR], STDERR_FILENO);
|
||||
|
||||
close(wpipe[RD]);
|
||||
close(wpipe[WR]);
|
||||
close(rpipe[RD]);
|
||||
close(rpipe[WR]);
|
||||
if (execlp("stockfish", "stockfish", NULL) == -1) {
|
||||
perror("execlp");
|
||||
return NULL;
|
||||
}
|
||||
return 0; /* not reached */
|
||||
}
|
||||
/* us */
|
||||
dup2(rpipe[RD], STDIN_FILENO);
|
||||
setvbuf(stdin, NULL, _IOLBF, 0);
|
||||
|
||||
close(wpipe[RD]);
|
||||
close(rpipe[RD]);
|
||||
close(rpipe[WR]);
|
||||
|
||||
out_desc = fdopen(wpipe[WR], "w");
|
||||
setvbuf(out_desc, NULL, _IOLBF, 0);
|
||||
|
||||
return out_desc;
|
||||
}
|
||||
|
||||
static void send_stockfish_fen(FILE *desc, pos_t *pos, char *fen)
|
||||
{
|
||||
char *buf = NULL;
|
||||
int count, __unused mycount = 0, fishcount;
|
||||
size_t alloc = 0;
|
||||
ssize_t buflen;
|
||||
pos_clear(pos);
|
||||
move_t *moves = pos->moves.move;
|
||||
int nmoves = pos->moves.nmoves;
|
||||
//char nodescount[] = "Nodes searched";
|
||||
printf("nmoves = %d\n", nmoves);
|
||||
fflush(stdout);
|
||||
//sprintf(str, "stockfish \"position fen %s\ngo perft depth\n\"", fen);
|
||||
fprintf(desc, "position fen %s\ngo perft 1\n", fen);
|
||||
//fflush(desc);
|
||||
while ((buflen = getline(&buf, &alloc, stdin)) > 0) {
|
||||
buf[--buflen] = 0;
|
||||
if (buflen == 0)
|
||||
continue;
|
||||
if (sscanf(buf, "Nodes searched: %d", &fishcount) == 1) {
|
||||
break;
|
||||
}
|
||||
//printf("%d: %s\n", line++, buf);
|
||||
if (sscanf(buf, "%*4s: %d", &count) == 1) {
|
||||
square_t from = sq_from_string(buf);
|
||||
square_t to = sq_from_string(buf + 2);
|
||||
mycount += count;
|
||||
//printf("move found: %c%c->%c%c %s->%s count=%d\n",
|
||||
// buf[0], buf[1], buf[2], buf[3],
|
||||
// sq_to_string(from), sq_to_string(to),
|
||||
// count);
|
||||
moves[nmoves++] = move_make(from, to);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
pos->moves.nmoves = nmoves;
|
||||
// printf("fishcount=%d mycount=%d\n", fishcount, mycount);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
static void compare_moves(pos_t *fish, pos_t *me)
|
||||
{
|
||||
char str1[1024] = {0}, str2[1024] = {0}, tmpstr[1024];
|
||||
char *skip = " ";
|
||||
move_t *m1 = fish->moves.move;
|
||||
move_t *m2 = me->moves.move;
|
||||
int n1 = fish->moves.nmoves;
|
||||
int n2 = me->moves.nmoves;
|
||||
|
||||
#define f(c) move_from(c)
|
||||
#define t(c) move_to(c)
|
||||
|
||||
for (move_t *c1 = m1, *c2 = m2; (c1 - m1 < n1) || (c2 - m2 < n2);) {
|
||||
// square_t f1 = move_from(*c1); square_t t1 = move_to(*c1);
|
||||
// square_t f2 = move_from(*c2); square_t t2 = move_to(*c2);
|
||||
|
||||
/* no more move in c2 */
|
||||
if (c2 - m2 >= n2) {
|
||||
while (c1 - m1 < n1) {
|
||||
sprintf(tmpstr, " %s-%s", sq_to_string(f(*c1)), sq_to_string(t(*c1)));
|
||||
strcat (str1, tmpstr);
|
||||
c1++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (c1 - m1 >= n1) {
|
||||
while (c2 - m2 < n2) {
|
||||
sprintf(tmpstr, " %s-%s", sq_to_string(f(*c2)), sq_to_string(t(*c2)));
|
||||
strcat (str2, tmpstr);
|
||||
c2++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/* missing move in c2 */
|
||||
if (f(*c1) < f(*c2) ||
|
||||
(f(*c1) == f(*c2) && t(*c1) < t(*c2))) {
|
||||
strcat(str2, skip);
|
||||
sprintf(tmpstr, " %s-%s", sq_to_string(f(*c1)), sq_to_string(t(*c1)));
|
||||
strcat (str1, tmpstr);
|
||||
while ((c1 - m1 < n1) && (f(*c1) < f(*c2) ||
|
||||
(f(*c1) == f(*c2) && t(*c1) < t(*c2)))) {
|
||||
c1++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
/* missing move in c1 */
|
||||
if (f(*c1) > f(*c2) ||
|
||||
(f(*c1) == f(*c2) && t(*c1) > t(*c2))) {
|
||||
strcat(str1, skip);
|
||||
sprintf(tmpstr, " %s-%s", sq_to_string(f(*c2)), sq_to_string(t(*c2)));
|
||||
strcat (str2, tmpstr);
|
||||
while ((c2 - m2 < n2) && (f(*c1) > f(*c2) ||
|
||||
(f(*c1) == f(*c2) && t(*c1) > t(*c2)))) {
|
||||
c2++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
sprintf(tmpstr, " %s-%s", sq_to_string(f(*c1)), sq_to_string(t(*c1)));
|
||||
strcat(str1, tmpstr);
|
||||
strcat(str2, tmpstr);
|
||||
c1++, c2++;
|
||||
}
|
||||
printf("F(%2d): %s\nM(%2d): %s\n", n1, str1, n2, str2);
|
||||
}
|
||||
|
||||
int main(int __unused ac, __unused char**av)
|
||||
{
|
||||
char str[256];
|
||||
int i = 0;
|
||||
FILE *outfd;
|
||||
pos_t *mypos = pos_new(), *fishpos = pos_new();
|
||||
|
||||
//bitboard_t wrong = 0x5088000040, tmp, loop;
|
||||
//bit_for_each64(loop, tmp, )
|
||||
|
||||
bitboard_init();
|
||||
hyperbola_init();
|
||||
for (int i = 0; i < 64; ++i) {
|
||||
sprintf(str, "\n%#x:\n %-22s%-22s%-22s%-22s%-22s%-22s%-22s", i,
|
||||
"sliding", "diagonal", "antidiagonal", "file", "rank", "knight",
|
||||
"king"
|
||||
);
|
||||
bitboard_print_multi(str, 7,
|
||||
bb_file[i] | bb_rank[i] |
|
||||
bb_diagonal[i] | bb_antidiagonal[i],
|
||||
bb_diagonal[i], bb_antidiagonal[i],
|
||||
bb_file[i], bb_rank[i],
|
||||
bb_knight[i], bb_king[i]);
|
||||
outfd = open_stockfish();
|
||||
|
||||
while (fentest[i]) {
|
||||
//printf(">>>>> %s\n", test[i]);
|
||||
fen2pos(mypos, fentest[i]);
|
||||
pos_print(mypos);
|
||||
|
||||
send_stockfish_fen(outfd, fishpos, fentest[i]);
|
||||
printf("Fu ");
|
||||
moves_print(fishpos, 0);
|
||||
fflush(stdout);
|
||||
gen_all_pseudomoves(mypos);
|
||||
printf("Mu ");
|
||||
moves_print(mypos, 0);
|
||||
fflush(stdout);
|
||||
puts("zobi");
|
||||
fflush(stdout);
|
||||
move_sort_by_sq(fishpos);
|
||||
printf("\nFs ");
|
||||
moves_print(fishpos, 0);
|
||||
fflush(stdout);
|
||||
|
||||
move_sort_by_sq(mypos);
|
||||
printf("Ms ");
|
||||
moves_print(mypos, 0);
|
||||
fflush(stdout);
|
||||
printf("\n");
|
||||
compare_moves(fishpos, mypos);
|
||||
//pos_print_board_raw(pos, 1);
|
||||
//printf("%s\n", pos2fen(pos, str));
|
||||
//get_stockfish_moves(test[i]);
|
||||
//exit(0);
|
||||
i++;
|
||||
}
|
||||
/*
|
||||
* for (square_t sq = 0; sq < 64; ++sq) {
|
||||
* sprintf(str, "%2d %#lx %#lx knight", sq, bb_sq[sq], bb_knight[sq]);
|
||||
* bitboard_print(str, bb_knight[sq]);
|
||||
* sprintf(str, "%2d %#lx %#lx knight", sq, bb_sq[sq], bb_king[sq]);
|
||||
* bitboard_print(str, bb_king[sq]);
|
||||
* }
|
||||
*/
|
||||
fclose(outfd);
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user