Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chess960 support #6

Merged
merged 4 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PHP library for reading and editing FEN and PGN chess formats.

## Features
### FEN class
- load FEN representing chess position
- load FEN representing chess position (standard or Shredder-FEN)
- read and modify all FEN fields
- export FEN
- read and set piece placement
Expand All @@ -15,13 +15,15 @@ PHP library for reading and editing FEN and PGN chess formats.
- perform move in given position
- test if move is legal in given position
- list all legal moves in given position
- supports Fisher's random (Chess960)

### PGN class
- load PGN representing chess game
- read and set all tag pairs (PGN headers)
- export PGN
- read and add moves
- get FEN position after any move
- supports Fisher's random (Chess960)

## Installation
Install with composer:
Expand All @@ -44,7 +46,7 @@ echo($fen->get_board());
// rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
echo($fen->get_active_color());
// w
echo($fen->get_castling_string());
echo($fen->get_castling());
// KQkq
echo($fen->get_en_passant());
// -
Expand Down Expand Up @@ -76,7 +78,7 @@ echo($fen->get_board());
// rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR
echo($fen->get_active_color());
// b
echo($fen->get_castling_string());
echo($fen->get_castling());
// KQq
echo($fen->get_en_passant());
// c6
Expand Down Expand Up @@ -124,7 +126,7 @@ 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');
$fen->set_castling('KQq');
$fen->set_en_passant('c6');
$fen->set_halfmove(1);
$fen->set_fullmove(2);
Expand Down Expand Up @@ -185,6 +187,7 @@ 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');
$fen->set_castling('-');
echo($fen->is_check() ? 'true' : 'false');
// false
echo($fen->is_stalemate() ? 'true' : 'false');
Expand Down
11 changes: 7 additions & 4 deletions README.template.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PHP library for reading and editing FEN and PGN chess formats.

## Features
### FEN class
- load FEN representing chess position
- load FEN representing chess position (standard or Shredder-FEN)
- read and modify all FEN fields
- export FEN
- read and set piece placement
Expand All @@ -15,13 +15,15 @@ PHP library for reading and editing FEN and PGN chess formats.
- perform move in given position
- test if move is legal in given position
- list all legal moves in given position
- supports Fisher's random (Chess960)

### PGN class
- load PGN representing chess game
- read and set all tag pairs (PGN headers)
- export PGN
- read and add moves
- get FEN position after any move
- supports Fisher's random (Chess960)

