major rework

This commit is contained in:
2026-01-03 16:22:42 +01:00
parent 4998a057f5
commit 716527fa8f
14 changed files with 2431 additions and 2261 deletions

View File

@@ -1,3 +1,4 @@
#pragma once
#include <ctype.h>
#include <locale.h>
@@ -9,15 +10,15 @@
#define INDEX_FMT PRIu8
static void bitboard_print(bitboard b, FILE* out)
static void bitboard_print(Bb64 b, FILE* out)
{
setlocale(LC_ALL, "");
fprintf(out, "\n");
for (index i = 7; i < 8; i--) {
for (Index8 i = 7; i < 8; i--) {
fprintf(out, "\033[0m"); /* reset background color */
fprintf(out, "%"INDEX_FMT" ", i+1);
for (index j = 0; j < 8; ++j) {
index const n = INDEX_FROM_RF(i,j);
for (Index8 j = 0; j < 8; ++j) {
Index8 const n = SQ_FROM_RF(i,j);
int color;
if ((b >> n) & 1) {
/* 41: red */
@@ -35,59 +36,6 @@ static void bitboard_print(bitboard b, FILE* out)
fprintf(out, " A B C D E F G H \n");
}
static void board_print_threats(const struct pos* pos, FILE* out, struct move* move)
{
enum player const player = pos->player;
int buf[8][8] = {0};
bitboard const threats = all_threats_from_player(pos, player);
for (enum player player = PLAYER_BEGIN; player < PLAYER_COUNT; ++player) {
for (enum piece piece = PIECE_BEGIN; piece < PIECE_COUNT; ++piece) {
bitboard x = pos->pieces[player][piece];
for (index i = 7; i < 8; i--) {
for (index j = 0; j < 8; ++j) {
if (x & (1ULL<<(i*8+j))) {
buf[i][j] = piece_unicode[player][piece];
}
}
}
}
}
/* see: https://en.wikipedia.org/wiki/ANSI_escape_code#Colors */
setlocale(LC_ALL, ""); /* needed for unicode symbols */
for (index i = 7; i < 8; i--) {
fprintf(out, "\033[0m"); /* reset background color */
fprintf(out, "%"INDEX_FMT" ", i+1);
for (index j = 0; j < 8; ++j) {
index const n = INDEX_FROM_RF(i,j);
if (move && n == move->from) {
fprintf(out, "\033[%d;%dm", 30, 44); /* 44: blue*/
} else if (move && n == move->to) {
fprintf(out, "\033[%d;%dm", 30, 44); /* 104: bright blue*/
}
else if ((threats >> n) & 1) {
fprintf(out, "\033[%d;%dm", 30, 41); /* 41: red */
} else {
/* 45: magenta, 47: white */
fprintf(out, "\033[%d;%dm", 30, (i+j) % 2 ? 45 : 47);
}
if (buf[i][j]) {
fprintf(out, "%lc ", buf[i][j]);
} else {
fprintf(out, " "); /* idk why this hack is needed but "%lc "
is not working when buf[i][j] = ' ' */
}
}
fprintf(out, "\033[0m"); /* reset background color */
fprintf(out, "\n");
}
fprintf(out, " A B C D E F G H \n");
}
static void tt_print_stats(struct tt* tt, FILE* out)
{
#ifndef NDEBUG
@@ -105,6 +53,7 @@ static void tt_print_stats(struct tt* tt, FILE* out)
fprintf(out, "tt insertions: %"PRIu64"\n", tt->insertions);
fprintf(out, "saturation: %.02lf\n", pct);
#else
(void)tt;
fprintf(out, "stats not available with NDEBUG\n");
#endif
}
@@ -113,14 +62,14 @@ static void board_print_fen(struct pos const* pos, FILE* out)
{
int buf[8][8] = {0};
for (enum player player = PLAYER_BEGIN; player < PLAYER_COUNT; ++player) {
for (enum piece piece = PIECE_BEGIN; piece < PIECE_COUNT; ++piece) {
bitboard x = pos->pieces[player][piece];
for (Side8 side = SIDE_BEGIN; side < SIDE_COUNT; ++side) {
for (Piece8 piece = PIECE_BEGIN; piece < PIECE_COUNT; ++piece) {
Bb64 x = pos->pieces[side][piece];
for (index i = 7; i < 8; i--) {
for (index j = 0; j < 8; ++j) {
for (Index8 i = 7; i < 8; i--) {
for (Index8 j = 0; j < 8; ++j) {
if (x & (1ULL<<(i*8+j))) {
buf[i][j] = piece_char[player][piece];
buf[i][j] = piece_char[side][piece];
}
}
}
@@ -147,22 +96,22 @@ static void board_print_fen(struct pos const* pos, FILE* out)
fprintf(out, "/");
}
fprintf(out, " %c ", pos->player == PLAYER_WHITE ? 'w' : 'b');
fprintf(out, " %c ", pos->moving_side == SIDE_WHITE ? 'w' : 'b');
bool any_castle = false;
if (!pos->castling_illegal[PLAYER_WHITE][CASTLE_KINGSIDE]) {
if (!pos->castling_illegal[SIDE_WHITE][CASTLE_KINGSIDE]) {
fprintf(out, "K");
any_castle = true;
}
if (!pos->castling_illegal[PLAYER_WHITE][CASTLE_QUEENSIDE]) {
if (!pos->castling_illegal[SIDE_WHITE][CASTLE_QUEENSIDE]) {
fprintf(out, "Q");
any_castle = true;
}
if (!pos->castling_illegal[PLAYER_BLACK][CASTLE_KINGSIDE]) {
if (!pos->castling_illegal[SIDE_BLACK][CASTLE_KINGSIDE]) {
fprintf(out, "k");
any_castle = true;
}
if (!pos->castling_illegal[PLAYER_BLACK][CASTLE_QUEENSIDE]) {
if (!pos->castling_illegal[SIDE_BLACK][CASTLE_QUEENSIDE]) {
fprintf(out, "q");
any_castle = true;
}
@@ -172,18 +121,18 @@ static void board_print_fen(struct pos const* pos, FILE* out)
if (pos->ep_targets) {
/* should be ep target square in algebraic notation */
enum square_index const sqi = bitboard_lsb(pos->ep_targets);
Sq8 const sqi = bitboard_lsb(pos->ep_targets);
assert(sqi >= SQ_INDEX_BEGIN && sqi < SQ_INDEX_COUNT);
assuming(sqi >= SQ_BEGIN && sqi < SQ_COUNT);
enum file_index const fi = index_to_file(sqi);
enum rank_index const ri = index_to_rank(sqi);
enum file_index const fi = sq_to_file(sqi);
enum rank_index const ri = sq_to_rank(sqi);
int const fch = tolower(file_index_char[fi]);
int const rch = tolower(rank_index_char[ri]);
assert(fch >= 'a' && fch <= 'h');
assert(rch >= '1' && rch <= '8');
assuming(fch >= 'a' && fch <= 'h');
assuming(rch >= '1' && rch <= '8');
fprintf(out, " %c%c", fch, rch);
} else {
@@ -196,20 +145,20 @@ static void board_print_fen(struct pos const* pos, FILE* out)
fprintf(out, "\n");
}
static void board_print(const struct pos* pos, struct move* move, FILE* out)
static void board_print(struct pos const* pos, struct move* move, FILE* out, bool print_threats)
{
int buf[8][8] = {0};
int color[8][8] = {0};
for (enum player player = PLAYER_BEGIN; player < PLAYER_COUNT; ++player) {
for (enum piece piece = PIECE_BEGIN; piece < PIECE_COUNT; ++piece) {
bitboard x = pos->pieces[player][piece];
for (Side8 side = SIDE_BEGIN; side < SIDE_COUNT; ++side) {
for (Piece8 piece = PIECE_BEGIN; piece < PIECE_COUNT; ++piece) {
Bb64 x = pos->pieces[side][piece];
for (index i = 7; i < 8; i--) {
for (index j = 0; j < 8; ++j) {
for (Index8 i = 7; i < 8; i--) {
for (Index8 j = 0; j < 8; ++j) {
if (x & (1ULL<<(i*8+j))) {
buf[i][j] = piece_unicode[PLAYER_BLACK][piece];
color[i][j] = (player == PLAYER_WHITE)
buf[i][j] = piece_unicode[SIDE_BLACK][piece];
color[i][j] = (side == SIDE_WHITE)
? 1
: 2;
}
@@ -218,16 +167,19 @@ static void board_print(const struct pos* pos, struct move* move, FILE* out)
}
}
Bb64 const threats = all_threats_from_side(pos, pos->moving_side);
/* see: https://en.wikipedia.org/wiki/ANSI_escape_code#Colors */
setlocale(LC_ALL, "");
for (index i = 7; i < 8; i--) {
for (Index8 i = 7; i < 8; i--) {
fprintf(out, "\033[0m"); /* reset background color */
fprintf(out, "%"INDEX_FMT" ", i+1);
for (index j = 0; j < 8; ++j) {
index const n = INDEX_FROM_RF(i,j);
for (Index8 j = 0; j < 8; ++j) {
Index8 const n = SQ_FROM_RF(i,j);
int bg, fg;
/* foreground color */
/**/ if (color[i][j] == 1) {
fg = 97; /* bright white */
}
@@ -238,14 +190,20 @@ static void board_print(const struct pos* pos, struct move* move, FILE* out)
fg = 35; /* magenta (should not happen) */
}
if (move && n == move->from) {
bg = 104; /* blue */
} else if (move && n == move->to) {
/* background color */
/**/ if (move && (n == move->to || n == move->from)) {
bg = 44; /* bright blue */
}
else if (print_threats && (threats >> n) & 1) {
bg = 41;
} else {
/* 45: magenta,
* 47: white */
bg = (i+j) % 2 ? 45 : 47;
* 43: yellow */
bg = (i+j) % 2 ? 45 : 43;
}
if ((pos->occupied[SIDE_WHITE] | pos->occupied[SIDE_BLACK]) & MASK_FROM_SQ(n)) {
bg += 60; /* make bright */
}
fprintf(out, "\033[%d;%dm", fg, bg);
@@ -261,5 +219,9 @@ static void board_print(const struct pos* pos, struct move* move, FILE* out)
fprintf(out, "\n");
}
fprintf(out, " A B C D E F G H \n");
fprintf(out, "half moves: %d\n"
"full moves: %d\n",
pos->halfmoves,
pos->fullmoves);
}