Files
Stefan WesselsandClaude Opus 5 d9d49f7f8e Replace the engine: 0x88 board, make/unmake, alpha-beta search
The old engine was weak for one structural reason. cpu_ScorePieceSubTree
looked like minimax but had a branching factor of one: at each level it
followed only the single reply a static heuristic ranked first, so it
evaluated one predicted line per root move and never saw a refutation the
heuristic missed. Two things compounded it - the root considered only one
move per piece, and the scoring functions returned move desirability rather
than position value, which is not a quantity minimax can validly accumulate
across plies.

What replaces it:

  engine.c  0x88 board, make/unmake against a per-ply state stack, legal
            move generation, and attack queries by ray-cast.  The stored
            attack database is gone - eng_IsAttacked walks outwards from a
            square instead of rebuilding 64 squares' worth of state after
            every trial move.
  eval.c    material and piece-square tables as a pure position value,
            kept as a running total updated incrementally by make/unmake.
  search.c  iterative-deepening negamax with alpha-beta, MVV-LVA and killer
            ordering, and a quiescence search over captures.

board.c becomes the adapter between engine and display; plat.h and the 0-63
tile numbering are untouched, so no platform file needed editing - including
the three that cannot be built here.

Verified: perft exact on all five reference positions to depth 5, and to
depth 6 on the two that publish one. Against the engine it replaces, at that
engine's strongest setting: 6 wins, 0 losses.

Smaller than what it replaces despite doing far more - 31129 bytes total on
the c64 at optspeed against 31543 for the two engines' worth of machinery it
supersedes, and 514 bytes less RAM. All five buildable targets fit.

tests/ carries the harness this was developed against: perft, a game fuzzer,
castling scenarios, an internal match runner, on-target C64 benchmarks, and
a UCI adapter for playing external engines. It stops being gitignored here -
the exclusion was scaffolding added during the rework to keep git status
quiet, and the harness is the evidence for everything above.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-07 12:36:05 -07:00

81 lines
1.8 KiB
C

/*
* c64perft.c
* cc65 Chess - test support
*
* The Phase 1 review gate. Runs perft on the new 0x88 core on a real C64 and
* reports the time in jiffies, which is emulated time and so is unaffected by
* running the emulator in warp mode.
*
* Perft is movegen + make + unmake + one attack test, which is the bulk of
* what a search node costs. So nodes/sec here is the number the per-skill
* node budgets get derived from.
*
* Build (not part of the game Makefile):
* cl65 -t c64 -Oris -I../src -o c64perft.prg ../src/engine.c c64perft.c
*/
#include <stdio.h>
#include <time.h>
#include "types.h"
#include "engine.h"
// depth 3 needs 3 plies of move list; one spare
#define PLIES 4
static t_engMove st_arena[PLIES][ENG_MAX_MOVES];
/*-----------------------------------------------------------------------*/
static unsigned long perft(char side, char depth, char ply)
{
t_engMove *moves = st_arena[ply];
t_engUndo undo;
char count, i;
unsigned long nodes = 0;
if(!depth)
return 1;
count = eng_GenMoves(side, moves);
for(i = 0; i < count; ++i)
{
eng_Make(&moves[i], &undo);
if(!eng_IsAttacked(geKing[side], 1 - side))
nodes += (depth == 1) ? 1 : perft(1 - side, depth - 1, ply + 1);
eng_Unmake(&moves[i], &undo);
}
return nodes;
}
/*-----------------------------------------------------------------------*/
int main(void)
{
char depth;
printf("cc65 chess 0x88 perft\n");
printf("clocks/sec %u\n\n", (unsigned)CLOCKS_PER_SEC);
for(depth = 1; depth <= 3; ++depth)
{
clock_t start, taken;
unsigned long nodes;
eng_SetStartPosition();
start = clock();
nodes = perft(SIDE_WHITE, depth, 0);
taken = clock() - start;
printf("d%u n=%lu t=%lu\n", (unsigned)depth, nodes, (unsigned long)taken);
}
printf("\ndone.\n");
// hold the screen so the exit screenshot has something on it
for(;;)
;
}