-# Returns 'none', 'white', 'black' or 'both', depending on which sides are in check.
-# The latter naturally indicates an invalid position.
-sub in_check {
- my $board = shift;
- my ($black_check, $white_check) = (0, 0);
+# Like can_reach, but also checks the move doesn't put the side in check.
+# We use this in prettyprint_move to reduce the disambiguation, because Chess.js
+# needs moves to be in minimally disambiguated form.
+sub can_legally_reach {
+ my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
+
+ return 0 if (!can_reach($board, $piece, $from_row, $from_col, $to_row, $to_col));
+
+ my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col);
+ my $side = ($piece eq lc($piece)) ? 'k' : 'K';
+
+ return !in_check($nb, $side);
+}
+
+my %pieces_against_side = (
+ k => { K => 1, Q => 1, R => 1, N => 1, B => 1, P => 1 },
+ K => { k => 1, q => 1, r => 1, n => 1, b => 1, p => 1 },
+);