Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Aug 13 02:09
  • Aug 11 12:45
    niklasf commented #915
  • Aug 11 12:44
    niklasf closed #915
  • Aug 11 12:44
    niklasf closed #361
  • Aug 11 12:44

    niklasf on master

    Link python-chess-engine-extens… (compare)

  • Aug 11 12:29
    niklasf labeled #916
  • Aug 11 12:29
    niklasf closed #916
  • Aug 11 12:29
    niklasf commented #916
  • Aug 11 12:25
    niklasf closed #917
  • Aug 11 12:25
    niklasf commented #917
  • Aug 11 12:20
    niklasf commented #917
  • Aug 11 12:19
    niklasf labeled #917
  • Aug 10 11:12
    alexlopespereira commented #917
  • Aug 09 18:55

    niklasf on master

    Move required Python to install… (compare)

  • Aug 09 15:12
    alexlopespereira opened #917
  • Aug 09 01:06
    alexlopespereira opened #916
  • Aug 08 14:24
    GarboChess opened #915
  • Aug 07 02:10
    niklasf closed #914
  • Aug 07 02:10
    niklasf labeled #914
AphilipA
@AphilipA
i think i figured out the problem i was having earlier. I was using google colab and it was not loading the right version of the chess module
any pointers for getting google colab to import the correct module?
Ghost
@ghost~58c242a5d73408ce4f4f9df5

The PyPI package name python-chess was renamed to chess, so if python-chess is installed with pip install python-chess, you get version 1.999 which is not maintained anymore and never will be. To get the latest and the actively maintained version of python-chess, it needs to be installed with pip install chess. This actively maintained version of python-chess is currently at 1.7.0. And to check which version of python-chess you are using, do this:

import chess
print(chess.__version__)

or

import chess
chess.__version__

Please respond with the version number you get on Google Colab.

Ghost
@ghost~58c242a5d73408ce4f4f9df5
I have already checked which version of python-chess Google Colab is using. They are using a very old version of python-chess: 0.23.11.
No wonder you get errors on Google Colab. To update python-chess on Google Colab, do this: %pip install chess
Ghost
@ghost~58c242a5d73408ce4f4f9df5
But it says: Requirement already satisfied: chess in /usr/local/lib/python3.7/dist-packages (1.7.0)
Ghost
@ghost~58c242a5d73408ce4f4f9df5
I wasn't able to import version 1.7.0 of python-chess. If I do import chess, Google Colab always imports version 0.23.11. I don't know why. It's very frustrating. Anyway, I'm glad your code works on the newest version of python-chess outside Google Colab. Fuck Google Colab, it's stupid.
AphilipA
@AphilipA
yea thats the same version I was getting. its disappointing because its a nice way to work synchronously with a partner
for jupyter notebooks
i dont know of any other jupyter notebook workspace synchronization utility
Ghost
@ghost~58c242a5d73408ce4f4f9df5
It's a bummer, actually. Maybe send a strong e-mail letter to Google?
Ray
@rheinzelman
I have maybe a dumb question about python-chess, is it able to produce a visual of the board allow the user to click to make moves or is this purely for legal move validation and complete virtualization of a chess game?
6 replies
Ghost
@ghost~58c242a5d73408ce4f4f9df5
There is chess.svg for that.
Sam Q
@SamQ26867433_twitter

@niklasf python-chess is really great! When first using chess.polyglot, all went well where I got:

