diff --git a/docs/README.md b/docs/README.md index 69c9242..4a03ca8 100644 --- a/docs/README.md +++ b/docs/README.md @@ -69,6 +69,7 @@ | [**PGN**](#PGN) | | | [PGN::__construct](#PGN__construct) | | | [PGN::export](#PGNexport) | | +| [PGN::validate](#PGNvalidate) | | | [PGN::set_tag](#PGNset_tag) | | | [PGN::set_initial_fen](#PGNset_initial_fen) | | | [PGN::unset_initial_fen](#PGNunset_initial_fen) | | @@ -1551,6 +1552,25 @@ PGN::export( ): string +--- +### PGN::validate + + + +```php +PGN::validate( ): void +``` + + + + + +**Return Value:** + + + + + --- ### PGN::set_tag diff --git a/src/PGN.php b/src/PGN.php index a0bf1ed..18a1be0 100644 --- a/src/PGN.php +++ b/src/PGN.php @@ -40,6 +40,11 @@ public function export() : string return trim($pgn); } + public function validate() : void + { + $this->get_current_fen(); + } + public function set_tag(string $name, ?string $value) : void { if ($value !== null) { @@ -111,6 +116,9 @@ public function get_current_fen(bool $as_object = false) public function get_fen_after_halfmove(int $halfmove_number, bool $as_object = false) { + if ($halfmove_number == 0) { + return $this->get_initial_fen($as_object); + } $halfmove_index = $this->get_halfmove_index($halfmove_number); $this->compute_fens($halfmove_number); if (!isset($this->fens[$halfmove_index])) { @@ -154,7 +162,12 @@ private function compute_fens(int $max_halfmove_to_compute) : void } $fen = $this->get_last_computed_fen(); for ($halfmove_number = sizeof($this->fens) + $this->get_initial_halfmove_number(); $halfmove_number <= $max_halfmove_to_compute; $halfmove_number ++) { - $fen->move($this->get_halfmove($halfmove_number)); + $halfmove = $this->get_halfmove($halfmove_number); + try { + $fen->move($halfmove); + } catch (\Exception $e) { + throw new \Exception('Move ' . ceil($halfmove_number / 2) . ($halfmove_number % 2 ? '. ' : '... ') . $halfmove . ' is invalid. FEN ' . $this->get_fen_after_halfmove($halfmove_number - 1), 0, $e); + } $this->fens[] = $fen->copy(); } } diff --git a/tests/unit/PGNTest.php b/tests/unit/PGNTest.php index 7c195e4..4902d63 100644 --- a/tests/unit/PGNTest.php +++ b/tests/unit/PGNTest.php @@ -8,6 +8,37 @@ final class PGNTest extends TestCase { +/* + public function testBugs() : void + { + $pgn = new PGN('[Event "Live Chess"] +[Site "Chess.com"] +[Date "2021.01.28"] +[Round "?"] +[White "djkvetak"] +[Black "Nirrmall"] +[Result "1-0"] +[ECO "B01"] +[WhiteElo "1126"] +[BlackElo "1092"] +[TimeControl "3600"] +[EndTime "7:02:24 PST"] +[Termination "djkvetak won by resignation"] + +1. e4 d5 2. exd5 Qxd5 3. c4 Qd8 4. d4 h6 5. Qh5 Nf6 6. Qf3 Qxd4 7. Nc3 Ng4 8. +Be3 Nxe3 9. fxe3 Qd8 10. Qg3 e6 11. Nf3 g6 12. a3 Bd6 13. Ne5 Qe7 14. Nb5 Qf6 +15. O-O-O Bxe5 16. Nxc7+ Bxc7 17. Qxc7 O-O 18. Qd6 Rd8 19. Qxd8+ Qxd8 20. Rxd8+ +Kg7 21. Rxc8 Kf6 22. g3 Ke7 23. Bg2 a5 24. Bxb7 Ra7 25. Rxb8 Kd7 26. Bc8+ Kc7 +27. Rb7+ Rxb7 28. Bxb7 Kxb7 29. Rd1 Kb6 30. b4 a4 31. Kb1 f5 32. Rc1 Kc6 33. h3 +h5 34. Re1 g5 35. h4 g4 36. e4 f4 37. gxf4 Kd6 38. f5 exf5 39. exf5 g3 40. Rg1 +Ke5 41. Rxg3 Kxf5 42. b5 Kf4 43. Rg4+ Kxg4 44. b6 Kxh4 45. b7 Kg5 46. b8=Q h4 +47. c5 h3 48. Qb5 Kg4 49. Qc4+ Kg3 50. Qe4 h2 51. c6 Kf2 52. c7 Kg1 53. c8=Q +h1=Q 54. Qc1+ Kh2 55. Qcxh1+ Kg3 1-0'); + $pgn->validate(); + } + */ + + protected $samples = [ '[Event "F/S Return Match"] [Site "Belgrade, Serbia JUG"] @@ -125,5 +156,4 @@ public function testHalfmoveOutOfBoundsTooSmall2() : void $pgn->get_halfmove(1); } - }