Tool to reverse a fen string

Discussion of anything and everything relating to chess playing software and machines.

Moderators: hgm, Rebel, chrisw

glav
Posts: 41
Joined: Sun Apr 07, 2019 1:10 am
Full name: Giovanni Lavorgna

Tool to reverse a fen string

Post by glav »

Sometimes, i like to play openings in countermove, i.e., the are some set-ups I like to play with the Black, that I would like to try to play also with the White pieces.

Is there a tool that allows to to reverse a fen string? Ideally the tool shoulg get as input a file containing a fen string representing a position like this:
[d]r1bqkb1r/pp3ppp/2nppn2/6B1/3NP3/2N5/PPPQ1PPP/R3KB1R b KQkq - 1 7


and output a file containing a fen string like this:
[d]r3kb1r/pppq1ppp/2n5/3np3/6b1/2NPPN2/PP3PPP/R1BQKB1R w KQkq - 1 8


The number move (last character) isn't really important. Ideally the tool shoule work from the command line, but other formats are also OK. Finally if the tool coulkd reverse also a pgn file would be terrific.
Thanks in advance.

Giovanni
istolacio
Posts: 13
Joined: Thu Aug 31, 2006 12:29 am
Location: Valencia, Spain

Re: Tool to reverse a fen string

Post by istolacio »

glav
Posts: 41
Joined: Sun Apr 07, 2019 1:10 am
Full name: Giovanni Lavorgna

Re: Tool to reverse a fen string

Post by glav »

Thank you! Appreciated.
Norm Pollock
Posts: 1056
Joined: Thu Mar 09, 2006 4:15 pm
Location: Long Island, NY, USA

Re: Tool to reverse a fen string

Post by Norm Pollock »

Interesting project. I gave it a try and I came up with my version of the tool. It is pretty fast considering it is written in Java. I estimate it flips "epd" records at about 10,000 records per second on a typical home PC using I5. No opcodes or blank lines in output. Outputs are "epd" files.

Consider this to be a beta version, so any comments, bug finds, suggestions appreciated. I will probably put it into my "40H-EPD" toolbox next week after I write up a readme and if no bugs are found (hopefully).

usage for command line execution:

epdFlip filename.epd

output:
outZ.epd : the flipped records
outZ2.epd: the original records and the flipped records alternating

link:
https://www.mediafire.com/file/l9jjdfsm ... p.exe/file
Updated links for 40H Tools and Databases
http://40Hchess.epizy.com
http://nk-qy.info/40h
Dann Corbit
Posts: 12540
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: Tool to reverse a fen string

Post by Dann Corbit »

You can also reverse the colors
If there are no pawns on the board you can rotate 90 and 270 degrees also.
Taking ideas is not a vice, it is a virtue. We have another word for this. It is called learning.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.
Ferdy
Posts: 4833
Joined: Sun Aug 10, 2008 3:15 pm
Location: Philippines

Re: Tool to reverse a fen string

Post by Ferdy »

glav wrote: Wed Dec 23, 2020 11:30 amFinally if the tool coulkd reverse also a pgn file would be terrific.
Thanks in advance.

Giovanni
Given a normal game,

[pgn][Event "Skilling Open Prelim"]
[Site "chess24.com INT"]
[Date "2020.11.22"]
[Round "4.5"]
[White "Duda, Jan-Krzysztof"]
[Black "So, Wesley"]
[Result "0-1"]
[BlackElo "2770"]
[BlackFideId "5202213"]
[BlackTitle "GM"]
[ECO "D37"]
[EventDate "2020.11.22"]
[Opening "QGD"]
[Variation "classical variation (5.Bf4)"]
[WhiteElo "2743"]
[WhiteFideId "1170546"]
[WhiteTitle "GM"]

1.d4 Nf6 2.c4 e6 3.Nf3 d5 4.Nc3 Be7 5.Bf4 O-O 6.e3 Nbd7 7.c5 Nh5 8.Bd3 Nxf4 9.exf4 b6 10.b4 a5 11.a3 c6 12.O-O Ba6 13.g3 Bf6 14.Qe2 Bxd3 15.Qxd3 Ra7 16.Ra2 Qa8 17.Rfa1 Rb8 18.Qc2 Rab7 19.Kg2 bxc5 20.bxc5 Qa6 21.Na4 Rb5 22.Re1 Qb7 23.f5 Nf8 24.g4 Qc7 25.h3 h6 26.Nc3 R5b7 27.Re3 exf5 28.gxf5 Bg5 29.Nxg5 hxg5 30.Qd1 Qf4 31.Qg4 g6 32.Rg3 Rb2 33.Rxb2 Rxb2 34.Qxf4 gxf4 35.Rf3 gxf5 36.Na4 Rd2 37.Nb6 Rxd4 38.Nc8 Ng6 39.Rb3 Kg7 40.Nd6 Nh4+ 41.Kf1 f3 42.Rb1 Rd3 43.a4 Rc3 44.Nb7 Ng6 45.Nxa5 Ne5 46.Nb3 Nd3 47.a5 Rc2 48.a6 Rxf2+ 49.Kg1 Rg2+ 50.Kf1 Rf2+ 51.Kg1 Rg2+ 52.Kf1 Nf4 53.a7 Nxh3 54.Ke1 Rg1+ 55.Kd2 Rxb1 56.a8=Q f2 57.Qb8 f1=Q 58.Qe5+ Kh7 0-1[/pgn]

