Giter VIP home page Giter VIP logo

pgntofen's Introduction

PGN to FEN

A chess library, where the only purpose is to convert a PGN structur to FEN.

Usage

The lib exposes several methods, to be used.

Move

You can insert one and one move if you want.

E.g

import pgntofen # assumes you have pgntofen.py in the same directory, or you know how to handle python modules.
pgnConverter = pgntofen.PgnToFen()
pgnConverter.move('d4')
fen = pgnConverter.getFullFen()
#fen will be 'rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR b - KQkq'

Moves

You can send a string, or an array of strings. If you send a string, it may be a valid PGN Line (1.e4 d5 2.Nf3 ....) if it'a and array of strings, you may only send the actulle moves (['e4', 'd5', 'Nf3'])

E.g

import pgntofen # assumes you have pgntofen.py in the same directory, or you know how to handle python modules.
pgnConverter = pgntofen.PgnToFen()
PGNMoves = 'd4 d5'
pgnConverter.pgnToFen(PGNMoves.split(''))
fen = pgnConverter.getFullFen()
#fen will be 'rnbqkbnr/ppp1pppp/8/3p4/3P4/8/PPP1PPPP/RNBQKBNR - KQkq'

pgnFile

parse a pgnFile that may have sveral pgn games. See test/Carlsen.pgn for an example

E.g

pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
file = "test/Carlsen.pgn"
stats =  pgnConverter.pgnFile(file);
# stats => {
# 'failed': [<pgntofen-error-obj>, ...],
# 'succeeded': [<game-obj>, ...]
# }

# a game-obj: (game_info, fens)
# pgntofen-error-obj: (game_info, lastMove, fen, error)
# fens: array of fen
# game_info is all the line in the pgn file working as a header before the game (e.g: all lines with [...])

Speed

Running 20 000 games with pgntofen takes about 45-50 seconds on a normal laptop (4GB Ram, i7, SSD). With chess-python this takes about 6m 15s to 6m 30s. So you should expect an 8x improvement at least.

The file fenStats.py takes either "pgntofen" or "chess-python" as input. to test on your computer run time python fenStats.py pgntofen and time python fenStats.py chess-python.

Development

Watch.sh

To run the test on each change, you can start the watch.sh script. ./watch.sh pwd pgntofen.test.py will run the test script pgntofen.test.py on each save to the current dir.

Validate a problem

The python-chess lib is and excellent library. If something goes wrong with this library, see if you can run the same moves with chess-python. If there is a mistake there also, it's probably something wrong with the moves you are putting in.

pgntofen's People

Contributors

abhinav7sinha avatar karstenoe avatar levibarnes avatar lucianoinfanti avatar sindresvendby avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pgntofen's Issues

Error in conversion

Thanks for the library

I'm getting List index out of range for the following list

['d4', 'd5', 'c4', 'dxc4', 'e4', 'e5', 'Nf3', 'exd4', 'Bxc4', 'Nc6', 'O-O', 'Be6', 'Bb5', 'Bc5', 'Nbd2', 'Ne7', 'Ng5', 'Qd7', 'Nxe6', 'Qxe6', 'Nb3', 'Qe5', 'f4', 'Qd6', 'e5', 'Qd5', 'Qd3', 'Bb6', 'Bd2', 'a5', 'Bc4', 'Qd7', 'Rae1', 'Qf5', 'Nc1', 'a4', 'Re2', 'Ba5', 'e6', 'fxe6', 'Bxe6', 'Qxd3', 'Nxd3', 'Bxd2', 'Rxd2', 'b6', 'g4', 'Nd8', 'Bc4', 'Ra5', 'Ne5', 'c5', 'Re1', 'b5', 'Bd3', 'g6', 'f5', 'gxf5', 'gxf5', 'c4', 'Be4', 'Ra6', 'Rxd4', 'Rf8', 'Nd7', 'Rf7', 'f6', 'Raxf6', 'Nxf6+', 'Rxf6', 'Bxh7', 'Nc6', 'Rd2', 'a3', 'bxa3', 'Kf7', 'Rd7', 'Kf8', 'Rb7', 'Rh6', 'Be4', 'Nd4', 'a4']

raceback (most recent call last):
File "", line 1, in
File "pgntofen.py", line 122, in pgnToFen
self.move(move)
File "pgntofen.py", line 142, in move
self.handleAllmoves(move)
File "pgntofen.py", line 204, in handleAllmoves
self.pawnMove(toPosition, specificCol, specificRow, takes, promote)
File "pgntofen.py", line 477, in pawnMove
self.updateOldLinePos(piece,chessBoardNumber, toPosition)
File "pgntofen.py", line 501, in updateOldLinePos
piece = self.internalChessBoard[posistion]
IndexError: list index out of range

