4

I am searching for a program/library in Python which will let two users or a user and an AI play chess with each other.

PS: I have found python-chess. Can this be used for the above along with a chess engine?

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("/usr/games/stockfish")
  • Start a new game by engine.uci()
  • Send a move by engine.position
  • Start analysis by engine.go
  • Get engine results by command.result()

You will need to read the documentation carefully.

To setup a game against yourself (copied from the package documentation)

>>> import chess

>>> board = chess.Board()

>>> board.push_san("e4")
Move.from_uci('e2e4')
>>> board.push_san("e5")
Move.from_uci('e7e5')
>>> board.push_san("Qh5")
Move.from_uci('d1h5')
>>> board.push_san("Nc6")
Move.from_uci('b8c6')
>>> board.push_san("Bc4")
Move.from_uci('f1c4')
>>> board.push_san("Nf6")
Move.from_uci('g8f6')
>>> board.push_san("Qxf7")
Move.from_uci('h5f7')
| improve this answer | |
  • Apart from python-chess, is there anything else which not requires to build the above by yourself? (Thanks though!) – theshepherd Jul 25 '16 at 14:48
  • @theshepherd A GUI like Chessbase, Arena? – SmallChess Jul 25 '16 at 15:39
  • Yes but with a CLI interface, so I can parse the results in a Python script. – theshepherd Jul 27 '16 at 15:59
1

You mentioned an AI. It might not be very strong, but sunfish is an example of a Python-based chess program, should you decide to go down that route.

Do also bear in mind that XBoard protocol is a valid option, using Polyglot by Fabien Letouzey to convert from UCI to XBoard. It is much harder to convert from XBoard to UCI, through something like WB2UCI by Odd Gunnar Malin, as XBoard is much more lax about things like thinking output.

I would recommend creating an account on the Computer Chess Club if you are going to create a GUI.

| improve this answer | |
0

Cutechess cli ( https://chessprogramming.wikispaces.com/Cutechess-cli ) by Ilari Pihlajisto, is often used to run automated engine-engine matches. There is also work on a python port. ( http://www.talkchess.com/forum/viewtopic.php?t=60504 ) Github: https://github.com/lucasart/zinc

| improve this answer | |
  • ZINC isn't a python port of cutechess-cli. – ZirconiumX Aug 14 '16 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.