BlueGit

projects-timfalken-com microchess

Files

microchess / web / tests / chess.php

chess.php

chess.php 4.4 KiB Download Raw
1<?php
2declare(strict_types=1);
3require dirname(__DIR__).'/lib/Chess.php';
4$checks=0;
5function check(bool $condition,string $label): void {global $checks;$checks++;if(!$condition)throw new RuntimeException($label);}
6function 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;}
7function sq(string $s): int {return (5-(int)$s[1])*4+ord($s[0])-97;}
8function 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);}
9function rejects(callable $fn,string $label): void {try{$fn();}catch(DomainException){check(true,$label);return;}check(false,$label);}
10$s=Chess::initial();
11check(count($s['board'])===20,'20 squares');check($s['board'][15]==='P'&&$s['board'][4]==='p','Pawn start squares');
12check(!Chess::check($s),'Initial king safe');check(Chess::result($s)===null,'Initial position playable');
13check(has($s,'d2','d3'),'Single pawn step');check(!has($s,'d2','d4'),'No double step');
14check(!has($s,'a1','a5'),'Rook cannot pass pawn');
15check(has($s,'c1','b3'),'Knight jumps');
16$s=position(['d1'=>'K','a5'=>'k','b2'=>'P','a3'=>'n','c3'=>'b']);
17check(has($s,'b2','a3')&&has($s,'b2','c3'),'Pawn captures diagonally');
18check(!has($s,'b2','a2')&&!has($s,'b2','b1'),'Pawn cannot move sideways/backwards');
19$s=position(['a1'=>'K','d5'=>'k','b4'=>'P']);
20foreach(['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);}
21rejects(fn()=>Chess::move($s,sq('b4'),sq('b5')),'Promotion required');
22rejects(fn()=>Chess::move($s,sq('b4'),sq('b5'),'k'),'Cannot promote to king');
23$s=position(['a1'=>'K','d5'=>'k','b2'=>'p'],'b');check(has($s,'b2','b1','q'),'Black promotion');
24$s=position(['a1'=>'K','a2'=>'R','a5'=>'r','d5'=>'k']);
25check(!has($s,'a2','b2'),'Pinned rook cannot expose king');check(has($s,'a2','a5'),'Pinned rook can capture attacker');
26$s=position(['a1'=>'K','c2'=>'k']);check(!has($s,'a1','b1')&&!has($s,'a1','b2'),'Kings cannot touch');
27$s=position(['a1'=>'K','d5'=>'k','c2'=>'p']);check(!has($s,'a1','b1'),'King cannot enter pawn attack');
28$s=position(['a5'=>'k','b4'=>'Q','c3'=>'K'],'b');check(Chess::result($s)===['winner'=>'w','reason'=>'Checkmate'],'Checkmate');
29$s=position(['a5'=>'k','c4'=>'Q','c3'=>'K'],'b');check(Chess::result($s)===['winner'=>null,'reason'=>'Stalemate'],'Stalemate');
30foreach([['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');
31check(!Chess::dead(position(['a1'=>'K','d5'=>'k','b2'=>'B','b3'=>'b'])),'Opposite-color bishops not automatically dead');
32check(!Chess::dead(position(['a1'=>'K','d5'=>'k','b2'=>'N','b3'=>'N'])),'Two knights not automatically dead');
33$s=Chess::initial();$s['halfmove']=100;check(Chess::claimable($s),'50 moves claimable');check(Chess::result($s)===null,'50 moves not automatic');
34$s['halfmove']=150;check(Chess::result($s)['reason']==='75-move rule','75 moves automatic');
35$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');
36$s['history']=array_fill(0,5,Chess::key($s));check(Chess::result($s)['reason']==='Fivefold repetition','Fivefold automatic');
37$s=position(['a5'=>'k','b4'=>'Q','c3'=>'K'],'b');$s['halfmove']=150;check(Chess::result($s)['reason']==='Checkmate','Mate overrides 75 moves');
38$s=Chess::initial();$s['halfmove']=99;$s=Chess::move($s,15,11);check($s['halfmove']===0,'Pawn resets halfmove');
39rejects(fn()=>Chess::move(Chess::initial(),4,8),'Cannot move opponent piece');
40// Exercise complete random games: no legal move may leave its own king in check.
41mt_srand(24);
42for($game=0;$game<15;$game++){
43 $s=Chess::initial();
44 for($ply=0;$ply<120;$ply++){
45 if(Chess::result($s))break;
46 $moves=Chess::legal($s);$m=$moves[mt_rand(0,count($moves)-1)];$color=$s['turn'];
47 $s=Chess::move($s,$m['from'],$m['to'],$m['promotion']);
48 check(!Chess::check($s,$color),'Legal move protects own king');
49 check(count(array_filter($s['board'],fn($p)=>strtolower($p)==='k'))===2,'Kings never captured');
50 }
51}
52echo "Chess: $checks assertions passed.\n";