30

May I know how to find in all grandmaster games, what the percentage of each of the following is?

  • Kingside castling
  • Queenside castling
  • Never castled by the end of the game but still has the right to castle
  • Lost the right to castle

Note When one side has castled more than once in a game, only the first castle counts.

5
  • 2
    By "all grandmaster games" you mean all such games in a certain database you have? Apr 18, 2019 at 5:20
  • Do you need this separately for white and black? (Percentage of white kingside castling, etc?) Apr 18, 2019 at 5:22
  • @user1583209, I do not have any certain database in my mind. If the percentage is available separately for white and black, it will be preferred.
    – Zuriel
    Apr 18, 2019 at 12:46
  • 1
    No player may ever castle twice in a game, since castling and then moving the king back to a position from which it could castle again requires at least one king move, disqualifying it from ever castling again.
    – user45266
    Apr 19, 2019 at 4:26
  • 4
    @user45266 It's an illegal move, but as per OP's link, some historical games do contain illegal moves that nobody caught at the time. Apr 19, 2019 at 6:58

2 Answers 2

32

If you are prepared to use standard Linux command-line tools like wc and grep then I think my free PGN processor, pgn-extract, will do much of the pre-processing necessary to count games in each category. Below is a basic bash script I put together as a proof of concept. It assumes your file of games is called inputfile.pgn - adjust as necessary, or pass it in as a command-line argument:

#!/bin/bash
# Output stats on castling.
src=inputfile.pgn
# Pre-process the games to allow textual analysis.
pgn-extract -C -V -N --notags --nochecks --linelength 10000 -s -o stripped.pgn ${src}
# Castling by white: detect a preceding move number.
grep '\. O-O ' stripped.pgn > Wkingside.pgn
grep '\. O-O-O ' stripped.pgn > Wqueenside.pgn
# Castling by black: detect no preceding move number.
grep '[^.] O-O ' stripped.pgn > Bkingside.pgn
grep '[^.] O-O-O ' stripped.pgn > Bqueenside.pgn
# Find games in which no castling occurred.
grep -v ' O-O ' stripped.pgn | grep -v ' O-O-O ' | grep -v '^$' > nocastle.pgn

# Output the FEN of the final position to include castling rights and
# look for the presence of rights.
pgn-extract -C -V -N --notags --nomovenumbers --linelength 10000 -s nocastle.pgn --dropply -1 -F | grep ' [wb] [KkQq]' > rights.txt

# Output the results - assumes valid Result tags in the source.
echo 'Number of games: ' `grep '^\[Result ' ${src} | wc -l`
echo 'W Kingside: ' `cat Wkingside.pgn | wc -l`
echo 'W Queenside: ' `cat Wqueenside.pgn | wc -l`
echo 'B Kingside: ' `cat Bkingside.pgn | wc -l`
echo 'B Queenside: ' `cat Bqueenside.pgn | wc -l`
echo 'Neither: ' `cat nocastle.pgn | wc -l`
echo 'Rights retained: ' `cat rights.txt | wc -l`

I tried the script on the KingBase database (Apr 2019) and, after removing broken games and duplicates got the following stats:

Number of games:  2072354
W Kingside:  1633762
W Queenside:  246158
B Kingside:  1670870
B Queenside:  128124
Neither:  69776
Rights retained:  24280

The 'neither' figure is about 3.4% while the 'rights retained' figure is about 1.2%. If games with 20 or fewer ply are removed then the 'neither' figure reduces to about 2.9% and the 'rights retained' figure to about 0.7%.

8
  • 1
    Thank you so much for sharing!! I believe that your code works great (unless one side castles more than once, which is illegal anyway).
    – Zuriel
    Apr 18, 2019 at 12:51
  • 7
    Great! Really digging this new trend of script-based answers for stats questions here on chess SE, like yours here or this one recently.
    – user929304
    Apr 18, 2019 at 13:32
  • So, to clarify, "Kingside" counts all those games in which one or both players castled kingside. Games in which one player castled kingside and the other castled queenside will be counted in both categories. Perhaps a more interesting statistic would be the fraction of games in which White (or respectively Black) castled kingside or queenside. Apr 18, 2019 at 17:44
  • 1
    I shouldn't think it would affect the percentages much, but 0-0+ and 0-0-0+ have occasionally happened.
    – Rosie F
    Apr 18, 2019 at 19:26
  • @RosieF that is a good comment about checks so I have added --nochecks to the arguments to pgn-extract in the script and updated the game numbers. There were about 5,000 games affected so the 'neither' percentage is lower than before.
    – kentdjb
    Apr 18, 2019 at 20:24
11

Just googling for "castling statistics" already yields a few results; even though the analysis is done on a different set of games than 'all grandmaster games', it gives a rough indication.

Kingside castling is done by 80-81% of the players, and queen side castling by 8-9%. I estimate that Never castled by the end of the game but still has the right to castle is negligible (less than 0.1%, since it requires a very short game). So that leaves about 11% for the final group Lost the right to castle.

2
  • 1
    The first link is great!! I have never seen such interesting statistics on chess before.
    – Zuriel
    Apr 18, 2019 at 13:08
  • 1
    It's less common these days, but historically, there were many games where two grandmasters would play a few standard opening moves and agree a draw, for example, because the result of their game wasn't going to affect the tournament standings. Probably many of these games ended in a position where at least one of the players still had the right to castle. The frequency plot for different ply depths in your second link contains more than 35,000 games (~2%) that ended after 20 ply or less and nearly all of those will be agreed draws, probably with at least one player having castling rights. Apr 18, 2019 at 17:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.