Skip to content

Commit

Permalink
Fix promoting.
Browse files Browse the repository at this point in the history
  • Loading branch information
onspli committed Sep 17, 2021
1 parent 863b31c commit 3a7a97a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/FEN.php
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ private function standard_move(Move &$move) : void

$new_board->set_square($origin, '');
if ($move->get_promotion()) {
$new_board->set_square($target, $move->get_promotion());
$new_board->set_square($target, Board::get_piece_of_color($move->get_promotion(), $this->get_active_color()));
} else {
$new_board->set_square($target, $piece);
if ($move->get_piece_type() == 'P' && $target->export() == $this->get_en_passant()) {
Expand All @@ -733,8 +733,9 @@ private function standard_move(Move &$move) : void

private function set_new_board(Board $new_board) : void
{
if ($new_board->is_check($this->get_active_color())) {
throw new RulesException('King is in check.');
$active_color = $this->get_active_color();
if ($new_board->is_check($active_color)) {
throw new RulesException('King is in check. ' . $new_board->export() . ' ' . $active_color);
}
$this->set_board($new_board);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/FENTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ public function testPromotion() : void
$fen->set_board('1q5k/P7/8/8/8/8/8/K7');
$fen->move('a8=Q');
$this->assertEquals('Qq5k/8/8/8/8/8/8/K7', $fen->get_board());

$fen = new FEN('2Q5/8/8/8/p3Q3/P7/7p/1K4k1 b - - 0 53');
$fen->move('h1=Q');
$this->assertEquals('2Q5/8/8/8/p3Q3/P7/8/1K4kq', $fen->get_board());
}

public function testPromotionNotSpecified() : void
Expand Down

0 comments on commit 3a7a97a

Please sign in to comment.