]> git.sesse.net Git - remoteglot-book/commitdiff
Use the new opening data from pgn-extract.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 11 Dec 2014 00:31:18 +0000 (01:31 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 11 Dec 2014 00:31:18 +0000 (01:31 +0100)
ECO.pm [deleted file]
binlookup.cpp
count.h
eco-list.pl [deleted file]
www/opening-stats.pl

diff --git a/ECO.pm b/ECO.pm
deleted file mode 100755 (executable)
index c1a3833..0000000
--- a/ECO.pm
+++ /dev/null
@@ -1,82 +0,0 @@
-#! /usr/bin/perl
-#
-# Get eco.pgn from ftp://ftp.cs.kent.ac.uk/pub/djb/pgn-extract/eco.pgn,
-# or any other opening database you might want to use as a base.
-#
-use strict;
-use warnings;
-use Chess::PGN::Parse;
-
-require 'Position.pm';
-
-package ECO;
-
-our %fen_to_opening = ();
-our @openings = ();
-
-sub init {
-       {
-               my $pos = Position->start_pos("white", "black");
-               my $key = _key_for_pos($pos);
-               push @openings, { eco => 'A00', name => 'Start position' };
-               $fen_to_opening{$key} = $#openings;
-       }
-
-       my $pgn = Chess::PGN::Parse->new("eco.pgn")
-               or die "can't open eco.pgn\n";
-       while ($pgn->read_game()) {
-               my $tags = $pgn->tags();
-               $pgn->quick_parse_game;
-               my $pos = Position->start_pos("white", "black");
-               my $moves = $pgn->moves // [];
-               my $eco = $pgn->eco;
-               next if (!defined($eco));
-               my $name = $tags->{'Opening'};
-               if (exists($tags->{'Variation'}) && $tags->{'Variation'} ne '') {
-                       $name .= ": " . $tags->{'Variation'};
-               }
-               for (my $i = 0; $i < scalar @$moves; ++$i) {
-                       my ($from_row, $from_col, $to_row, $to_col, $promo) = $pos->parse_pretty_move($moves->[$i]);
-                       $pos = $pos->make_move($from_row, $from_col, $to_row, $to_col, $promo, $moves->[$i]);
-               }
-               my $key = _key_for_pos($pos);
-               push @openings, { eco => $pgn->eco(), name => $name };
-               $fen_to_opening{$key} = $#openings;
-       }
-}
-
-sub persist {
-       my $filename = shift;
-       open my $fh, ">", $filename
-               or die "openings.txt: $!";
-       for my $opening (@openings) {
-               print $fh $opening->{'eco'}, " ", $opening->{'name'}, "\n";
-       }
-       close $fh;
-}
-
-sub unpersist {
-       my $filename = shift;
-       open my $fh, "<", $filename
-               or die "openings.txt: $!";
-       while (<$fh>) {
-               chomp;
-               push @openings, $_;
-       }
-       close $fh;
-}
-
-sub get_opening_num {  # May return undef.
-       my $pos = shift;
-       return $fen_to_opening{_key_for_pos($pos)};
-}
-
-sub _key_for_pos {
-       my $pos = shift;
-       my $key = $pos->fen;
-       # Remove the move clocks.
-       $key =~ s/ \d+ \d+$//;
-       return $key;
-}
-
-1;
index 5b5fd40f6101cfd29634506294dc03b8ffc7d719..488a8e3912fcbfa7ccc2c6843cb121bcec49ee52 100644 (file)
@@ -36,7 +36,7 @@ int main(int argc, char **argv)
        while (mtbl_iter_next(it, &key, &len_key, &val, &len_val)) {
                string move((char *)(key + prefix_len), len_key - prefix_len);
                const Count* c = (Count *)val;
-               printf("%s %d %d %d %d %f %f %d\n", move.c_str(),
+               printf("%s %d %d %d %u %f %f %d\n", move.c_str(),
                        c->white, c->draw, c->black, c->opening_num,
                        float(c->sum_white_elo) / c->num_elo,
                        float(c->sum_black_elo) / c->num_elo,
diff --git a/count.h b/count.h
index 3024b8981f748a4953bbd9afe03716646228dd61..8d843c7b70f2c968e7bac5e7189c14299e9e7d26 100644 (file)
--- a/count.h
+++ b/count.h
@@ -3,7 +3,7 @@ struct Count {
        int white = 0;
        int draw = 0;
        int black = 0;
-       int opening_num = -1;
+       unsigned int opening_num = 0;
        unsigned long long sum_white_elo = 0;
        unsigned long long sum_black_elo = 0;
        int num_elo = 0;
diff --git a/eco-list.pl b/eco-list.pl
deleted file mode 100644 (file)
index 52e3546..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-#! /usr/bin/perl
-use strict;
-use warnings;
-require 'ECO.pm';
-
-ECO::init();
-ECO::persist();
-
index 06f67b26931905884642f75c9210b03f7e56f842..d74f1add083bddcc89ec6551b4091df760f0e16e 100755 (executable)
@@ -5,9 +5,9 @@ use CGI;
 use JSON::XS;
 use lib '..';
 use Position;
-use ECO;
 
-ECO::unpersist("../openings.txt");
+our %openings = ();
+read_openings();
 
 my $cgi = CGI->new;
 my $fen = $cgi->param('fen');
@@ -31,7 +31,7 @@ while (<$fh>) {
                black_avg_elo => $black_avg_elo * 1,
                num_elo => $num_elo * 1
        };
-       $opening = $ECO::openings[$opening_num];
+       $opening = $openings{$opening_num};
 }
 close $fh;
 
@@ -44,3 +44,18 @@ sub num {
        my $x = shift;
        return $x->{'white'} + $x->{'draw'} + $x->{'black'};
 }
+
+sub read_openings {
+       open my $fh, "../openings.txt"
+               or die "../openings.txt: $!";
+       for my $line (<$fh>) {
+               chomp $line;
+               my ($hash, $eco, $opening, $variation, $subvariation) = split /\t/, $line;
+               if ($variation eq '') {
+                       $openings{$hash} = $eco . ": " . $opening;
+               } else {
+                       $openings{$hash} = $eco . ": " . $opening . ": " . $variation;
+               }
+       }
+       close $fh;
+}