mirror of
https://github.com/StewBC/cc65-Chess.git
synced 2026-11-24 18:23:54 +00:00
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>
90 lines
2.2 KiB
C
90 lines
2.2 KiB
C
/*
|
|
* selfplay.c
|
|
* cc65 Chess - test support
|
|
*
|
|
* Plays the engine against itself through cpu_Play, which is the same path
|
|
* the game uses, and reports result, nodes and time. This is the instrument
|
|
* Phase 4 tunes with.
|
|
*
|
|
* Both sides are deterministic, so every game at a given skill is the same
|
|
* game. Phase 4 needs opening variety before this can measure anything
|
|
* finer than "did the change break it".
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <time.h>
|
|
#include "types.h"
|
|
#include "globals.h"
|
|
#include "engine.h"
|
|
#include "undo.h"
|
|
#include "board.h"
|
|
#include "search.h"
|
|
#include "cpu.h"
|
|
#include "testutil.h"
|
|
|
|
int test_RunSelfPlay(int games, int maxPlies, int verbose)
|
|
{
|
|
int failures = 0, skill;
|
|
|
|
printf("selfplay: %d ply limit\n", maxPlies);
|
|
printf(" %5s %6s %9s %10s %s\n", "skill", "plies", "total(s)", "nodes/move", "result");
|
|
|
|
for(skill = 0; skill < SEARCH_NUM_SKILLS; ++skill)
|
|
{
|
|
char side = SIDE_WHITE, outcome = OUTCOME_OK, lastMover = SIDE_WHITE;
|
|
int ply;
|
|
unsigned long nodes = 0;
|
|
clock_t started;
|
|
const char *result = "unfinished";
|
|
t_searchResult probe;
|
|
|
|
board_Init();
|
|
undo_Init();
|
|
gSkillLevel = skill;
|
|
gUserMode = 0;
|
|
|
|
started = clock();
|
|
for(ply = 0; ply < maxPlies; ++ply)
|
|
{
|
|
lastMover = side;
|
|
|
|
// same search the turn will run, so the node count is honest
|
|
search_Best(side, gcSearchSkill[skill].m_depth,
|
|
gcSearchSkill[skill].m_nodes, &probe);
|
|
nodes += probe.m_nodes;
|
|
|
|
outcome = cpu_Play(side);
|
|
|
|
if(KING != (geBoard[geKing[0]] & PIECE_DATA) ||
|
|
KING != (geBoard[geKing[1]] & PIECE_DATA))
|
|
{
|
|
printf(" skill %d ply %d: king tracker broken\n", skill, ply);
|
|
++failures;
|
|
break;
|
|
}
|
|
|
|
if(outcome >= OUTCOME_CHECKMATE)
|
|
break;
|
|
side = 1 - side;
|
|
}
|
|
started = clock() - started;
|
|
|
|
switch(outcome)
|
|
{
|
|
case OUTCOME_CHECKMATE:
|
|
result = lastMover == SIDE_WHITE ? "black mates" : "white mates";
|
|
break;
|
|
case OUTCOME_STALEMATE: result = "stalemate"; break;
|
|
case OUTCOME_DRAW: result = "draw (50 move)"; break;
|
|
}
|
|
|
|
printf(" %5d %6d %9.2f %10lu %s\n", skill + 1, ply,
|
|
(double)started / CLOCKS_PER_SEC,
|
|
ply ? nodes / (unsigned long)ply : 0UL, result);
|
|
}
|
|
|
|
(void)games; (void)verbose;
|
|
printf(" -> %d failing\n", failures);
|
|
return failures;
|
|
}
|