Flip it.

[pgn][Event "Skilling Open Prelim"]
[Site "chess24.com INT"]
[Date "2020.11.22"]
[Round "4.5"]
[White "Duda, Jan-Krzysztof"]
[Black "So, Wesley"]
[Result "0-1"]
[BlackElo "2770"]
[BlackFideId "5202213"]
[BlackTitle "GM"]
[ECO "D37"]
[EventDate "2020.11.22"]
[FEN "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1"]
[Opening "QGD"]
[SetUp "1"]
[Variation "classical variation (5.Bf4)"]
[WhiteElo "2743"]
[WhiteFideId "1170546"]
[WhiteTitle "GM"]

1... d5 2. Nf3 c5 3. e3 Nf6 4. d4 Nc6 5. Be2 Bf5 6. O-O e6 7. Nbd2 c4 8. Nh4 Bd6 9. Nxf5 exf5 10. b3 b5 11. a4 a6 12. c3 O-O 13. Ba3 g6 14. Bf3 Qe7 15. Bxd6 Qxd6 16. Ra2 Ra7 17. Qa1 Rfa8 18. Rb1 Qc7 19. Rab2 Kg7 20. bxc4 bxc4 21. Qa3 Na5 22. Rb4 Re8 23. Qb2 f4 24. Nf1 g5 25. Qc2 h6 26. h3 Nc6 27. R4b2 Re6 28. exf4 gxf4 29. Bg4 Nxg4 30. hxg4 Qd8 31. Qf5 Qg5 32. g3 Rg6 33. Rb7 Rxb7 34. Rxb7 Qxf5 35. gxf5 Rf6 36. gxf4 Na5 37. Rd7 Nb3 38. Rxd5 Nc1 39. Ng3 Rb6 40. Kg2 Nd3 41. Nh5+ Kf8 42. f6 Rb8 43. Rd6 a5 44. Rc6 Nb2 45. Ng3 Nxa4 46. Ne4 Nb6 47. Nd6 a4 48. Rc7 a3 49. Rxf7+ Kg8 50. Rg7+ Kf8 51. Rf7+ Kg8 52. Rg7+ Kf8 53. Nf5 a2 54. Nxh6 Ke8 55. Rg8+ Kd7 56. Rxb8 a1=Q 57. f7 Qb1 58. f8=Q Qe4+ 59. Kh2 0-1
[/pgn]

Is this what you need?
Do you like to flip the white/black names, elos, fideid, result etc. too?
Dann Corbit
Posts: 12540
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: Tool to reverse a fen string

Post by Dann Corbit »

I guess that for reversing a PGN file something much simpler is wanted:
Turn the PGN file into EPD
Reverse the order of the EPD strings so that the last one is first and the first one is last.

The above is very useful for analysis. Actual move inversion like you showed would have very limited usefulness (as I see it).
What would you do with it?
Taking ideas is not a vice, it is a virtue. We have another word for this. It is called learning.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.
Ferdy
Posts: 4833
Joined: Sun Aug 10, 2008 3:15 pm
Location: Philippines

Re: Tool to reverse a fen string

Post by Ferdy »

Dann Corbit wrote: Sat Dec 26, 2020 9:33 am The above is very useful for analysis. Actual move inversion like you showed would have very limited usefulness (as I see it).
What would you do with it?
I don't know at the moment, that is why I ask the TS.
Vinvin
Posts: 5228
Joined: Thu Mar 09, 2006 9:40 am
Full name: Vincent Lejeune

Re: Tool to reverse a fen string

Post by Vinvin »

In Arena, go in the "position" menu -> "Set up a position" -> "edit" tab and there you have "mirror" and "swap color"
glav
Posts: 41
Joined: Sun Apr 07, 2019 1:10 am
Full name: Giovanni Lavorgna

Re: Tool to reverse a fen string

Post by glav »

Ferdy wrote: Sat Dec 26, 2020 9:26 am
glav wrote: Wed Dec 23, 2020 11:30 amFinally if the tool coulkd reverse also a pgn file would be terrific.
Thanks in advance.

Giovanni
Given a normal game,

[pgn][Event "Skilling Open Prelim"]
[Site "chess24.com INT"]
[Date "2020.11.22"]
[Round "4.5"]
[White "Duda, Jan-Krzysztof"]
[Black "So, Wesley"]
[Result "0-1"]
[BlackElo "2770"]
[BlackFideId "5202213"]
[BlackTitle "GM"]
[ECO "D37"]
[EventDate "2020.11.22"]
[Opening "QGD"]
[Variation "classical variation (5.Bf4)"]
[WhiteElo "2743"]
[WhiteFideId "1170546"]
[WhiteTitle "GM"]

