100,'n'=>300,'b'=>300,'r'=>500,'q'=>900,'k'=>0]; private const MATE=100000; private const THRESHOLD=100; private int $nodes=0; private int $deadline; public function __construct(private int $milliseconds=200,private int $maxNodes=8000) {} public function rate(array $before,array $after): array { $started=hrtime(true);$this->deadline=$started+max(0,$this->milliseconds)*1000000;$this->nodes=0; $out=['quality'=>null,'reason'=>'quiet','model'=>'tactical-estimate','ply'=>$after['ply']]; try { $this->budget(); $result=Chess::result($after); if ($result) { if ($result['winner']===$before['turn']) $out['quality']='good'; $out['reason']=$result['winner']===null?'draw':'checkmate'; } elseif (Chess::claimable($before)||Chess::claimable($after)) { // A move-only grade would omit the player's draw-claim option. $out['reason']='draw-claim'; } else { // Opponent's best reply, our answer, then up to two tactical continuation plies. $score=-$this->search($after,2,2,-self::MATE,self::MATE,0); $gain=$score-$this->material($before); if ($score<=-self::MATE+100) {$out['quality']='poor';$out['reason']='mate-threat';} elseif ($score>=self::MATE-100) {$out['quality']='good';$out['reason']='mate-threat';} elseif ($gain>=self::THRESHOLD) {$out['quality']='good';$out['reason']='material-gain';} elseif ($gain<=-self::THRESHOLD) {$out['quality']='poor';$out['reason']='material-loss';} $out['gain']=$gain; } } catch (FeedbackLimit $e) { $out['quality']=null;$out['reason']='inconclusive'; } $out['nodes']=$this->nodes;$out['elapsed_ms']=round((hrtime(true)-$started)/1000000,2); return $out; } private function budget(): void { if (++$this->nodes>$this->maxNodes || hrtime(true)>=$this->deadline) throw new FeedbackLimit(); } /** Material from the side-to-move perspective; one pawn is 100. */ private function material(array $s): int { $value=0; foreach ($s['board'] as $p) if ($p!=='.') $value+=(Chess::color($p)===$s['turn']?1:-1)*self::VALUES[strtolower($p)]; return $value; } private function search(array $s,int $depth,int $tactical,int $alpha,int $beta,int $ply,int $evasions=2): int { $this->budget(); $legal=Chess::legal($s);$check=Chess::check($s); if (!$legal) return $check?-self::MATE+$ply:0; if (Chess::dead($s)||$s['halfmove']>=150||Chess::repetitions($s)>=5) return 0; $claim=Chess::claimable($s); if ($depth===0 && $tactical===0) { if (!$check) return $claim?max(0,$this->material($s)):$this->material($s); // Give checks a little extra room; never pretend that staying in check is a legal option. if ($evasions===0) throw new FeedbackLimit(); $tactical=1;$evasions--; } $best=$claim?0:-self::MATE; if ($depth===0 && !$check) { // Quiet material baseline; follow captures/promotions to avoid praising an exchange halfway through. $best=max($best,$this->material($s)); if ($best>=$beta) return $best; $alpha=max($alpha,$best); $legal=array_values(array_filter($legal,fn($m)=>$s['board'][$m['to']]!=='.'||$m['promotion']!==null)); } usort($legal,fn($a,$b)=>$this->priority($s,$b)<=>$this->priority($s,$a)); foreach ($legal as $m) { $next=Chess::afterLegal($s,$m); $score=-$this->search($next,max(0,$depth-1),$depth>0?$tactical:$tactical-1,-$beta,-$alpha,$ply+1,$evasions); // The mover may also claim a draw using this intended move. if (Chess::claimable($next)) $score=max(0,$score); $best=max($best,$score);$alpha=max($alpha,$score); if ($alpha>=$beta) break; } return $best; } private function priority(array $s,array $m): int { $victim=$s['board'][$m['to']]; return ($m['promotion']?self::VALUES[$m['promotion']]:0)+($victim==='.'?0:10*self::VALUES[strtolower($victim)]-self::VALUES[strtolower($s['board'][$m['from']])]); } } final class FeedbackLimit extends RuntimeException {}