Hot answers tagged

8

Would this be what you are looking for? PGN-extract (A command line utility) http://www.cs.kent.ac.uk/people/staff/djb/pgn-extract/ I can see a flag in the feature doc that might help: -W[cm|epd|halg|lalg|elalg|san|uci] - specify the output format to use -Whalg is hyphenated long algebraic. -Wlalg is long algebraic -Welalg[PNBRQK] is enhanced long ...


6

Python utilities for experimenting with Leela Chess Zero a neural network based chess engine: https://github.com/glinscott/leela-chess/ Here: https://github.com/so-much-meta/lczero_tools This allows you to run the network in Python on specific board positions via python-chess, and get policy/value outputs. (Works with pytorch, and is also able to run the ...


4

Yes, there is a chess engine in Python: Sunfish There is also a python library for chess: python-chess


4

The argument needs to point to the Stockfish executable. If you download the v11 zip from the Stockfish website, extract it and find the directories with the executables. I extracted it to C:\Users\<username>\Downloads\stockfish-11-win The executables are in C:\Users\<username>\Downloads\stockfish-11-win\Windows To use the executable, give the ...


3

Both approaches will work. In the second approach, you will have better accuracy of your evaluations but you may miss an important move. For example, if 20...Rxc3 positional exchange sacrifice is a strong move, you will probably miss it with a 1-2s quick engine search. In the first approach, you will lose accuracy but you won't miss a strong hidden resource ...


3

Build a cache. You're exposing yourself to endless transposition. Please don't shuffle your moves. I don't have the numbers but I doubt randomly shuffle all your lists can be quick. It's O(n). I fail to see how it can address your repetition anyway. Double check your generator. Source code is not here, so I have no idea I don't know what exactly your table ...


3

The volatility of the score here is caused by the time limit you have set. With time=0.100 you're restricting the engine to 100 milliseconds. With such a small time limit, there are a lot of small factors that can dramatically change the results. You can try increasing the time limit, or instead of using a time limit you can try a depth limit instead. This ...


3

As per: https://python-chess.readthedocs.io/en/latest/engine.html#indefinite-or-infinite-analysis engine.analysis(board) now returns an AnalysisResult object, which has the property info that you want. So instead of: nwinfo = engine.analysis(...) print(nwinfo["score"]) You would loop over info in the AnalysisResult, and print the score property from each ...


3

I used the following python script using python-chess library : import chess import chess.pgn def KPvK_up_to_symmetry_export_to_FEN_func(pgn_file_path, nb_games, output_pgn_file_name): output_pgn = open(output_pgn_file_name,"w+") games = 0 pgn_file = open(pgn_file_path, encoding="utf-8-sig") while nb_games == 0 or games < nb_games: ...


3

In the error log you've pasted you can see it's a permission error, which can occur for files that behave as "executables" if you will (think any form of program, e.g. scripts, compiled files etc), but that have not been given the rights by the os to do so. One way to fix it, go to the folder containing the engine file and open a terminal to run the ...


3

You can do this by sending out MultiPV, something like: engine = chess.uci.popen_engine(...) engine.setoption({ "MultiPV" : multiPV}) The module will generate multiple lines for you, as an array.


2

Yes. You can use the package to play against yourself and an engine. While I don't think there is one function that can do everything for you, but I can give you hints: To setup up a game against an AI: Read UCI documentation (you can find on Google) Read the UCI functions for python-chess here Spawn an engine thread by engine = chess.uci.popen_engine("/...


2

The code for those routines fairly liberally uses bitwise-and (&) rather than boolean-and (and) for the logical expressions, so I assume integer values rather than booleans are being returned. I would guess that you would see something similar with is_castling if castling is possible; the False value is returned explicitly rather than being the result of ...


2

To get evaluation score for each legal move, you can do: Set MultiPV to a high number, like in your answer Play the moves, then invoke SF on each of the resulting FEN. For example, in the starting position you could start 20 SF instances, one for each possible move. This is the approach adopted by commercial companies, such as Chessable. The first approach ...


2

I have experience with only SunFish, but I doubt it's 2500+ level. http://talkchess.com/forum3/viewtopic.php?f=2&t=67025 This link should help you. If you're not happy with the folks at talkchess, you probably won't find a better answer anywhere else.