1.d4 Nf6 2.c4 e6 3.Nf3 d5 4.Nc3 Be7 5.Bf4 O-O 6.e3 Nbd7 7.c5 Nh5 8.Bd3 Nxf4 9.exf4 b6 10.b4 a5 11.a3 c6 12.O-O Ba6 13.g3 Bf6 14.Qe2 Bxd3 15.Qxd3 Ra7 16.Ra2 Qa8 17.Rfa1 Rb8 18.Qc2 Rab7 19.Kg2 bxc5 20.bxc5 Qa6 21.Na4 Rb5 22.Re1 Qb7 23.f5 Nf8 24.g4 Qc7 25.h3 h6 26.Nc3 R5b7 27.Re3 exf5 28.gxf5 Bg5 29.Nxg5 hxg5 30.Qd1 Qf4 31.Qg4 g6 32.Rg3 Rb2 33.Rxb2 Rxb2 34.Qxf4 gxf4 35.Rf3 gxf5 36.Na4 Rd2 37.Nb6 Rxd4 38.Nc8 Ng6 39.Rb3 Kg7 40.Nd6 Nh4+ 41.Kf1 f3 42.Rb1 Rd3 43.a4 Rc3 44.Nb7 Ng6 45.Nxa5 Ne5 46.Nb3 Nd3 47.a5 Rc2 48.a6 Rxf2+ 49.Kg1 Rg2+ 50.Kf1 Rf2+ 51.Kg1 Rg2+ 52.Kf1 Nf4 53.a7 Nxh3 54.Ke1 Rg1+ 55.Kd2 Rxb1 56.a8=Q f2 57.Qb8 f1=Q 58.Qe5+ Kh7 0-1[/pgn]

Flip it.

[pgn][Event "Skilling Open Prelim"]
[Site "chess24.com INT"]
[Date "2020.11.22"]
[Round "4.5"]
[White "Duda, Jan-Krzysztof"]
[Black "So, Wesley"]
[Result "0-1"]
[BlackElo "2770"]
[BlackFideId "5202213"]
[BlackTitle "GM"]
[ECO "D37"]
[EventDate "2020.11.22"]
[FEN "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1"]
[Opening "QGD"]
[SetUp "1"]
[Variation "classical variation (5.Bf4)"]
[WhiteElo "2743"]
[WhiteFideId "1170546"]
[WhiteTitle "GM"]

1... d5 2. Nf3 c5 3. e3 Nf6 4. d4 Nc6 5. Be2 Bf5 6. O-O e6 7. Nbd2 c4 8. Nh4 Bd6 9. Nxf5 exf5 10. b3 b5 11. a4 a6 12. c3 O-O 13. Ba3 g6 14. Bf3 Qe7 15. Bxd6 Qxd6 16. Ra2 Ra7 17. Qa1 Rfa8 18. Rb1 Qc7 19. Rab2 Kg7 20. bxc4 bxc4 21. Qa3 Na5 22. Rb4 Re8 23. Qb2 f4 24. Nf1 g5 25. Qc2 h6 26. h3 Nc6 27. R4b2 Re6 28. exf4 gxf4 29. Bg4 Nxg4 30. hxg4 Qd8 31. Qf5 Qg5 32. g3 Rg6 33. Rb7 Rxb7 34. Rxb7 Qxf5 35. gxf5 Rf6 36. gxf4 Na5 37. Rd7 Nb3 38. Rxd5 Nc1 39. Ng3 Rb6 40. Kg2 Nd3 41. Nh5+ Kf8 42. f6 Rb8 43. Rd6 a5 44. Rc6 Nb2 45. Ng3 Nxa4 46. Ne4 Nb6 47. Nd6 a4 48. Rc7 a3 49. Rxf7+ Kg8 50. Rg7+ Kf8 51. Rf7+ Kg8 52. Rg7+ Kf8 53. Nf5 a2 54. Nxh6 Ke8 55. Rg8+ Kd7 56. Rxb8 a1=Q 57. f7 Qb1 58. f8=Q Qe4+ 59. Kh2 0-1
[/pgn]

Is this what you need?
Do you like to flip the white/black names, elos, fideid, result etc. too?
Hi, Ferdy. Sorry for the delay in answering your post and thanks again for your usual kindness and willingness to help.

Yes, it is indeed what I need. My intended use would be to study the opening repertoire of players I like the play of style of. For istance, say that I understood and liked a few plans of Kasparov in the Sicilian with black. If I reproduce them with white they should be even better. Right? In this perspective, it would be great if I could gather some more information from the database to see if other players have already employed the same idea. So I need a tool to quickly reverse the game in order to have "probes" to scan the database with. Such a tool would be a great step in that direction. May be I could reverse all Kasparov games with black at once and try to mine the games for other plans I liked.
OK to reverse names, elo, results, etct.
Thanks again.
Giovanni