## Installation
Install with composer:
Expand All @@ -40,7 +42,7 @@ echo($fen->export());
echo($fen->export_short());
echo($fen->get_board());
echo($fen->get_active_color());
echo($fen->get_castling_string());
echo($fen->get_castling());
echo($fen->get_en_passant());
echo($fen->get_halfmove());
echo($fen->get_fullmove());
Expand All @@ -54,7 +56,7 @@ echo($fen->export());
echo($fen->export_short());
echo($fen->get_board());
echo($fen->get_active_color());
echo($fen->get_castling_string());
echo($fen->get_castling());
echo($fen->get_en_passant());
echo($fen->get_halfmove());
echo($fen->get_fullmove());
Expand All @@ -76,7 +78,7 @@ $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');
$fen->set_castling('KQq');
$fen->set_en_passant('c6');
$fen->set_halfmove(1);
$fen->set_fullmove(2);
Expand Down Expand Up @@ -105,6 +107,7 @@ 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');
$fen->set_castling('-');
echo($fen->is_check() ? 'true' : 'false');
echo($fen->is_stalemate() ? 'true' : 'false');
echo($fen->preview());
Expand Down
108 changes: 41 additions & 67 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
| [Board::get_defended_squares](#Boardget_defended_squares) | Get array of all squares defended (or attacked) by $defender being on $defender_square. |
| [Board::get_reachable_squares](#Boardget_reachable_squares) | Get array of all squares reachable from $origin_square by $moving_piece. |
| [Board::find_squares_with_piece](#Boardfind_squares_with_piece) | Returns array of squares containing piece. |
| [Board::find_square_with_piece](#Boardfind_square_with_piece) | Returns square containing piece. If there are more pieces, throws. |
| [Board::get_color_of_piece](#Boardget_color_of_piece) | Returns the color of the piece. |
| [Board::get_piece_of_color](#Boardget_piece_of_color) | Converts piece to requested color. |
| [Board::get_opposite_color](#Boardget_opposite_color) | Get color opposite to color passed as an argument. |
| [Board::is_square_attacked_by_piece](#Boardis_square_attacked_by_piece) | Tells whether the square is attacked by particular piece |
| [Board::is_square_attacked](#Boardis_square_attacked) | Tells whether the square is attacked by the color specified. |
| [Board::is_check](#Boardis_check) | Tells whether the king of color specified is in check. |
| [**FEN**](#FEN) | FEN is a standard notation for describing a particular board position of a chess game |
| [FEN::__construct](#FEN__construct) | Load FEN or setup starting position. |
| [FEN::__construct](#FEN__construct) | Load FEN (or Shredder-FEN) or setup starting position. |
| [FEN::__clone](#FEN__clone) | |
| [FEN::export](#FENexport) | Export whole FEN string. |
| [FEN::export_short](#FENexport_short) | Export FEN string without halfmoves count and fullmove number. |
Expand All @@ -34,10 +35,8 @@
| [FEN::set_square](#FENset_square) | Set piece on a particular square. |
| [FEN::get_active_color](#FENget_active_color) | Active color. |
| [FEN::set_active_color](#FENset_active_color) | Set active color. |
| [FEN::get_castling_string](#FENget_castling_string) | Castling availability. |
| [FEN::set_castling_string](#FENset_castling_string) | Set castling availability. |
| [FEN::get_castling_availability](#FENget_castling_availability) | Get castling availability of particular type. |
| [FEN::set_castling_availability](#FENset_castling_availability) | Set castling availability of particular type. |
| [FEN::get_castling](#FENget_castling) | Castling availability. |
| [FEN::set_castling](#FENset_castling) | Set castling availability. |
| [FEN::get_en_passant](#FENget_en_passant) | Get En Passant target square. |
| [FEN::set_en_passant](#FENset_en_passant) | Set En Passant target square. |
| [FEN::get_halfmove](#FENget_halfmove) | Get Halfmove clock |
Expand Down Expand Up @@ -367,6 +366,32 @@ Board::find_squares_with_piece( string piece, bool as_object = false ): array



---
### Board::find_square_with_piece

Returns square containing piece. If there are more pieces, throws.

```php
Board::find_square_with_piece( string piece, bool as_object = false ): mixed
```




**Parameters:**

| Parameter | Type | Description |
|-----------|------|-------------|
| `piece` | **string** | |
| `as_object` | **bool** | |


**Return Value:**





---
### Board::get_color_of_piece

Expand Down Expand Up @@ -539,7 +564,7 @@ move according to chess rules.

### FEN::__construct

Load FEN or setup starting position.
Load FEN (or Shredder-FEN) or setup starting position.

```php
FEN::__construct( string fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1' ): void
Expand Down Expand Up @@ -818,19 +843,22 @@ FEN::set_active_color( string color ): void


---
### FEN::get_castling_string
### FEN::get_castling

Castling availability.

```php
FEN::get_castling_string( ): string
FEN::get_castling( ): string
```

If neither side can castle, this is "-".
Otherwise, this has one or more letters: "K" (White can castle kingside),
"Q" (White can castle queenside), "k" (Black can castle kingside), and/or
"q" (Black can castle queenside). A move that temporarily prevents castling
does not negate this notation.
Shredder-FEN is also supported - instead of K and Q the letters
of files containing rooks are used. For standard chess its AHah.
X-FEN is not supported.



Expand All @@ -841,19 +869,22 @@ castling availability string


---
### FEN::set_castling_string
### FEN::set_castling

Set castling availability.

```php
FEN::set_castling_string( string castling ): void
FEN::set_castling( string castling ): void
```

If neither side can castle, this is "-".
Otherwise, this has one or more letters: "K" (White can castle kingside),
"Q" (White can castle queenside), "k" (Black can castle kingside), and/or
"q" (Black can castle queenside). A move that temporarily prevents castling
does not negate this notation.
Shredder-FEN is also supported - instead of K and Q the letters
of files containing rooks are used. For standard chess its AHah.
X-FEN is not supported.


**Parameters:**
Expand All @@ -869,63 +900,6 @@ does not negate this notation.



---
### FEN::get_castling_availability

Get castling availability of particular type.

```php
FEN::get_castling_availability( string type ): bool
```

Possible castling types: "K" (White can castle kingside),
"Q" (White can castle queenside), "k" (Black can castle kingside), and/or
"q" (Black can castle queenside). A move that temporarily prevents castling
does not negate this notation.


**Parameters:**

| Parameter | Type | Description |
|-----------|------|-------------|
| `type` | **string** | |


**Return Value:**





---
### FEN::set_castling_availability

Set castling availability of particular type.

```php
FEN::set_castling_availability( string type, bool avalability ): void
```

Possible castling types: "K" (White can castle kingside),
"Q" (White can castle queenside), "k" (Black can castle kingside), and/or
"q" (Black can castle queenside). A move that temporarily prevents castling
does not negate this notation.


**Parameters:**

| Parameter | Type | Description |
|-----------|------|-------------|
| `type` | **string** | |
| `avalability` | **bool** | |


**Return Value:**





---
### FEN::get_en_passant

Expand Down
21 changes: 14 additions & 7 deletions src/Board.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,18 @@ public function find_squares_with_piece(string $piece, bool $as_object = false)
return $arr;
}

/**
* Returns square containing piece. If there are more pieces, throws.
*/
public function find_square_with_piece(string $piece, bool $as_object = false)
{
$squares = $this->find_squares_with_piece($piece, $as_object);
if (sizeof($squares) != 1) {
throw new RulesException("There are " . sizeof($squares) . " pieces '" . $piece . "' on the board.");
}
return $squares[0];
}

/**
* Returns the color of the piece.
* @return string w|b
Expand Down Expand Up @@ -493,13 +505,8 @@ public function is_square_attacked($square, string $attacking_color) : bool
public function is_check(string $color) : bool
{
self::validate_color($color);

$king_squares = $this->find_squares_with_piece(self::get_piece_of_color('K', $color), true);
if (sizeof($king_squares) != 1) {
throw new RulesException("There are " . sizeof($king_squares) . " kings of active color on the board.");
}

return $this->is_square_attacked($king_squares[0], self::get_opposite_color($color));
$king_square = $this->find_square_with_piece(self::get_piece_of_color('K', $color), true);
return $this->is_square_attacked($king_square, self::get_opposite_color($color));
}

private static function validate_color(string $color) : void
Expand Down
Loading