| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | require dirname(__DIR__).'/lib/HexGrid.php'; |
| 4 | require dirname(__DIR__).'/lib/SynhexSim.php'; |
| 5 | require dirname(__DIR__).'/lib/SynhexMatch.php'; |
| 6 | $checks=0; |
| 7 | function check(bool $ok,string $label): void {global $checks;$checks++;if(!$ok)throw new RuntimeException($label);} |
| 8 | |
| 9 | $cfg=SynhexMatch::config(); |
| 10 | check($cfg['grid_cols']===17 && $cfg['grid_rows']===14,'Documented 17×14 odd-r board'); |
| 11 | check($cfg['walk_range']===5 && $cfg['act_duration_ms']===3000,'Walk 5 hexes / 3s act'); |
| 12 | check($cfg['base_accuracy']===15,'Base accuracy 15%'); |
| 13 | check($cfg['weapon_shotgun_pellets']>=2,'Shotgun fires multiple pellets'); |
| 14 | check($cfg['vision_cone_half_deg']===60,'120° cone (half-angle 60)'); |
| 15 | check(count($cfg['spawn']['w'])===4 && count($cfg['spawn']['b'])===4,'Four units per side'); |
| 16 | check($cfg['spawn']['w'][0]['role']==='vip' && $cfg['spawn']['w'][0]['weapon']==='pistol','VIP pistol'); |
| 17 | $weapons=array_column($cfg['spawn']['w'],'weapon'); |
| 18 | check($weapons===['pistol','smg','smg','shotgun'],'VIP + 2 SMG + shotgun'); |
| 19 | |
| 20 | check(HexGrid::distance(0,0,1,0)===1,'East neighbor is 1'); |
| 21 | check(HexGrid::distance(0,0,-1,0)===1,'West neighbor is 1'); |
| 22 | [$q,$r]=HexGrid::axial(3,4);[$c,$row]=HexGrid::offset($q,$r); |
| 23 | check($c===3 && $row===4,'Offset/axial round-trip'); |
| 24 | check(HexGrid::inCone(0,0,0,2,0,60),'Forward hex is in cone'); |
| 25 | check(!HexGrid::inCone(0,0,0,-2,0,60),'Rear hex is outside cone'); |
| 26 | $walls=[HexGrid::key(1,0)=>true]; |
| 27 | check(!HexGrid::los(0,0,2,0,$walls),'Wall blocks LoS'); |
| 28 | check(HexGrid::los(0,0,0,1,[]),'Open LoS'); |
| 29 | $walk=HexGrid::allHexes(8,6); |
| 30 | unset($walk[HexGrid::key(...HexGrid::axial(3,2))]); |
| 31 | [$q1,$r1]=HexGrid::axial(0,2);[$q2,$r2]=HexGrid::axial(6,2); |
| 32 | $path=HexGrid::path($q1,$r1,$q2,$r2,$walk); |
| 33 | check($path!==null && count($path)>2,'A* walks around a wall'); |
| 34 | |
| 35 | $s=SynhexMatch::initial(24,$cfg); |
| 36 | check(count($s['units'])===8,'Eight operatives'); |
| 37 | check($s['grid']['cols']===17 && $s['phase']==='planning','Planning on configured grid'); |
| 38 | check(count(array_filter($s['units'],fn($u)=>$u['role']==='vip'))===2,'Two VIPs'); |
| 39 | check(SynhexMatch::isYourTurn($s,'w') && SynhexMatch::isYourTurn($s,'b'),'Both plan at once'); |
| 40 | foreach ($s['walls'] as $key) check(is_string($key) && str_contains($key,','),'Walls stored as q,r keys'); |
| 41 | |
| 42 | $s=SynhexMatch::commit($s,'w',['units'=>[]],$cfg); |
| 43 | check(!SynhexMatch::isYourTurn($s,'w') && SynhexMatch::isYourTurn($s,'b'),'First lock waits for opponent'); |
| 44 | $s=SynhexMatch::commit($s,'b',['units'=>[]],$cfg); |
| 45 | check(($s['phase']??'')==='playback','Both locks run the act'); |
| 46 | check((int)$s['timeline']['durationMs']===3000,'Playback lasts one act'); |
| 47 | check(isset($s['timeline']['snapshots'][0]['units']),'Timeline has snapshots'); |
| 48 | $again=SynhexMatch::initial(24,$cfg); |
| 49 | $again=SynhexMatch::commit($again,'w',['units'=>[]],$cfg); |
| 50 | $again=SynhexMatch::commit($again,'b',['units'=>[]],$cfg); |
| 51 | check($again['timeline']['shots']===$s['timeline']['shots'],'Same seed is deterministic'); |
| 52 | |
| 53 | $s=SynhexMatch::ackPlayback($s,'w'); |
| 54 | check(!SynhexMatch::isYourTurn($s,'w') && SynhexMatch::isYourTurn($s,'b'),'Playback ack waits for opponent'); |
| 55 | $s=SynhexMatch::ackPlayback($s,'b'); |
| 56 | check($s['phase']==='planning' || $s['phase']==='ended','After both Play, next plan or ended'); |
| 57 | |
| 58 | $tiny=$cfg; |
| 59 | $tiny['grid_cols']=7;$tiny['grid_rows']=5;$tiny['wall_density']=0;$tiny['wall_gen_attempts']=1; |
| 60 | $tiny['spawn']=['w'=>[['id'=>'vip','role'=>'vip','weapon'=>'pistol','col'=>1,'row'=>2,'facing'=>0]],'b'=>[['id'=>'vip','role'=>'vip','weapon'=>'pistol','col'=>5,'row'=>2,'facing'=>3]]]; |
| 61 | $tiny['base_accuracy']=100;$tiny['accuracy_cap']=100;$tiny['shot_range']=10;$tiny['vision_range']=10; |
| 62 | $duel=SynhexMatch::initial(7,$tiny); |
| 63 | $duel=SynhexMatch::commit($duel,'w',['units'=>[]],$tiny); |
| 64 | $duel=SynhexMatch::commit($duel,'b',['units'=>[]],$tiny); |
| 65 | check($duel['timeline']['shots']!==[],'Facing VIPs fire when accuracy is certain'); |
| 66 | check($duel['result']!==null,'VIP death produces a result after the act'); |
| 67 | |
| 68 | $shotCfg=$cfg;$shotCfg['weapon_shotgun_pellets']=5; |
| 69 | $st=SynhexMatch::initial(3,$shotCfg); |
| 70 | $shot=null; |
| 71 | foreach ($st['units'] as $u) if ($u['weapon']==='shotgun') $shot=$u; |
| 72 | check($shot!==null,'Shotgun unit exists'); |
| 73 | $alive=array_values(array_filter($st['units'],fn($u)=>$u['team']!==$shot['team'])); |
| 74 | $target=$alive[0]; |
| 75 | $aims=new ReflectionClass(SynhexSim::class); |
| 76 | $m=$aims->getMethod('pelletAims'); |
| 77 | $m->setAccessible(true); |
| 78 | $points=$m->invoke(null,$shot,$target,5,(int)$shotCfg['weapon_shotgun_spread']); |
| 79 | check(count($points)===5,'Shotgun volley has five rays'); |
| 80 | |
| 81 | $bad=SynhexMatch::initial(1,$cfg); |
| 82 | try {SynhexMatch::sanitizePlan($bad,'w',['units'=>['w-vip'=>['path'=>[['q'=>99,'r'=>99]]]]],$cfg);check(false,'Rejects illegal path');} |
| 83 | catch (DomainException) {check(true,'Rejects illegal path');} |
| 84 | |
| 85 | $view=SynhexMatch::clientView($st,'w',$cfg); |
| 86 | check(isset($view['grid'],$view['units'],$view['rules']['walkRange']),'Client view exposes board + tunables'); |
| 87 | $own=array_filter($view['units'],fn($u)=>$u['team']==='w' && empty($u['hologram'])); |
| 88 | check(count($own)===4,'Viewer always sees own units'); |
| 89 | |
| 90 | echo "Synhex: $checks assertions passed.\n"; |
projects-timfalken-com microchess