start bitboard integration

This commit is contained in:
2023-06-22 16:08:57 +02:00
parent cb94ca52b9
commit 1154f141c9
11 changed files with 113 additions and 52 deletions

View File

@@ -5,7 +5,7 @@
* 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.htmlL>.
* 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>
*
@@ -14,7 +14,7 @@
#ifndef CHESSDEFS_H
#define CHESSDEFS_H
#include "bits.h"
#include <bits.h>
/* piece_t bits structure
*/
@@ -31,6 +31,19 @@ enum {
E_KING,
};
/* pos_t bitboards tables
*/
enum {
BB_ALL = 0, /* OR of all bitboards */
BB_UNUSED, /* future use ? */
BB_PAWN = E_PAWN,
BB_KNIGHT,
BB_BISHOP,
BB_ROOK,
BB_QUEEN,
BB_KING,
BB_END
};
/* piece bitmask in piece_t
*/
@@ -44,6 +57,8 @@ enum {
KING = 1 << (E_KING - 1), /* 0x40 01000000 */
};
#define PIECETOBB(p) (ffs64(PIECE(p))) /* from piece_t to bb piece array */
#define WHITE 0 /* 0x00 00000000 */
#define BLACK 1 /* 0x01 00000001 */
#define OPPONENT(p) !(p)

View File

