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?

3 Answers 3

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')
4
  • Apart from python-chess, is there anything else which not requires to build the above by yourself? (Thanks though!) Jul 25, 2016 at 14:48
  • @theshepherd A GUI like Chessbase, Arena?
    – ABCD
    Jul 25, 2016 at 15:39
  • Yes but with a CLI interface, so I can parse the results in a Python script. Jul 27, 2016 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.

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

1
  • ZINC isn't a python port of cutechess-cli.
    – ZirconiumX
    Aug 14, 2016 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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