I don't think you'll need to elaborate. Please avail yourself with the existing solution:
https://www.chessprogramming.org/Zobrist_Hashing
I don't think you'll need to elaborate. Please avail yourself with the existing solution:
https://www.chessprogramming.org/Zobrist_Hashing
Scid comes with a scid.eco file. This file contains ECO codes, opening names and the corresponding moves, e.g.
D10o "Slav: 3.Nc3 dxc4" 1.d4 d5 2.c4 c6 3.Nc3 dxc4 *
D10o "Slav: 3.Nc3 dxc4 4.a4" 1.d4 d5 2.c4 c6 3.Nc3 dxc4 4.a4 *
D10o "Slav: 3.Nc3 dxc4 4.e3" 1.d4 d5 2.c4 c6 3.Nc3 dxc4 4.e3 *
D10p "Slav: 3.Nc3 dxc4 4.e4" 1.d4 d5 2.c4 c6 3.Nc3 dxc4 4.e4 *
D10q "Slav: 3.Nc3 dxc4 4.e4 b5" 1.d4 d5 2.c4 c6 3.Nc3 dxc4 4.e4 b5 *
I use the same format in my Chess Suite and proceed as follows:
- During initialization I read all lines from such a file and store the ECO and the name for the final position of that move sequence (FEN) in a map
- for a new game a scan successively all positions and apply ECO and opening name to the game for the last game position found in the map.
That work's fine.
Hey I'm actually working on some similar personal projects, the best way to identify the opening i've found is to basically create a lookup table of EPD to opening data, (eco, name, pgn, whatever info you want). My epd lookup doesnt have the scid openings, but includes all the ECO openings, and is only about 4000 items.
For the EPD its basically the fen without the half move clock and full move number parts.
From a pgn of your game, your able to extract the moves, and the fen position if using a library like chess.js which manages game state for you and provides the list of moves and fen. When you get the list of moves, you essentially play the game backwards, matching the fen against the lookup table and basically you just loop backwards until you find a match.
Kinda like...
export function identifyOpening(pgn: string) {
if (!pgn) return null;
const chess = new Chess();
chess.loadPgn(pgn);
const history = chess.history({ verbose: true });
let result = null;
while (!result) {
if (history.length === 0) break;
const epd = history.at(-1)?.after.split(" ").slice(0, 4).join(" ") ?? "";
result = epdLookup[epd as keyof typeof epdLookup];
history.pop();
}
return result;
}
i would create a dictionary of fens where the opening is uniquely identifyable. Then i would generate the fen after the first, second, ... move and match the fen with the dictionary. Select the last fen that is in the dictionary and take its opening
Thanks guys! There is a lot to think about here. Zobrist Hashing looks very intense and a bit outside my knowledge. The other two suggestions is what I'm going to look at.
I took a massive list of PGNs from the Lichess github and created a python script that would "run" those games and return the resulting FENs. The ECO and the opening name were included.
I was able to add a bunch more data into it. But yeah, got completely trapped at other move orders or transpositions.
Thanks again. You've given me some ideas that I was never able to find through google.
Image attached is a sample of what I'd been working with:
(The Id numbers are my own - just whatever loop number basically.)
Hi, first time poster.
This is about getting an opening name from a transposed position
I've been working on a Chess React app for a while which provides stats, including win rates for openings.
I've created a JSON file of about 60000 lines which has lots of chess openings.
My code reads the games PGN, matches the first part of it to my JSON and returns that opening.
I've created seperate "game" logic in another project that converts the PGN to a FEN position. (I don't want to use this seperate code in my react app as it's quite large on its own.)
The issue is, my code will only recognise the opening if the PGN matches, meaning that the moves must be in a correct order
It will recognise this as the Italian Game, Blackburne Shilling Gambit:
1.e4 e5 2.Nf3 Nc6 3.Bc4 Nd4
But it will recognise this as a Bishops opening:
1.e4 e5 2.Bc4 Nc6 3.Nf3 Nd4
Despite the FEN being the same
I'm looking for ways to get them both to say the correct opening, as well as all other openings.
An idea would be to get every possible FEN and put the opening name to that, but considering the number of board positions that exist, that doesn't seem possible.
If this is more vague, I can definitely elaborate
Uhh... How do you post images? 😅 I can't really explain this without a visual aid haha