Forums

How do I create a FEN string?

Sort:
ipcress12

Here's a Python snippet that works. "input" and "output" are for the pgn file paths. You don't have to install pgn-extract, just place its folder somewhere.

PGN_EXTRACT = r"C:\pgn-extract\pgn-extract.exe"

def add_fens(input, output):
    import subprocess
    args = [PGN_EXTRACT, "--fencomments", "-o%s" % output, input]
    subprocess.call(args)
    print open(output, "r").read()

JCapanegra

Thanks!

I´m seeing the longest match in worldwide Tennis Davis Cup history:

 

14/13 Argentina against Brasil in the 5th set after 6 hours 40 minutes..... I´ll come back soon.... or not????!!!

 

15/13 in the 5th after 6 hours 44 minutes Argentina!!!!

Was the 4th point, now the 5th! :-D

ipcress12

You're welcome!

If you don't use Python, you might give it a try. It's a great little language. It connects to just about everything and it's used all over the place.

JCapanegra

Ooops, no idea about that Python Embarassed

I need to just cut and paste and try to compile?.... need to download tht Pthon first. Tongue Out

ipcress12

Download Python 2.7. I use http://www.activestate.com/activepython/downloads. It's pretty painless.

Cut and paste my code, then add something like:

input = "c:/temp/this_game.pgn"
output = "c:/temp/this_game_fens.pgn"
add_fens(input, output)

this_game.pgn must be a valid pgn file in c:/temp.

JCapanegra

I did a new attempt before try Phyton..... commas solved... using "/" to ditinguish fen part from other information.. and found a match palyed in FRA/RUS  that mixed all the stuff again Cry

 

I´m ging to ipcress12 Phyton suggestion

JCapanegra

Mmmm..... it not seems easy....

 

you need any kind of GUI? You could not "cut and paste" onto DOS windows that is all that appears when I launch Python Interactive Shell from Windows Start Button Embarassed

JCapanegra

¡Por favor, sea relevante, útil y agradable!

JCapanegra

Just in case.... would you show me (or upload to a dropbox or something) the result of applying your program to this lines for example:

 

[Event "Lloyds Bank op"]

[Site "London/Paris"]

[Date "1984.??.??"]

[Round "1"]

[White "Adams, Michael"]

[Black "Sedgwick, David"]

[Result "1-0"]

[WhiteElo ""]

[BlackElo ""]

[ECO "C05"]

 

1.e4 e6 2.d4 d5 3.Nd2 Nf6 4.e5 Nfd7 5.f4 c5 6.c3 Nc6 7.Ndf3 cxd4 8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don't know if this helps but I use Python (vers. 3) & I find the GUI that came with it is not very useful. You can't use the Python DOS-type window to launch programs in Python, you will probably need some kind of GUI.

Having said that, I execute my own Python programs (I think scripts is the proper term?) by double-clicking on them, so as to open them using Windows. As long as you've remembered to name your script with an extension or file-type of '.py' then Windows will try to open it with the Python interpreter.

That should at least enable you to run a program but getting it to work as you want may be a little more difficult. ipcress12 may be able to help you there.

ipcress12

JC: I use Eclipse with the PyDev extension to work in Python. I recommend it if you really want to write Python code. The older Python IDE, PyWin is good too. Both are straightforward to download and install. The most basic Python IDE is called IDLE. It comes with Python and it's tolerable.

However, if you just want to work on this particular problem, here's a cut-to-the-bone solution for Windows:

* Copy pgn-extract folder to "C:/"
* Install Python 2.7
* Create "C:/temp" folder.
* Add "this_game.pgn" file to "C:/temp"
* Create "add_fens.py" in "C:/temp" folder
* Add the following code to add_fens.py:

PGN_EXTRACT = "C:/pgn-extract/pgn-extract.exe"
def add_fens(input, output):
    import subprocess
    args = [PGN_EXTRACT, "--fencomments", "-o%s" % output, input]
    subprocess.call(args)
    print open(output, "r").read()

input = "this_game.pgn"
output = "this_game_out.pgn"
add_fens(input, output)

* Double-click "C:/temp/add_fens.py"
- "C:/temp/this_game_out.pgn" should appear.

ipcress12

If I write your pgn code to "this_game.pgn" it will output both games in a format that looks like this:

[Event "Lloyds Bank op"]
[Site "London/Paris"]
[Date "1984.??.??"]
[Round "1"]
[White "Adams, Michael"]
[Black "Sedgwick, David"]
[Result "1-0"]
[WhiteElo ""]
[BlackElo ""]
[ECO "C05"]

