Make engine dep-free/standalone and add Wasm target

This commit is contained in:
2025-12-19 04:01:32 +01:00
parent 509f58a379
commit 02b1ca4cfe
10 changed files with 1264 additions and 392 deletions

51
tests.c
View File

@@ -3,9 +3,11 @@
#include <unistd.h> /* usleep */
#include "base.h"
#include "board_print.h"
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
/* Tests are mostly generated by ChatGPT */
@@ -388,9 +390,7 @@ static void test_bishops(void)
int main()
{
printf("sizeof board: %zu\n", sizeof (struct board));
printf("sizeof pos: %zu\n", sizeof (struct pos));
printf("sizeof mailbox: %zu\n", sizeof (struct board){0}.mailbox);
printf("sizeof tt: %zu\n", sizeof (struct tt));
#if 1
@@ -402,49 +402,60 @@ int main()
fprintf(stdout, "\033[30;%dm ", i);
}
fprintf(stdout, "\033[0m\n"); /* reset background color */
struct board board = BOARD_INITIAL;
/* board is too big for the stack */
struct board* b = malloc(sizeof *b);
if (!b) {
abort();
}
*b = BOARD_INIT;
//board_load_fen_unsafe(&board, "1n1q1rk1/r1p2P2/1p1pp2p/pB2P3/2P5/PPN5/6b1/3QK1NR b - - 0 1");
//board_load_fen_unsafe(&board, "5R2/7k/P7/6pp/3B4/1PPK2bP/4r3/8 b - - 3 57");
board_print_fen(&board.pos, stdout);
board_print(&board.pos, NULL, stdout);
board_print_fen(&b->pos, stdout);
board_print(&b->pos, NULL, stdout);
struct move moves[MOVE_MAX];
for (int turn = 0; turn < 200; ++turn) {
size_t move_count = 0;
all_moves(&board.pos, board.pos.player, &move_count, moves);
all_moves(&b->pos, b->pos.player, &move_count, moves);
if (move_count == 0) {
printf("no moves for %s, aborting\n", player_str[board.pos.player]);
board_print_threats(&board.pos, stdout, NULL);
board.pos.player = opposite_player(board.pos.player);
board_print_threats(&board.pos, stdout, NULL);
printf("no moves for %s, aborting\n", player_str[b->pos.player]);
board_print_threats(&b->pos, stdout, NULL);
b->pos.player = opposite_player(b->pos.player);
board_print_threats(&b->pos, stdout, NULL);
break;
}
//struct move move = moves[0];
struct move move = search(&board, board.pos.player, 8);
struct search_result sr = search(b, b->pos.player, 7);
struct move move = sr.move;
double const score = sr.score;
printf("move %d: {\n"
" .from = %s, (%s)\n"
" .to = %s,\n"
" .score = %lf,\n"
" .mask = ",
turn,
square_index_display[move.from],
piece_str[board.mailbox[move.from]],
square_index_display[move.to]
piece_str[b->mailbox[move.from]],
square_index_display[move.to],
score
);
if (move.attr & MATTR_CAPTURE) printf("MATTR_CAPTURE ");
if (move.attr & MATTR_PROMOTE) printf("MATTR_PROMOTE ");
printf("\n}\n");
enum move_result const r = board_move_2(&board, move);
enum move_result const r = board_move_2(b, move);
#if 1
board_print_fen(&board.pos, stdout);
print_stats(stdout);
board_print(&board.pos, &move, stdout);
board_print_fen(&b->pos, stdout);
tt_print_stats(&b->tt, stdout);
board_print(&b->pos, &move, stdout);
#endif
if (r == MR_STALEMATE) {
@@ -452,11 +463,11 @@ int main()
break;
}
if (board.pos.pieces[PLAYER_WHITE][PIECE_KING] == 0ULL) {
if (b->pos.pieces[PLAYER_WHITE][PIECE_KING] == 0ULL) {
printf("white king gone!!\n");
exit(1);
}
if (board.pos.pieces[PLAYER_BLACK][PIECE_KING] == 0ULL) {
if (b->pos.pieces[PLAYER_BLACK][PIECE_KING] == 0ULL) {
printf("black king gone!!\n");
exit(1);
}