import chess
import chess.polyglot
board = chess.Board()
with chess.polyglot.open_reader(polyglot_books/Cerebellum3Merge.bin') as reader:
    for entry in reader.find_all(board):
        print(entry.move, entry.weight, entry.learn)

e2e4 255 0
d2d4 127 0

So how do I get the rest of possible openings in book? (e.g. - 1.c4, 1.Nf3....etc)

Sam Q
@SamQ26867433_twitter
@niklasf The solution is to edit the polyglot book to assign weights, the moves were then found. Apparently, when a move isn't given a weight it's not found by the chess.polyglot.open_reader function.
Ghost
@ghost~58c242a5d73408ce4f4f9df5
@SamQ26867433_twitter, the documentation is saying this: By default, entries with weight 0 are excluded. This is a common way to delete entries from an opening book without compacting it. Pass minimum_weight=0 to select all entries.
Ghost
@ghost~58c242a5d73408ce4f4f9df5

@niklasf, don't you think some aliases are missing in chess.variant for the 3-check chess variant? Currently, python-chess has these aliases for it: aliases = ["Three-check", "Three check", "Threecheck", "Three check chess", "3-check", "3 check", "3check"]

The list should be:
aliases = ["Three-check", "Three check", "Threecheck", "Three-check chess", "Three check chess", "Threecheck chess", "3-check", "3 check", "3check", "3-check chess", "3 check chess", "3check chess"]

Niklas Fiekas
@niklasf
the goal is not to cover all conceivable spellings, just real world uses. the recent additions are actually used by chess.com (afaik)
Ghost
@ghost~58c242a5d73408ce4f4f9df5
I understand, but maybe some day the alias 3-check chess will become used by lichess.org or chess.com or some other website, and you'll need to alter the alias list again. To be future-proof, I see no harm in adding some not-yet-used aliases.
Ghost
@ghost~58c242a5d73408ce4f4f9df5

@niklasf, I have found a bug in python-chess. The bug involves chess.Board().status(). Here's my proof of concept:

>>> import chess
>>> chess.Board("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1").status()
<Status.VALID: 0>

As you can see, I passed the starting FEN to chess.Board(), but I set Black (b) as being the first to move. And since this is clearly not valid, please add a check to the status() method to detect whether it is actually White that is on turn to move in the starting position.

Fixing this bug also involves adding a new Status enum.

Niklas Fiekas
@niklasf

what's the exact criterium that you see violated? note that

This does not imply that the position is actually reachable with a series of legal moves from the starting position.

is not a goal

Ghost
@ghost~58c242a5d73408ce4f4f9df5
I'm talking about the starting position itself here. No move has been done, none. Just w is a b (color part of the FEN) in the starting position, indicating that Black starts a game of chess. And since White always moves first, not Black, so a FEN like this should be reported as not valid by the status() method.
Ghost
@ghost~58c242a5d73408ce4f4f9df5
Nothing is violated per se, but an invalid chess board position is reported by the status() method as being valid. And that is wrong, and this needs to be fixed.
Ray
@rheinzelman
Is there a best practice to create a dynamic list of legal moves based on the turns? currently my program is able to have either player move the other's pieces because legal_moves returns a list of moves for both players. Or perhaps there's another constraint for moves that I'm missing?
Niklas Fiekas
@niklasf
@rheinzelman board.legal_moves should definitely only contain the legal moves of the current side to move. can you please double-check and/or show an example where it has both sides?
2 replies
peter-mckenzie
@peter-mckenzie
I didn't see a built in way to get the material balance, using the old 1,3,3,5,9 scale, of the current position? Just checking I didn't miss something before I write code to do this...
Niklas Fiekas
@niklasf
@peter-mckenzie hi, yep, this feature doesn't exist in python-chess
Niklas Witzig
@nmwitzig
Hi @niklasf , I have one question: Is it possible to replace the say black pawns on an svg board with a custom label e.g., a 1? I tried to tinker with the sourcecode of the squares attribute described here (https://python-chess.readthedocs.io/en/latest/svg.html resp. in python-chess/chess/init.py ) but unsucessful so far. Any tips very welcome! Thank you!
Niklas Fiekas
@niklasf
hi @nmwitzig. unfortunately it will require working with the raw svg. here's an svg path that represents a 1: https://github.com/niklasf/python-chess/blob/eef8a29907e91662a24a4f0a79416f14c8a6d24c/chess/svg.py#L51
Niklas Witzig
@nmwitzig
Hi @niklasf , thank you! I think I can make this work! Did you use a particular font when coming up with these paths? I think I will simply create custom labels (e.g., with https://danmarshall.github.io/google-font-to-svg-path/) and then try to embed them into the svg board similarly how you place this XX.
Thank you!
9acca9
@9acca9
Hi. Im making a physical chess board. I want to add the possibility to play a game "face to face" and save that game to lichess.org i will do that through the lichess api but, how i can get the PGN format with python chess?) Thanks!
Niklas Fiekas
@niklasf
9acca9
@9acca9

hi @9acca9. did you find https://python-chess.readthedocs.io/en/latest/pgn.html#writing ?

oh, thanks! i will try it! Thanks

Wolfgang Fahl
@WolfgangFahl
@9acca9 we had the same issue with https://github.com/WolfgangFahl/play-chess-with-a-webcam - and the lichess organizer was very helpful. We also used this python-chess library.
squint2
@squint2
@niklasf I am currently doing a chess app using the python-chess library. Since using FENs to search for positions in a SQL database would be easy, I wish to read a pgn file, convert moves from the pgn to FENs for each move and then use keyboard arrow keys to traverse forward and back through the FENs to display the moves on a GUI board already created. I've already successfully read a pgn file, but can't see how to generate the FENs by using 'for move in first_game.mainline_moves():'. How do you recommend generating a FEN for each move in 'first_game.mainline_moves():'? Do you provide capability to convert sequential FEN positions of a game into algebraic moves for display in a move list? What are your thought on my use of FENs in this case? Does it take a lot longer to search for similar chess variations using a database full of pgn-based moves rather than FENs?
Niklas Fiekas
@niklasf
hi @squint2. while iterating you can update a board. see the example in https://python-chess.readthedocs.io/en/latest/pgn.html#chess.pgn.read_game for how to do that. then just use board.fen()
fen seems decent for database lookup, although some specialized hashes or tightly packed bytes could be more efficient. whether or not you should use fen or moves depends. do you want to find transpositions?
it might also be good that fens are somewhat limited in size (unlike lists of moves of arbitrary length)
clarke-baxter
@clarke-baxter
hi
I'm struggling with writing a PGN. I just need a simple file with a record of a single game. I'm using board=chess.Board() and board.push(). When the game is over how do I get the game record? Do I need to create a game object? If so, how do I get the moves from board to game? Any help appreciated.
Niklas Fiekas
@niklasf
hi. print(chess.pgn.Game.from_board(board))should do
clarke-baxter
@clarke-baxter
Thank you so much!!! python chess is brilliant!. I am a complete Python newbie but have managed using your work to make my chess computer from around 1979
display the moves on a modern lcd segment display and 'speak' the moves. I got really hung up on this bit.
Thanks again for python chess and thanks for responding. cb.