tinycodinglm / chess /chess-0104.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 12)
3c88a0f verified
|
Raw History Blame Contribute Delete
2.4 kB
opencode trace
System You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing c...

You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies.

User

How to use Stockfish engine with python-chess

Assistant

Stockfish with python-chess:

Installation:

bash
pip install chess
# Download Stockfish from https://stockfishchess.org/download/

Basic Engine Usage:

python
import chess
import chess.engine

# Start engine
engine = chess.engine.SimpleEngine.popen_uci("stockfish")

# Get best move
board = chess.Board()
result = engine.play(board, chess.engine.Limit(time=1.0))
print(result.move)  # e2e4

# Analyze position
analysis = engine.analyse(board, chess.engine.Limit(depth=20))
print(analysis["score"])  # Cp(+20)
print(analysis["pv"])     # principal variation

engine.quit()

Multi-PV Analysis:

python
# Get top 3 lines
analysis = engine.analyse(board, chess.engine.Limit(depth=20), multipv=3)
for info in analysis:
    print(info["score"], info["pv"][0])

Custom Engine Options:

python
engine = chess.engine.SimpleEngine.popen_uci("stockfish")
engine.configure({
    "Threads": 4,
    "Hash": 256,
    "Skill Level": 20
})

Analyze Game:

python
import chess.pgn

def analyze_game(pgn_path):
    with open(pgn_path) as f:
        game = chess.pgn.read_game(f)
    
    board = game.board()
    engine = chess.engine.SimpleEngine.popen_uci("stockfish")
    
    for move in game.mainline_moves():
        board.push(move)
        analysis = engine.analyse(board, chess.engine.Limit(depth=18))
        score = analysis["score"].relative.score(mate_score=10000)
        print(f"{move}: {score/100:.2f}")
    
    engine.quit()

UCI Protocol:

markdown
uci
isready
position startpos moves e2e4 e7e5
go depth 20
bestmove e2e4