Difference between revisions of "Python-chess"

From BITPlan Wiki
Jump to navigation Jump to search
 
(5 intermediate revisions by the same user not shown)
Line 83: Line 83:
 
See [https://github.com/WolfgangFahl/play-chess-with-a-webcam/issues/19 Play Chess With a Webcam Issue 'Detecting Moves by a WebCAM'] for how the terms below might be used.
 
See [https://github.com/WolfgangFahl/play-chess-with-a-webcam/issues/19 Play Chess With a Webcam Issue 'Detecting Moves by a WebCAM'] for how the terms below might be used.
 
== AN Algebraic Notation ==
 
== AN Algebraic Notation ==
The [https://en.wikipedia.org/wiki/Algebraic_notation_(chess) Algebraic Notation] describes moves on the chessboard by giving each field a name e.g. "a2" and thus allow to describe from which field e.g. "a2" to which other field e.g. "a4" a move has been performed.
+
{{:Algebraic Notation}}
 
 
Example a2 a4 move:
 
<html>
 
<iframe src="https://lichess.org/embed/AbwZfoMz#1?theme=auto&bg=auto"
 
width=600 height=397 frameborder=0></iframe>
 
</html>
 
  
 
== FEN Forsyth–Edwards Notation ==
 
== FEN Forsyth–Edwards Notation ==
The [https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation Forsyth–Edwards Notation] describes a board position.
+
{{:FEN}}
 
 
Examples:
 
* [https://lichess.org/editor/rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR_w_KQkq_- rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR]
 
* [https://lichess.org/editor/8/8/8/8/8/8/8/8_w_-_-_0_1 8/8/8/8/8/8/8/8] empty board
 
 
 
Given a FEN you can continue playing from that position. E.g given
 
[https://lichess.org/editor/8/5k2/8/5K1P/8/8/8/8_w_-_-_0_1 8/5k2/8/5K1P/8/8/8/8]
 
 
 
a game might continue as:
 
<html>
 
<iframe src="https://lichess.org/embed/XynYr4v1?theme=auto&bg=auto"
 
width=600 height=397 frameborder=0></iframe>
 
</html>
 
  
 
== PGN Portable Game Notation ==
 
== PGN Portable Game Notation ==
The [https://en.wikipedia.org/wiki/Portable_Game_Notation Portable Game Notation] is used to record the course of a game for others to follow or replay.
+
{{:PGN}}
 
 
Example:
 
<pre>
 
[Event "Play Chess With a WebCam"]
 
[Site "Willich, Germany"]
 
[Date "2019-12-21 12:46:49"]
 
[Round "1"]
 
[White "Wolfgang"]
 
[Black "Maria"]
 
[Result "1-0"]
 
 
 
1. e4 e5 2. Bc4 Nc6 3. Qh5 Nf6 4. Qxf7# 1-0
 
</pre>
 
If you paste the above PGN into https://lichess.org/paste you'll get the following result:
 
 
 
{{lichess|cpOszEMY}}
 
  
See also http://lichess.bitplan.com
 
 
== UCI Universal Chess Interface ==
 
== UCI Universal Chess Interface ==
The [https://en.wikipedia.org/wiki/Universal_Chess_Interface Universal Chess Interface] is an open communication protocol to allow chess engines to talk to each other or user interfaces.
+
{{:UCI}}
 
 
Example:
 
<source lang='bash' highlight="3,13">
 
stockfish
 
Stockfish 240314 SSE4.2 by Tord Romstad, Marco Costalba and Joona Kiiski
 
go
 
info depth 1 seldepth 1 score cp 75 nodes 27 nps 13500 time 2 multipv 1 pv e2e4
 
info depth 2 seldepth 2 score cp 12 nodes 140 nps 70000 time 2 multipv 1 pv e2e4 e7e5
 
info depth 3 seldepth 3 score cp 51 nodes 345 nps 115000 time 3 multipv 1 pv e2e4 d7d5 b1c3 d5e4 c3e4
 
info depth 4 seldepth 4 score cp 12 nodes 1059 nps 211800 time 5 multipv 1 pv g1f3 d7d5 d2d4 g8f6
 
info depth 5 seldepth 5 score cp 50 nodes 1678 nps 279666 time 6 multipv 1 pv g1f3 d7d5 d2d4 g8f6 b1c3
 
info depth 6 seldepth 6 score cp 12 nodes 3941 nps 394100 time 10 multipv 1 pv d2d4 g8f6 b1c3 d7d5 g1f3 b8c6
 
info depth 7 seldepth 7 score cp 46 nodes 5356 nps 446333 time 12 multipv 1 pv d2d4 g8f6 b1c3 d7d5 g1f3 e7e6 e2e3
 
info nodes 5356 time 12
 
bestmove d2d4 ponder g8f6
 
quit
 
</source>
 
 
=== Links ===
 
=== Links ===
 
* https://www.shredderchess.com/chess-info/features/uci-universal-chess-interface.html
 
* https://www.shredderchess.com/chess-info/features/uci-universal-chess-interface.html

Latest revision as of 17:05, 28 December 2019

Click here to comment see PlayChessWithAWebCam

python-chess is a pure Python chess library with move generation, move validation and support for common formats. It is open sourced at https://github.com/niklasf/python-chess

Based on a request for more detailed documentation this page gives some more details.

Documentation

UCI / XBoard communication

    import chess
    import chess.engine
    import chess.pgn

    engine = chess.engine.SimpleEngine.popen_uci("/usr/bin/stockfish")
    board = chess.Board()
    game = chess.pgn.Game() 
    game.headers["Event"] = "test_chessengine %s" % (chessEngine.name)
    game.headers["Site"] = "http://wiki.bitplan.com/index.php/PlayChessWithAWebCam" 
    game.headers["White"] = "%s" % (chessEngine.name)
    game.headers["Black"] = "%s" % (chessEngine.name)
    game.headers["Date"] = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
    moveIndex=0
    node=None
    while not board.is_game_over():
        result = engine.play(board, chess.engine.Limit(time=0.1))
        move=str(result.move)
        board.push(result.move)
        print ("%d-%s: %s" % (moveIndex//2+1,"white" if moveIndex%2==0 else "black",move))
        print (board.unicode())
        if moveIndex == 0:
            node = game.add_variation(chess.Move.from_uci(move))
        else:
            node = node.add_variation(chess.Move.from_uci(move))
        moveIndex+=1

    engine.quit()    
    print (game)   
    # try pasting resulting pgn to
    # https://lichess.org/paste

Example run

1-white: e2e4
♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
· · · · · · · ·
· · · · · · · ·
· · · · ♙ · · ·
· · · · · · · ·
♙ ♙ ♙ ♙ · ♙ ♙ ♙
♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
1-black: e7e6
♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
♟ ♟ ♟ ♟ · ♟ ♟ ♟
· · · · ♟ · · ·
· · · · · · · ·
· · · · ♙ · · ·
· · · · · · · ·
♙ ♙ ♙ ♙ · ♙ ♙ ♙
♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
...
87-white: b7b1
· · · · · · · ·
· · · · · · · ·
· · · · · · ♙ ·
♟ · · · · · · ·
♙ · · · · · · ·
· · · ♔ · · · ·
· · · · · · · ·
· ♕ · ♚ · · · ·

Links

Glossary

See Play Chess With a Webcam Issue 'Detecting Moves by a WebCAM' for how the terms below might be used.

AN Algebraic Notation

The Algebraic Notation describes moves on the chessboard by giving each field a name e.g. "a2" and thus allow to describe from which field e.g. "a2" to which other field e.g. "a4" a move has been performed.

Example a2 a4 move:

FEN Forsyth–Edwards Notation

The Forsyth–Edwards Notation describes a board position.

Examples:

Given a FEN you can continue playing from that position. E.g given 8/5k2/8/5K1P/8/8/8/8

a game might continue as:

PGN Portable Game Notation

The Portable Game Notation is used to record the course of a game for others to follow or replay.

Example:

[Event "Play Chess With a WebCam"]
[Site "Willich, Germany"]
[Date "2019-12-21 12:46:49"]
[Round "1"]
[White "Wolfgang"]
[Black "Maria"]
[Result "1-0"]

1. e4 e5 2. Bc4 Nc6 3. Qh5 Nf6 4. Qxf7# 1-0

If you paste the above PGN into https://lichess.org/paste you'll get the following result:



See also http://lichess.bitplan.com

UCI Universal Chess Interface

The Universal Chess Interface is an open communication protocol to allow chess engines to talk to each other or user interfaces.

Example:

stockfish
Stockfish 240314 SSE4.2 by Tord Romstad, Marco Costalba and Joona Kiiski
go 
info depth 1 seldepth 1 score cp 75 nodes 27 nps 13500 time 2 multipv 1 pv e2e4
info depth 2 seldepth 2 score cp 12 nodes 140 nps 70000 time 2 multipv 1 pv e2e4 e7e5
info depth 3 seldepth 3 score cp 51 nodes 345 nps 115000 time 3 multipv 1 pv e2e4 d7d5 b1c3 d5e4 c3e4
info depth 4 seldepth 4 score cp 12 nodes 1059 nps 211800 time 5 multipv 1 pv g1f3 d7d5 d2d4 g8f6
info depth 5 seldepth 5 score cp 50 nodes 1678 nps 279666 time 6 multipv 1 pv g1f3 d7d5 d2d4 g8f6 b1c3
info depth 6 seldepth 6 score cp 12 nodes 3941 nps 394100 time 10 multipv 1 pv d2d4 g8f6 b1c3 d7d5 g1f3 b8c6
info depth 7 seldepth 7 score cp 46 nodes 5356 nps 446333 time 12 multipv 1 pv d2d4 g8f6 b1c3 d7d5 g1f3 e7e6 e2e3
info nodes 5356 time 12
bestmove d2d4 ponder g8f6
quit

Links