Skip to content

Commit

Permalink
Update docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
onspli committed Sep 17, 2021
1 parent 5d558a1 commit 5d10b7e
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 54 deletions.
137 changes: 97 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Setup chess board to starting position and read FEN fields.
$fen = new Onspli\Chess\FEN;
echo($fen->export());
// rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
echo($fen->export_short());
// rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -
echo($fen->get_board());
// rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
echo($fen->get_active_color());
Expand Down Expand Up @@ -68,6 +70,8 @@ Initialize custom position and read FEN fields.
$fen = new Onspli\Chess\FEN('rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR b KQq c6 1 2');
echo($fen->export());
// rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR b KQq c6 1 2
echo($fen->export_short());
// rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR b KQq c6
echo($fen->get_board());
// rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR
echo($fen->get_active_color());
Expand All @@ -93,12 +97,31 @@ RNBQKBNR
*/
```

Manipulate with pieces.
``` php
$fen = new Onspli\Chess\FEN;
echo($fen->get_square('a1'));
// R
$fen->set_square('a1', '');
$fen->set_square('a3', 'R');
echo($fen->preview());
/*
rnbqkbnr
pppppppp
........
........
........
R.......
PPPPPPPP
.NBQKBNR
*/
```

Each of the fields can be set with the corresponding setter:
``` php
$fen = new Onspli\Chess\FEN;
echo($fen->export());
// rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1

$fen->set_board('rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR');
$fen->set_active_color('b');
$fen->set_castling_string('KQq');
Expand Down Expand Up @@ -162,6 +185,10 @@ Test check, mate, stalemate:
$fen = new Onspli\Chess\FEN;
$fen->set_active_color('w');
$fen->set_board('1q5k/8/8/8/8/8/8/K7');
echo($fen->is_check() ? 'true' : 'false');
// false
echo($fen->is_stalemate() ? 'true' : 'false');
// false
echo($fen->preview());
/*
.q.....k
Expand All @@ -173,12 +200,12 @@ echo($fen->preview());
........
K.......
*/
echo($fen->is_check() ? 'true' : 'false');
// false
echo($fen->is_stalemate() ? 'true' : 'false');
// false
$fen->move('Ka2');
$fen->move('Qa8');
echo($fen->is_check() ? 'true' : 'false');
// true
echo($fen->is_mate() ? 'true' : 'false');
// false
echo($fen->preview());
/*
q......k
Expand All @@ -190,10 +217,6 @@ q......k
K.......
........
*/
echo($fen->is_check() ? 'true' : 'false');
// true
echo($fen->is_mate() ? 'true' : 'false');
// false
```

List all possible moves:
Expand Down Expand Up @@ -251,11 +274,68 @@ Array
*/
```

Load game in PGN notation:
Load game in PGN notation and read tags and moves:
``` php
$pgn = new Onspli\Chess\PGN('[Event "Testing"] 1.Nf3 Nf6 2.c4 g6');
echo($pgn->get_tag('Event'));
// Testing
echo($pgn->get_halfmove(2));
// Nf6
echo($pgn->get_initial_halfmove_number());
// 1
echo($pgn->get_last_halfmove_number());
// 4
```

Record new moves, add tags and export PGN:
``` php
$pgn = new Onspli\Chess\PGN('[Event "Testing"] 1.Nf3 Nf6 2.c4 g6');
$pgn->set_tag('Site', 'Github');
$pgn->move('a4');
$pgn->move('a5');
print_r($pgn->get_tags());
/*
Array
(
[Event] => Testing
[Site] => Github
)

*/
echo($pgn->export_tags());
/*
[Event "Testing"]
[Site "Github"]

*/
echo($pgn->export_movetext());
// 1. Nf3 Nf6 2. c4 g6 3. a4 a5
echo($pgn->export());
/*
[Event "Testing"]
[Site "Github"]
1. Nf3 Nf6 2. c4 g6 3. a4 a5
*/
```

Extract position after certain move:
``` php
$pgn = new Onspli\Chess\PGN('1.Nf3 Nf6 2.c4 g6');
echo($pgn->get_current_fen());
// rnbqkb1r/pppppp1p/5np1/8/2P5/5N2/PP1PPPPP/RNBQKB1R w KQkq - 0 3
echo($pgn->get_initial_fen());
// rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
echo($pgn->get_fen_after_halfmove(0));
// rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
echo($pgn->get_fen_after_halfmove(2));
// rnbqkb1r/pppppppp/5n2/8/8/5N2/PPPPPPPP/RNBQKB1R w KQkq - 2 2
echo($pgn->get_fen_after_halfmove(Onspli\Chess\PGN::get_halfmove_number(1, 'b')));
// rnbqkb1r/pppppppp/5n2/8/8/5N2/PPPPPPPP/RNBQKB1R w KQkq - 2 2
```