License

Do you think you could add a license to this? I'd like to use it in an open source, not-for-profit project. Any open source license would be fine.

wrong order of segments

enpassant field needs to be behind the castling availability.
for me this worked:

def getFullFen(self):
        return self.getFen() + ' ' + ('w ' if self.whiteToMove else 'b ') + (self.castlingRights if self.castlingRights else '-') + ' ' + self.enpassant

resetBoard doesn't reset fens

In class PgnToFen the function resetBoard doesn't reset self.fens[] which results in weird behavior of the function pgnFile.

Issue converting multiple PGN to FEN

I know this is an old lib, I've just started using it to create a chess engine that plays unseen moves from the opponent.

I've got an issue with pgnFile() giving me the same FEN for every game. Here's my code.

The output I'm getting is this. The FEN list contains the same game each time.

If you feel like picking up an old project and helping me out I'd appreciate it. :)

Error in conversion

Hi, I receive the same error message that has been reported in an earlier issue:

IndexError('list index out of range',))

PGN-file to reproduce:

`[Event "41. Olympiad Women 2014"]
[Site "Tromso NOR"]
[Date "2014.08.05"]
[Round "4.7"]
[White "Hoang, Thanh Trang"]
[Black "Ordaz Valdes, Lisandra Teresa"]
[Result "1/2-1/2"]
[WhiteElo "2485"]
[BlackElo "2345"]
[ECO "E35"]
[EventDate "2014.08.02"]

1.d4 Nf6 2.c4 e6 3.Nc3 Bb4 4.Qc2 d5 5.cxd5 exd5 6.Bg5 c5 7.dxc5 h6 8.Bh4
g5 9.Bg3 Ne4 10.e3 Qa5 11.Rc1 Qxa2 12.Bb5+ Bd7 13.Bxd7+ Nxd7 14.Ne2 Nxc3
15.Nxc3 Qc4 16.Bd6 Nxc5 17.Bxc5 Qxc5 18.O-O Bxc3 19.Qa4+ Qc6 20.Qa3 Qa6
21.Qxa6 bxa6 22.Rxc3 O-O 23.Rc6 Kg7 24.h4 Rfb8 25.Rd1 Rxb2 26.Rxd5 Rb6 27.
Rc7 Rb5 28.Rdd7 Rf8 29.Rxa7 a5 30.h5 g4 31.Rd4 Rg5 32.g3 Rxh5 33.Rxg4+ Rg5
34.Rf4 Re5 35.Kg2 Kg6 36.Ra6+ Kg7 37.Rg4+ Rg5 38.Rxg5+ hxg5 39.Kf3 f6 40.
Rxa5 Re8 41.Kg4 Kg6 42.Ra7 Rb8 43.Kf3 Rb2 44.g4 Rb3 45.Re7 Ra3 46.Rd7 Ra2
47.Rd3 Ra1 48.Kg2 Ra2 49.Kg3 Ra1 50.f4 Rg1+ 51.Kh3 gxf4 52.exf4 Rh1+ 53.
Kg2 Ra1 54.Rd6 Kf7 55.Kf3 Rg1 56.Rd7+ Ke6 57.Ra7 Rf1+ 58.Ke3 Rg1 59.Ra6+
Kf7 60.Kf3 Re1 61.Ra7+ Kf8 62.Ra3 Kf7 63.Re3 Rg1 64.Rc3 Re1 65.Rc2 Kf8 66.
Re2 Rg1 67.Re4 Kf7 68.f5 Kf8 69.Ke3 Kf7 70.Kd4 Rd1+ 71.Kc5 Kg7 72.Rd4 Ra1
73.Rd7+ Kh6 74.Rf7 Kg5 75.Rg7+ Kf4 76.Kd6 Re1 77.Rg6 Ra1 78.Ke7 Ke5 79.Rg7
Ra6 80.Kf7 Kf4 81.Kg6 Kxg4 82.Rf7 Ra8 83.Rxf6 Rg8+ 84.Kf7 Ra8 85.Kg7 Kg5
86.Rf7 Ra5 87.f6 Ra6 88.Rf8 Rb6 89.f7 Rg6+ 90.Kh7 Rh6+ 1/2-1/2
`

You can check for yourself that the file contains a valid game and that it can be read by other chess software without problems. Please let me know if you need further help to investigate the problem.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.