-sub in_mate {
- my $board = shift;
- my $check = in_check($board);
- return 0 if ($check eq 'none');
-
- # try all possible moves for the side in check
- for my $row (0..7) {
- for my $col (0..7) {
- my $piece = substr($board->[$row], $col, 1);
- next if ($piece eq '-');
-
- if ($check eq 'white') {
- next if ($piece eq lc($piece));
- } else {
- next if ($piece eq uc($piece));
- }
-
- for my $dest_row (0..7) {
- for my $dest_col (0..7) {
- next if ($row == $dest_row && $col == $dest_col);
- next unless (can_reach($board, $piece, $row, $col, $dest_row, $dest_col));
-
- my @nb = @$board;
- substr($nb[$row], $col, 1, '-');
- substr($nb[$dest_row], $dest_col, 1, $piece);
-
- my $new_check = in_check(\@nb);
- return 0 if ($new_check ne $check && $new_check ne 'both');
- }
- }
- }
- }
-
- # nothing to do; mate
- return 1;
+sub output_json {
+ my $info = $engine->{'info'};
+
+ my $json = {};
+ $json->{'position'} = $pos_calculating->to_json_hash();
+ $json->{'id'} = $engine->{'id'};
+ $json->{'score'} = long_score($info, $pos_calculating, '');
+
+ $json->{'nodes'} = $info->{'nodes'};
+ $json->{'nps'} = $info->{'nps'};
+ $json->{'depth'} = $info->{'depth'};
+ $json->{'tbhits'} = $info->{'tbhits'};
+ $json->{'seldepth'} = $info->{'seldepth'};
+
+ # single-PV only for now
+ $json->{'pv_uci'} = $info->{'pv'};
+ $json->{'pv_pretty'} = [ prettyprint_pv($pos_calculating->{'board'}, @{$info->{'pv'}}) ];
+
+ my %refutation_lines = ();
+ my @refutation_lines = ();
+ if (defined($engine2)) {
+ for (my $mpv = 1; $mpv < 500; ++$mpv) {
+ my $info = $engine2->{'info'};
+ my $pretty_move = "";
+ my @pretty_pv = ();
+ last if (!exists($info->{'pv' . $mpv}));
+
+ eval {
+ my $pv = $info->{'pv' . $mpv};
+ my $pretty_move = join('', prettyprint_pv($pos_calculating->{'board'}, $pv->[0]));
+ my @pretty_pv = prettyprint_pv($pos_calculating->{'board'}, @$pv);
+ $refutation_lines{$pv->[0]} = {
+ sort_key => $pretty_move,
+ depth => $info->{'depth' . $mpv},
+ score_sort_key => score_sort_key($info, $pos_calculating, $mpv, 0),
+ pretty_score => short_score($info, $pos_calculating, $mpv, 0),
+ pretty_move => $pretty_move,
+ pv_pretty => \@pretty_pv,
+ };
+ $refutation_lines{$pv->[0]}->{'pv_uci'} = $pv;
+ };
+ }
+ }
+ $json->{'refutation_lines'} = \%refutation_lines;
+
+ open my $fh, ">/srv/analysis.sesse.net/www/analysis.json.tmp"
+ or return;
+ print $fh JSON::XS::encode_json($json);
+ close $fh;
+ rename("/srv/analysis.sesse.net/www/analysis.json.tmp", "/srv/analysis.sesse.net/www/analysis.json");