FEN is returned as `php string` by default. Passing parameter `php $as_object = true` makes it FEN object:
``` php
$pgn = new Onspli\Chess\PGN('1.Nf3 Nf6 2.c4 g6');
echo($pgn->get_current_fen(true)->preview());
/*
rnbqkb.r
Expand All @@ -278,36 +358,13 @@ pppppppp
PPPPPPPP
RNBQKB.R
*/
```

echo($pgn->get_halfmove(Onspli\Chess\PGN::get_halfmove_number(2, 'w')));
// c4
echo($pgn->get_halfmove(3));
// c4
echo($pgn->get_last_halfmove_number());
// 4

echo($pgn->get_fen_after_halfmove(2));
// rnbqkb1r/pppppppp/5n2/8/8/5N2/PPPPPPPP/RNBQKB1R w KQkq - 2 2
PGN with custom initial position:
``` php
$pgn = new Onspli\Chess\PGN('[FEN "rnbqkb1r/pppp1ppp/5n2/4p3/2P1P3/5N2/PP1P1PPP/RNBQKB1R b KQkq - 0 3"] 3... Nc6 4. Qb3');
echo($pgn->get_initial_fen());
// rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1

$pgn->move('a4');
$pgn->set_tag('Site', 'Github');
echo($pgn->export());
/*
[Event "Testing"]
[Site "Github"]
1. Nf3 Nf6 2. c4 g6 3. a4
*/

for ($hm = $pgn->get_initial_halfmove_number() - 1; $hm <= $pgn->get_last_halfmove_number(); $hm++) echo $pgn->get_fen_after_halfmove($hm) . PHP_EOL;
/*
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
rnbqkbnr/pppppppp/8/8/8/5N2/PPPPPPPP/RNBQKB1R b KQkq - 1 1
rnbqkb1r/pppppppp/5n2/8/8/5N2/PPPPPPPP/RNBQKB1R w KQkq - 2 2
rnbqkb1r/pppppppp/5n2/8/2P5/5N2/PP1PPPPP/RNBQKB1R b KQkq c3 0 2
rnbqkb1r/pppppp1p/5np1/8/2P5/5N2/PP1PPPPP/RNBQKB1R w KQkq - 0 3
rnbqkb1r/pppppp1p/5np1/8/P1P5/5N2/1P1PPPPP/RNBQKB1R b KQkq a3 0 3

*/
// rnbqkb1r/pppp1ppp/5n2/4p3/2P1P3/5N2/PP1P1PPP/RNBQKB1R b KQkq - 0 3
echo($pgn->get_initial_halfmove_number());
// 6
```
62 changes: 48 additions & 14 deletions README.template.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Setup chess board to starting position and read FEN fields.
``` php
$fen = new Onspli\Chess\FEN;
echo($fen->export());
echo($fen->export_short());
echo($fen->get_board());
echo($fen->get_active_color());
echo($fen->get_castling_string());
Expand All @@ -50,6 +51,7 @@ Initialize custom position and read FEN fields.
``` php
$fen = new Onspli\Chess\FEN('rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR b KQq c6 1 2');
echo($fen->export());
echo($fen->export_short());
echo($fen->get_board());
echo($fen->get_active_color());
echo($fen->get_castling_string());
Expand All @@ -59,11 +61,19 @@ echo($fen->get_fullmove());
echo($fen->preview());
```

Manipulate with pieces.
``` php
$fen = new Onspli\Chess\FEN;
echo($fen->get_square('a1'));
$fen->set_square('a1', '');
$fen->set_square('a3', 'R');
echo($fen->preview());
```

Each of the fields can be set with the corresponding setter:
``` php
$fen = new Onspli\Chess\FEN;
echo($fen->export());

$fen->set_board('rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR');
$fen->set_active_color('b');
$fen->set_castling_string('KQq');
Expand Down Expand Up @@ -95,14 +105,14 @@ Test check, mate, stalemate:
$fen = new Onspli\Chess\FEN;
$fen->set_active_color('w');
$fen->set_board('1q5k/8/8/8/8/8/8/K7');
echo($fen->preview());
echo($fen->is_check() ? 'true' : 'false');
echo($fen->is_stalemate() ? 'true' : 'false');
echo($fen->preview());
$fen->move('Ka2');
$fen->move('Qa8');
echo($fen->preview());
echo($fen->is_check() ? 'true' : 'false');
echo($fen->is_mate() ? 'true' : 'false');
echo($fen->preview());
```

List all possible moves:
Expand All @@ -114,23 +124,47 @@ echo($fen->preview());
print_r($fen->get_legal_moves());
```