@@ -5,7 +5,7 @@
* 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.htmlL>.
* 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>
*
@@ -13,26 +13,33 @@
#include <stdio.h>
#include <list.h>
#include <debug.h>
#include "eval.h"
#include "list.h"
#include "debug.h"
eval_t eval(pos_t *pos)
{
eval_t material[2] = {0};
eval_t control[2] = {0};
eval_t res = 0;
struct list_head *p_cur, *p_tmp, *list;
piece_list_t *piece;
//printf("val(%d-%c) = %lu\n", PAWN, P_LETTER(PAWN), P_VALUE(PAWN));
//printf("val(%d-%c) = %lu\n", KNIGHT, P_LETTER(KNIGHT), P_VALUE(KNIGHT));
//bitboard_print2(pos->bb[0][BB_PAWN], pos->bb[1][BB_PAWN]);
//bitboard_print2(pos->bb[0][BB_QUEEN], pos->bb[1][BB_QUEEN]);
/* 1) pieces value
*/
for (int color=0; color <2; ++color) {
list = &pos->pieces[color];
list_for_each_safe(p_cur, p_tmp, list) {
piece = list_entry(p_cur, piece_list_t, list);
if (PIECE(piece->piece) != KING)
material[color] += piece->value;
for (uint color = 0; color < 2; ++color) {
for (uint piece = PAWN; piece <= KING; piece <<= 1) {
uint bb = PIECETOBB(piece);
# ifdef DEBUG_EVAL
log_f(2, "color=%u piece=%u bb=%u=%c count=%ul val=%ld\n",
color, piece, bb, P_LETTER(piece), popcount64(pos->bb[color][bb]),
P_VALUE(piece));
# endif
/* attention here */
material[color] += popcount64(pos->bb[color][bb]) * P_VALUE(piece);
}
}
@@ -54,11 +61,11 @@ eval_t eval(pos_t *pos)
res, (float)res/10);
# endif
/* 3) mobility: 5 mobility diff = 1 pawn
/* 3) mobility: 10 mobility diff = 1 pawn
*/
res = pos->mobility[WHITE] - pos->mobility[BLACK];
# ifdef DEBUG_EVAL
log_f(2, "mobility: W:%ld B:%ld eval=%ld (%.3f pawns)\n",
log_f(2, "mobility: W:%ud B:%ud eval=%ld (%.3f pawns)\n",
pos->mobility[WHITE], pos->mobility[BLACK],
res, (float)res/5);
# endif
@@ -83,7 +90,7 @@ int main(int ac, char**av)
pos_t *pos;
eval_t res;
debug_init(2);
debug_level_set(9);
piece_pool_init();
moves_pool_init();
pos_pool_init();
@@ -95,10 +102,15 @@ int main(int ac, char**av)
fen2pos(pos, av[1]);
}
moves_gen(pos, OPPONENT(pos->turn), false);
moves_gen(pos, pos->turn, true);
pos_print(pos);
pos_pieces_print(pos);
moves_gen(pos, OPPONENT(pos->turn), false);
moves_print(pos, M_PR_SEPARATE);
//pos_print(pos);
//pos_pieces_print(pos);
moves_gen(pos, pos->turn, false);
moves_print(pos, M_PR_SEPARATE);
res = eval(pos);
printf("eval=%ld (%.3f pawns)\n", res, (float)res/100);
}

View File

@@ -5,7 +5,7 @@
* 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.htmlL>.
* 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>
*

View File

@@ -5,7 +5,7 @@
* 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.htmlL>.
* 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>
*
@@ -17,7 +17,8 @@
#include <string.h>
#include <ctype.h>
#include "debug.h"
#include <debug.h>
#include "chessdefs.h"
#include "position.h"
#include "board.h"
@@ -48,7 +49,7 @@
pos_t *fen2pos(pos_t *pos, char *fen)
{
char *p = fen;
short rank, file, skip, color;
short rank, file, skip, color, bbpiece;
piece_t piece;
board_t *board = pos->board;
# define SKIP_BLANK(p) for(;*(p) == ' '; (p)++)
@@ -61,29 +62,36 @@ pos_t *fen2pos(pos_t *pos, char *fen)
char cp = toupper(*p);
switch (cp) {
case CHAR_PAWN:
bbpiece = BB_PAWN;
piece = PAWN;
goto set_square;
case CHAR_KNIGHT:
bbpiece = BB_KNIGHT;
piece = KNIGHT;
goto set_square;
case CHAR_BISHOP:
bbpiece = BB_BISHOP;
piece = BISHOP;
goto set_square;
case CHAR_ROOK:
bbpiece = BB_ROOK;
piece = ROOK;
goto set_square;
case CHAR_QUEEN:
bbpiece = BB_QUEEN;
piece = QUEEN;
goto set_square;
case CHAR_KING:
bbpiece = BB_KING;
piece = KING;
pos->king[color]=SQ88(file, rank);
//pos->bb[color][BB_KING] = BB(file, rank);
//goto set_square;
set_square:
# ifdef DEBUG_FEN
log_i(5, "f=%d r=%d *p=%c piece=%c color=%d\n",
file, rank, *p, cp, color);
# endif
pos->bb[color][bbpiece] |= BB(file, rank);
pos->occupied[color] |= BB(file, rank);
SET_COLOR(piece, color);
board[SQ88(file, rank)].piece = piece;
@@ -158,8 +166,11 @@ pos_t *fen2pos(pos_t *pos, char *fen)
* 6) current move number
*/
SKIP_BLANK(p);
log_i(5, "pos=%d\n", (int)(p-fen));
//log_i(5, "pos=%d\n", (int)(p-fen));
sscanf(p, "%hd %hd", &pos->clock_50, &pos->curmove);
# ifdef DEBUG_FEN
log_i(5, "50 rule=%d current move=%d\n", pos->clock_50, pos->curmove);
# endif
return pos;
}

View File

@@ -5,7 +5,7 @@
* 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.htmlL>.
* 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>
*

View File

@@ -5,7 +5,7 @@
* 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.htmlL>.
* 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>
*
@@ -14,12 +14,13 @@
#include <malloc.h>
#include <ctype.h>
#include <list.h>
#include <debug.h>
#include "chessdefs.h"
#include "board.h"
#include "piece.h"
#include "move.h"
#include "list.h"
#include "debug.h"
static pool_t *moves_pool;
@@ -50,7 +51,7 @@ static struct can_castle {
pool_t *moves_pool_init()
{
if (!moves_pool)
moves_pool = pool_init("moves", 128, sizeof(move_t));
moves_pool = pool_create("moves", 128, sizeof(move_t));
return moves_pool;
}
@@ -530,8 +531,8 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece, bool doit)
//bitboard_print(pos->occupied[color]);
//bitboard_print(pos->occupied[OPPONENT(color)]);
# ifdef DEBUG_MOVE
log_i(2, "BB: skipping %#llx [%c%c] (same color piece)\n",
new, FILE2C(F88(new)), RANK2C(R88(new)));
log_i(2, "BB: skipping %#lx [%c%c] (same color piece)\n",
bb_new, FILE2C(F88(new)), RANK2C(R88(new)));
# endif
break;
}
@@ -566,6 +567,7 @@ int moves_gen(pos_t *pos, bool color, bool doit)
piece_list = &pos->pieces[color];
pos->mobility[color] = 0;
pos->controlled[color] = 0;
if (doit)
pseudo_moves_castle(pos);
list_for_each_safe(p_cur, tmp, piece_list) {

View File

@@ -5,7 +5,7 @@
* 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.htmlL>.
* 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>
*

View File

@@ -5,7 +5,7 @@
* 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.htmlL>.
* 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>
*
@@ -14,10 +14,13 @@
#include <malloc.h>
#include <ctype.h>
#include <debug.h>
#include <pool.h>
#include <list.h>
#include "chessdefs.h"
#include "piece.h"
#include "board.h"
#include "debug.h"
#include "position.h"
static pool_t *pieces_pool;
@@ -50,7 +53,7 @@ void piece_list_print(struct list_head *list)
pool_t *piece_pool_init()
{
if (!pieces_pool)
pieces_pool = pool_init("pieces", 128, sizeof(piece_list_t));
pieces_pool = pool_create("pieces", 128, sizeof(piece_list_t));
return pieces_pool;
}
@@ -115,30 +118,37 @@ int pieces_del(pos_t *pos, short color)
int main(int ac, char**av)
{
pos_t *pos;
debug_init(5);
printf("zobi\n");fflush(stdout);
debug_level_set(6);
log_f(5, "kfsjdhg\n");
pos_pool_init();
pos = pos_get();
piece_pool_init();
if (ac == 1) {
printf("zoba\n");fflush(stdout);
pos_startpos(pos);
} else {
fen2pos(pos, av[1]);
}
pos_print(pos);
pos_pieces_print(pos);
printf("0x1c:\n");
printf("0x1c = 11100 = C1-E1:\n");
bitboard_print(0x1c);
printf("0x70:\n");
printf("0x70 = 111 = A1-C1\n");
bitboard_print(0x70);
printf("0x0e:\n");
printf("0x0e = 1110 = B1-D1\n");
bitboard_print(0x0e);
printf("0x60:\n");
printf("0x60 = 1100000 = F1-G1\n");
bitboard_print(0x60);
printf("A1:\n");
bitboard_print(A1);
printf("1:\n");
bitboard_print(1L);
printf("H1:\n");

View File

@@ -5,7 +5,7 @@
* 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.htmlL>.
* 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>
*
@@ -56,6 +56,7 @@ extern struct piece_details {
piece_details[E_PIECE(p)].abbrev_b)
#define P_CSYM(p) (IS_WHITE(p)? piece_details[E_PIECE(p)].symbol_w: \
piece_details[E_PIECE(p)].symbol_b)
#define P_VALUE(p) (piece_details[E_PIECE(p)].value)
/* use short name or symbol - no effect
*/
#define P_USE_UTF 1

View File

@@ -5,7 +5,7 @@
* 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.htmlL>.
* 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>
*
@@ -147,6 +147,9 @@ pos_t *pos_clear(pos_t *pos)
pos->king[BLACK] = 0;
pos->occupied[WHITE] = 0;
pos->occupied[BLACK] = 0;
for (int color=0; color<2; ++color)
for (int piece = BB_ALL; piece < BB_END; ++piece)
pos->bb[color][piece] = 0;
pos->controlled[WHITE] = 0;
pos->controlled[BLACK] = 0;
pos->mobility[WHITE] = 0;
@@ -172,8 +175,11 @@ pos_t *pos_get()
if (pos) {
INIT_LIST_HEAD(&pos->pieces[WHITE]);
INIT_LIST_HEAD(&pos->pieces[BLACK]);
INIT_LIST_HEAD(&pos->moves);
pos_clear(pos);
} else {
fprintf(stderr, "zobaaa\n");
}
return pos;
}
@@ -211,7 +217,7 @@ pos_t *pos_dup(pos_t *pos)
pool_t *pos_pool_init()
{
if (!pos_pool)
pos_pool = pool_init("positions", 128, sizeof(pos_t));
pos_pool = pool_create("positions", 128, sizeof(pos_t));
return pos_pool;
}

View File

@@ -5,7 +5,7 @@
* 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.htmlL>.
* 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>
*
@@ -15,10 +15,12 @@
#define POSITION_H
#include <stdint.h>
#include "chessdefs.h"
#include "board.h"
#include "pool.h"
#include "list.h"
#include <pool.h>
#include <list.h>
#include <bits.h>
#include "chessdefs.h"
typedef struct pos_s {
piece_t turn; /* we use only color bit */
@@ -30,8 +32,10 @@ typedef struct pos_s {
board_t board[BOARDSIZE];
square_t en_passant;
square_t king[2];
bitboard_t occupied[2];
square_t king[2]; /* obsolete by bb array */
bitboard_t bb[2][BB_END]; /* use: pieces[BLACK][BB_PAWN] */
bitboard_t occupied[2]; /* OR of bb[COLOR][x] */
bitboard_t controlled[2];
struct list_head pieces[2];
struct list_head moves;