Skip to content

Commit

Permalink
Add PGN::validate and descriptive exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
onspli committed Sep 17, 2021
1 parent 82a43c7 commit 863b31c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
20 changes: 20 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | |
Expand Down Expand Up @@ -1551,6 +1552,25 @@ PGN::export( ): string



---
### PGN::validate



```php
PGN::validate( ): void
```





**Return Value:**





---
### PGN::set_tag

Expand Down
15 changes: 14 additions & 1 deletion src/PGN.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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])) {
Expand Down Expand Up @@ -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();
}
}
Expand Down
32 changes: 31 additions & 1 deletion tests/unit/PGNTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -125,5 +156,4 @@ public function testHalfmoveOutOfBoundsTooSmall2() : void
$pgn->get_halfmove(1);
}


}

0 comments on commit 863b31c

Please sign in to comment.