<?php
declare(strict_types=1);
require dirname(__DIR__).'/lib/Chess.php';
$checks=0;
function check(bool $condition,string $label): void {global $checks;$checks++;if(!$condition)throw new RuntimeException($label);}
function position(array $pieces,string $turn='w'): array {$s=Chess::initial();$s['board']=array_fill(0,20,'.');foreach($pieces as $sq=>$p){$i=(5-(int)$sq[1])*4+ord($sq[0])-97;$s['board'][$i]=$p;}$s['turn']=$turn;$s['history']=[Chess::key($s)];return $s;}
function sq(string $s): int {return (5-(int)$s[1])*4+ord($s[0])-97;}
function has(array $s,string $from,string $to,?string $p=null): bool {return in_array(['from'=>sq($from),'to'=>sq($to),'promotion'=>$p],Chess::legal($s),true);}
function rejects(callable $fn,string $label): void {try{$fn();}catch(DomainException){check(true,$label);return;}check(false,$label);}
$s=Chess::initial();
check(count($s['board'])===20,'20 squares');check($s['board'][15]==='P'&&$s['board'][4]==='p','Pawn start squares');
check(!Chess::check($s),'Initial king safe');check(Chess::result($s)===null,'Initial position playable');
check(has($s,'d2','d3'),'Single pawn step');check(!has($s,'d2','d4'),'No double step');
check(!has($s,'a1','a5'),'Rook cannot pass pawn');
check(has($s,'c1','b3'),'Knight jumps');
$s=position(['d1'=>'K','a5'=>'k','b2'=>'P','a3'=>'n','c3'=>'b']);
check(has($s,'b2','a3')&&has($s,'b2','c3'),'Pawn captures diagonally');
check(!has($s,'b2','a2')&&!has($s,'b2','b1'),'Pawn cannot move sideways/backwards');
$s=position(['a1'=>'K','d5'=>'k','b4'=>'P']);
foreach(['q','r','b','n']as$p){check(has($s,'b4','b5',$p),'Promotion '.$p);$next=Chess::move($s,sq('b4'),sq('b5'),$p);check($next['board'][sq('b5')]===strtoupper($p),'Promoted piece '.$p);}
rejects(fn()=>Chess::move($s,sq('b4'),sq('b5')),'Promotion required');
rejects(fn()=>Chess::move($s,sq('b4'),sq('b5'),'k'),'Cannot promote to king');
$s=position(['a1'=>'K','d5'=>'k','b2'=>'p'],'b');check(has($s,'b2','b1','q'),'Black promotion');
$s=position(['a1'=>'K','a2'=>'R','a5'=>'r','d5'=>'k']);
check(!has($s,'a2','b2'),'Pinned rook cannot expose king');check(has($s,'a2','a5'),'Pinned rook can capture attacker');
$s=position(['a1'=>'K','c2'=>'k']);check(!has($s,'a1','b1')&&!has($s,'a1','b2'),'Kings cannot touch');
$s=position(['a1'=>'K','d5'=>'k','c2'=>'p']);check(!has($s,'a1','b1'),'King cannot enter pawn attack');
$s=position(['a5'=>'k','b4'=>'Q','c3'=>'K'],'b');check(Chess::result($s)===['winner'=>'w','reason'=>'Checkmate'],'Checkmate');
$s=position(['a5'=>'k','c4'=>'Q','c3'=>'K'],'b');check(Chess::result($s)===['winner'=>null,'reason'=>'Stalemate'],'Stalemate');
foreach([['a1'=>'K','d5'=>'k'],['a1'=>'K','d5'=>'k','b2'=>'B'],['a1'=>'K','d5'=>'k','b2'=>'N'],['a1'=>'K','d5'=>'k','b2'=>'B','c3'=>'b']] as $pieces)check(Chess::dead(position($pieces)),'Insufficient mating material');
check(!Chess::dead(position(['a1'=>'K','d5'=>'k','b2'=>'B','b3'=>'b'])),'Opposite-color bishops not automatically dead');
check(!Chess::dead(position(['a1'=>'K','d5'=>'k','b2'=>'N','b3'=>'N'])),'Two knights not automatically dead');
$s=Chess::initial();$s['halfmove']=100;check(Chess::claimable($s),'50 moves claimable');check(Chess::result($s)===null,'50 moves not automatic');
$s['halfmove']=150;check(Chess::result($s)['reason']==='75-move rule','75 moves automatic');
$s=Chess::initial();$s['history']=array_fill(0,3,Chess::key($s));check(Chess::claimable($s),'Threefold claimable');check(Chess::result($s)===null,'Threefold not automatic');
$s['history']=array_fill(0,5,Chess::key($s));check(Chess::result($s)['reason']==='Fivefold repetition','Fivefold automatic');
$s=position(['a5'=>'k','b4'=>'Q','c3'=>'K'],'b');$s['halfmove']=150;check(Chess::result($s)['reason']==='Checkmate','Mate overrides 75 moves');
$s=Chess::initial();$s['halfmove']=99;$s=Chess::move($s,15,11);check($s['halfmove']===0,'Pawn resets halfmove');
rejects(fn()=>Chess::move(Chess::initial(),4,8),'Cannot move opponent piece');
// Exercise complete random games: no legal move may leave its own king in check.
mt_srand(24);
for($game=0;$game<15;$game++){
 $s=Chess::initial();
 for($ply=0;$ply<120;$ply++){
  if(Chess::result($s))break;
  $moves=Chess::legal($s);$m=$moves[mt_rand(0,count($moves)-1)];$color=$s['turn'];
  $s=Chess::move($s,$m['from'],$m['to'],$m['promotion']);
  check(!Chess::check($s,$color),'Legal move protects own king');
  check(count(array_filter($s['board'],fn($p)=>strtolower($p)==='k'))===2,'Kings never captured');
 }
}
echo "Chess: $checks assertions passed.\n";
