BlueGit

projects-timfalken-com microchess

Files

microchess / web / tests / feedback.php

feedback.php

effects
timfa Tim Falken · pushed by timfa · 2 months ago
d82deb2f History
feedback.php 3.5 KiB Download Raw
1<?php
2declare(strict_types=1);
3require dirname(__DIR__).'/lib/MoveFeedback.php';
4$checks=0;
5function verify(bool $ok,string $label): void {global $checks;$checks++;if(!$ok)throw new RuntimeException($label);}
6function setup(array $pieces,string $turn='w'): array {
7 $s=Chess::initial();$s['board']=array_fill(0,20,'.');foreach($pieces as $i=>$p)$s['board'][$i]=$p;
8 $s['turn']=$turn;$s['history']=[Chess::key($s)];return $s;
9}
10$cases=[
11 'quiet opening'=>[Chess::initial(),15,11,null,null],
12 'free queen'=>[setup([19=>'K',3=>'k',12=>'R',8=>'q']),12,8,null,'good'],
13 'poisoned pawn'=>[setup([19=>'K',1=>'k',12=>'Q',8=>'p',0=>'r']),12,8,null,'poor'],
14 'equal rook exchange'=>[setup([19=>'K',3=>'k',12=>'R',8=>'r',0=>'r']),12,8,null,null],
15 'checkmate'=>[setup([10=>'K',0=>'k',6=>'Q']),6,5,null,'good'],
16 'allows mate in one'=>[setup([16=>'K',10=>'k',14=>'q',3=>'R']),3,7,null,'poor'],
17 'promotion'=>[setup([16=>'K',3=>'k',5=>'P']),5,1,'q','good'],
18 'underpromotion draw'=>[setup([16=>'K',3=>'k',5=>'P']),5,1,'n',null],
19];
20foreach($cases as $name=>[$s,$from,$to,$promotion,$quality]){
21 verify(!Chess::check($s,Chess::opposite($s['turn'])),"Legal fixture: $name");
22 $next=Chess::move($s,$from,$to,$promotion);$result=(new MoveFeedback())->rate($s,$next);
23 echo "$name: ".json_encode($result)."\n";
24 verify($result['quality']===$quality,"$name quality");
25 verify($result['elapsed_ms']<500,"$name stays comfortably inside feedback window");
26 verify(Chess::afterLegal($s,$next['last'])===$next,"Shared transition: $name");
27 // Same tactical verdict with colors swapped and board rotated.
28 $mirror=$s;$mirror['board']=array_map(fn($p)=>$p==='.'?'.':(ctype_upper($p)?strtolower($p):strtoupper($p)),array_reverse($s['board']));$mirror['turn']='b';$mirror['history']=[Chess::key($mirror)];
29 $next=Chess::move($mirror,19-$from,19-$to,$promotion);
30 verify((new MoveFeedback())->rate($mirror,$next)['quality']===$quality,"Black perspective: $name");
31}
32$s=Chess::initial();$next=Chess::move($s,15,11);
33foreach([new MoveFeedback(0),new MoveFeedback(200,1)] as $engine){$r=$engine->rate($s,$next);verify($r['quality']===null&&$r['reason']==='inconclusive','Budget exhaustion is silent, not a false verdict');}
34$s=setup([19=>'K',3=>'k',12=>'R',8=>'q']);$s['halfmove']=100;
35verify((new MoveFeedback())->rate($s,Chess::move($s,12,8))['quality']===null,'Existing draw claim suppresses move-only grading');
36$s=setup([10=>'K',0=>'k',6=>'Q']);$s['halfmove']=149;
37verify((new MoveFeedback())->rate($s,Chess::move($s,6,5))['quality']==='good','Mate takes precedence over automatic move-count draw');
38$s=setup([10=>'K',0=>'k',6=>'Q']);$next=Chess::move($s,6,7);$next['history']=array_fill(0,5,Chess::key($next));
39verify((new MoveFeedback())->rate($s,$next)['reason']==='draw','Fivefold repetition respected');
40// Small reproducible sample exercises realistic positions without any exhaustive search.
41mt_srand(17);$s=Chess::initial();$worst=0;$total=0;$silent=0;
42for($i=0;$i<30;$i++){
43 if(Chess::result($s))$s=Chess::initial();$moves=Chess::legal($s);$m=$moves[mt_rand(0,count($moves)-1)];$next=Chess::afterLegal($s,$m);
44 $r=(new MoveFeedback())->rate($s,$next);$worst=max($worst,$r['elapsed_ms']);$total+=$r['elapsed_ms'];$silent+=$r['reason']==='inconclusive'?1:0;$s=$next;
45 verify(in_array($r['quality'],[null,'good','poor'],true),'Only supported pulse values');
46}
47echo "$checks feedback checks passed. Sample: mean ".round($total/30,2)." ms, max $worst ms, $silent budget/horizon abstentions / 30 moves.\n";