1. e4 { rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1 } 1...
e6 { rnbqkbnr/pppp1ppp/4p3/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2 } 2. d4 {
rnbqkbnr/pppp1ppp/4p3/8/3PP3/8/PPP2PPP/RNBQKBNR b KQkq d3 0 2 } 2... d5 {
rnbqkbnr/ppp2ppp/4p3/3p4/3PP3/8/PPP2PPP/RNBQKBNR w KQkq d6 0 3 } 3. Nd2 {
rnbqkbnr/ppp2ppp/4p3/3p4/3PP3/8/PPPN1PPP/R1BQKBNR b KQkq - 1 3 } 3... Nf6 {
rnbqkb1r/ppp2ppp/4pn2/3p4/3PP3/8/PPPN1PPP/R1BQKBNR w KQkq - 2 4 } 4. e5 {
rnbqkb1r/ppp2ppp/4pn2/3pP3/3P4/8/PPPN1PPP/R1BQKBNR b KQkq - 0 4 } 4... Nfd7
{ rnbqkb1r/pppn1ppp/4p3/3pP3/3P4/8/PPPN1PPP/R1BQKBNR w KQkq - 1 5 } 5. f4 {

 

... etc.

JCapanegra

Thanks guys! But to read it for my stats, I need only the fen lines without those headers.

But after two days of learning and work, I could do my own very very too tooooo slow program to make what I need that is somehing like this:

"2r4k/p4b2/4pq2/1p1p1nR1/5P2/P2B4/1P2Q2P/1K4R1 b - -  30"

"7k/p1r2b2/4pq2/1p1p1nR1/5P2/P2B4/1P2Q2P/1K4R1 w - -  31"

"7k/p1r2b2/4pq2/1p1p1BR1/5P2/P7/1P2Q2P/1K4R1 b - -  31"

"7k/p1r2b2/5q2/1p1p1pR1/5P2/P7/1P2Q2P/1K4R1 w - -  32"

"7k/p1r2b2/5q2/1p1p1p1R/5P2/P7/1P2Q2P/1K4R1 b - -  32"

"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -  1"

"rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3  1"

"rnbqkbnr/pppp1ppp/4p3/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq -  2"

"rnbqkbnr/pppp1ppp/4p3/8/3PP3/8/PPP2PPP/RNBQKBNR b KQkq d3  2"

"rnbqkbnr/ppp2ppp/4p3/3p4/3PP3/8/PPP2PPP/RNBQKBNR w KQkq d6  3"

where you could see after move 32 that a new game positions starts with nothing in the middle.

Is a pitty that cause pgn-extract-17-19 (that I use into my programm) do not show it, m fen lines do not show neither the necesary data for 50 moves rule. pgn2fen do it Cry

I uploaded the necesary files to sourceforge.net:

https://sourceforge.net/projects/pgn2fenc/

ipcress12

I can't tell what problem you were trying to solve.

I can tell you -- because I've written a suite of PGN parsing tools of my own including FEN support -- that it's a lot more work to write that code yourself as opposed to calling pgn-extract with --fencomments and then ignoring the game header and move information.

JCapanegra

I know you are right about the great work needed to did what I did! Laughing

But I do not know another way to cut those headers than a QBasic program.

And soon or later I need to ignore those headers.... so I just prefer to cut them in a previous step so I could have a cleaner stats program than it would be if it must includes the ignore of the headers in it.

 

BTW: not sure if my poor english and my even poorest programming knowledge is allowing us to undesrtand each other Undecided

ipcress12

Sure, use QBasic to remove the headers.

This is standard operating procedure for solving programming challenges. Find something that does most of what you want then massage the output to get what you really want.

stephen_33

JCapanegra, I may be misunderstanding your point in post #53 but you do realise Black resigned on that move (32) & the game ended?  I think that's why it then opens/starts the next game in the sequence.

What exactly did you expect to see 'in the middle'?  The new FEN string is for a standard starting position, so isn't that what you want?

theturk1234

If you need some help, go to DF14 and copy the position and paste it into WORD ot notepad. Note the results. Then try to move the pieces around and note the results.

JCapanegra

Sorry Stephen, not sure if I understand you (my english is too poor).

I think fen line

"7k/p1r2b2/5q2/1p1p1p1R/5P2/P7/1P2Q2P/1K4R1 b - -  32"

means:

"This is the position when blacks need to make his 32th move."

or in other words:

"This si the position after 32th move from whites and before 32th move from black."

The "b 32" indicates it´s black 32th turn.

Yes, white made his 32th move and then black resign.

The italic lines there are a copy of fen in the way I need them. That is without empty line between each game.

That is because for my stats its dont care about games, just care positions. In fact the number of the moves are not needed for me... at least for the moment Cool

JCapanegra

What is DF14 theturk?