2

You may claim yourself the World Computer Champion in Python if you beat all of them: http://talkchess.com/forum3/viewtopic.php?f=2&t=67025


2

This should get you started, up to you to add extra features based on your objective (such as saving the games and moves, etc.) import chess import chess.engine import os import sys arguments = sys.argv pondertime = float(arguments[1]) #first argument: ponder time in sec maxmoves = int(arguments[2]) #2nd argument: max number of desired moves gamecount = ...


2

If you've already installed python-chess via pip/conda/choco or your favourite package manager, then to use chess.pgn, in your .py files just call from chess import pgn or import chess.pgn. You have to explicitly write either import, even if you already have import chess. Then you can use it like from chess import pgn with open("my_pgn_file.pgn", "r") as ...


2

First you need to install this library: pip install python-chess And make sure you're using the right pip, because still in some Linus distros pip and python are python 2.x and for python 3.x you should use pip3 and python3. If you want to check try this: pip --version You will see an output like this that will tell you what python you're using, in my ...


2

First, engine.options["NeverClearHash"] is wrong on two counts. It is engine.option, not engine.options and that command queries the value rather than sets it. To set the value you have to do something like: engine.configure({"NeverClearHash": 1}) or perhaps: engine.configure({"NeverClearHash": true}) Good programmers are never too proud to read the ...


2

The Problem was p.communicate(), which kills the subprocess. The interaction with the engine works well with the following code: def put(command, inf_list, tmp_time): engine.stdin.write(command+'\n') engine.stdin.flush() time.sleep(tmp_time) if command <> "quit": engine.stdin.write('isready\n') engine.stdin....


2

python-chess already has the built in utilities to handle conversions. Here is an example: import chess board = chess.Board('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1') WP = board.pieces(chess.PAWN, chess.WHITE) BP = board.pieces(chess.PAWN, chess.BLACK) print (int(WP), int (BP)) print (list(WP), list(BP)) print ("\nWHITE PAWNS:\n" + str(...


1

I need to get two things out of the way: If your are concerned about performance, Python is not for you. The language just isn't built for this kind of thing. That said, I do think it can be useful for learning/building your first engine, but it's unlikely your engine will be very strong. Speed improvements for an engine come from optimizations to the ...


1

If you want to access every node, then you're basically iterating over the game tree. A straightforward recursion will get you there, something like: def traverse(node): # do whatever stuff you want for the node here print(node.move) # terminating condition if (node.is_end()): return # recursion for child_node in node....


1

Even though the best answer is to use a pre-existing module instead of reinventing the wheel, just for illustrative purposes here's a version that works using only pure Python Popen(). The key is the use of "isready" and waiting for an answer: import time from subprocess import Popen, PIPE def put(p, msg): p.stdin.write(msg+'\n') p.stdin.flush() ...


1

If you're working with python, you'll have far better luck with python-chess It includes a fully featured core API for the rules of chess, and is also able to handle engine communication via both UCI and XBoard (along with support for opening books, endgame tablebases, chess variants, rendering, PGN files, etc.) This would be significantly easier than ...


1

An alternative to Sunfish is PyChess: http://pychess.org/ (or https://github.com/pychess/pychess) It's stronger than sunfish (has 2100 bullet rating on lichess: https://lichess.org/@/PyChessBot ) and also allows playing chess variants, such as antichess or fisher random. It has a lot more code than sunfish though, so pulling it out as a separate library ...


1

Seems to be working just fine: >>> from subprocess import Popen, PIPE >>> p = Popen( 'stockfish', stdout=PIPE, stdin=PIPE) >>> p.stdin.write('uci\n') >>> p.stdin.write('go depth 10\n') >>> print p.communicate()[0] Stockfish 09-06-13 64bit by Tord Romstad, Marco Costalba and Joona Kiiski id name Stockfish 09-06-13 ...


1

The problem was that the moves were tested in the context of the new board (after the move). But the correct way is that they should be tested with the board they were played on. See the discussion here. https://github.com/niklasf/python-chess/issues/169


Only top voted, non community-wiki answers of a minimum length are eligible