Load game in PGN notation:
Load game in PGN notation and read tags and moves:
``` php
$pgn = new Onspli\Chess\PGN('[Event "Testing"] 1.Nf3 Nf6 2.c4 g6');
echo($pgn->get_tag('Event'));
echo($pgn->get_current_fen(true)->preview());
echo($pgn->get_fen_after_halfmove(2, true)->preview());

echo($pgn->get_halfmove(Onspli\Chess\PGN::get_halfmove_number(2, 'w')));
echo($pgn->get_halfmove(3));
echo($pgn->get_halfmove(2));
echo($pgn->get_initial_halfmove_number());
echo($pgn->get_last_halfmove_number());
```

echo($pgn->get_fen_after_halfmove(2));
echo($pgn->get_initial_fen());

$pgn->move('a4');
Record new moves, add tags and export PGN:
``` php
$pgn = new Onspli\Chess\PGN('[Event "Testing"] 1.Nf3 Nf6 2.c4 g6');
$pgn->set_tag('Site', 'Github');
$pgn->move('a4');
$pgn->move('a5');
print_r($pgn->get_tags());
echo($pgn->export_tags());
echo($pgn->export_movetext());
echo($pgn->export());
```

for ($hm = $pgn->get_initial_halfmove_number() - 1; $hm <= $pgn->get_last_halfmove_number(); $hm++) echo $pgn->get_fen_after_halfmove($hm) . PHP_EOL;
Extract position after certain move:
``` php
$pgn = new Onspli\Chess\PGN('1.Nf3 Nf6 2.c4 g6');
echo($pgn->get_current_fen());
echo($pgn->get_initial_fen());
echo($pgn->get_fen_after_halfmove(0));
echo($pgn->get_fen_after_halfmove(2));
echo($pgn->get_fen_after_halfmove(Onspli\Chess\PGN::get_halfmove_number(1, 'b')));
```

FEN is returned as `php string` by default. Passing parameter `php $as_object = true` makes it FEN object:
``` php
$pgn = new Onspli\Chess\PGN('1.Nf3 Nf6 2.c4 g6');
echo($pgn->get_current_fen(true)->preview());
echo($pgn->get_fen_after_halfmove(2, true)->preview());
```

PGN with custom initial position:
``` php
$pgn = new Onspli\Chess\PGN('[FEN "rnbqkb1r/pppp1ppp/5n2/4p3/2P1P3/5N2/PP1P1PPP/RNBQKB1R b KQkq - 0 3"] 3... Nc6 4. Qb3');
echo($pgn->get_initial_fen());
echo($pgn->get_initial_halfmove_number());
```
20 changes: 20 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
| [PGN::unset_initial_fen](#PGNunset_initial_fen) | Unset custom initial position - use the standard initial position. |
| [PGN::unset_tag](#PGNunset_tag) | Remove tag pair (header). |
| [PGN::get_tag](#PGNget_tag) | Read tag pair (header) value. |
| [PGN::get_tags](#PGNget_tags) | Get all tags as associative array |
| [PGN::get_initial_fen](#PGNget_initial_fen) | Get initial position. |
| [PGN::get_current_fen](#PGNget_current_fen) | Get FEN of current position. |
| [PGN::get_fen_after_halfmove](#PGNget_fen_after_halfmove) | Get position after given halfmove. |
Expand Down Expand Up @@ -1751,6 +1752,25 @@ PGN::get_tag( string name ): ?string



---
### PGN::get_tags

Get all tags as associative array

```php
PGN::get_tags( ): array
```





**Return Value:**





---
### PGN::get_initial_fen

Expand Down
8 changes: 8 additions & 0 deletions src/PGN.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ public function get_tag(string $name) : ?string
return $this->tags[$name];
}

/**
* Get all tags as associative array
*/
public function get_tags() : array
{
return $this->tags;
}

/**
* Get initial position.
*/
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/PGNTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,18 @@ public function testValidateMoves() : void
$pgn->validate_moves();
}

public function testGetAllTags() : void
{
$pgn = new PGN($this->samples[0]);
$this->assertEquals([
"Event" => "F/S Return Match",
"Site" => "Belgrade, Serbia JUG",
"Date" => "1992.11.04",
"Round" => "29",
"White" => "Fischer, Robert J.",
"Black" => "Spassky, Boris V.",
"Result" => "1/2-1/2"
], $pgn->get_tags());
}

}

0 comments on commit 5d10b7e

Please sign in to comment.