Skip to content

A draughts (checkers) library for Python with move generation, PDN reading and writing, engine communication and balloted openings

License

Notifications You must be signed in to change notification settings

AttackingOrDefending/pydraughts

Repository files navigation

pydraughts

PyPI version Tests Build CodeQL codecov

pydraughts is a draughts (checkers) library for Python with move generation, PDN reading and writing, engine communication and balloted openings. It is based on ImparaAI/checkers.

Installing

Download and install the latest release:

pip install pydraughts

Features

Variants:

  • Standard (International)
  • Frisian
  • frysk!
  • Antidraughts
  • Breakthrough
  • Russian
  • Brazilian
  • English/American
  • Italian
  • Turkish

Engine protocols:

  • Hub
  • DXP
  • CheckerBoard

PDN Reading and Writing

  • Import pydraughts
from draughts import Board, Move, WHITE, BLACK
  • Create a game
board = Board(variant="standard", fen="startpos")
  • Make a move
move = Move(board, steps_move=[34, 30])
board.push(move)

# Multi-capture
board2 = Board(fen="W:WK40:B19,29")
board2.push(Move(board2, pdn_move='40x14'))
  • Get a visual representation of the board
print(board)

"""
   | b |   | b |   | b |   | b |   | b 
---------------------------------------
 b |   | b |   | b |   | b |   | b |   
---------------------------------------
   | b |   | b |   | b |   | b |   | b 
---------------------------------------
 b |   | b |   | b |   | b |   | b |   
---------------------------------------
   |   |   |   |   |   |   |   |   |   
---------------------------------------
   |   |   |   |   |   |   |   | w |   
---------------------------------------
   | w |   | w |   | w |   |   |   | w 
---------------------------------------
 w |   | w |   | w |   | w |   | w |   
---------------------------------------
   | w |   | w |   | w |   | w |   | w 
---------------------------------------
 w |   | w |   | w |   | w |   | w |   
"""
  • Get legal moves
moves = board.legal_moves()
  • Detect wins and draws
has_white_won = board.winner() == WHITE
is_draw = board.winner() == 0
winnner = board.winner()
is_game_over = board.is_over()
  • Convert move to other types
move = Move(board, board_move=moves[0].board_move).pdn_move
  • Get fen
fen = game.fen
  • Communicate with engines
from draughts.engine import HubEngine, Limit
engine = HubEngine(["scan.exe", "hub"])
engine.init()
limit = Limit(time=10)
engine_move = engine.play(board, limit, ponder=False)
  • Read PDN games
from draughts.PDN import PDNReader
games = PDNReader(filename=filepath)
game = games.games[0]
moves = game.moves
  • Write PDN games
from draughts.PDN import PDNWriter
games = PDNWriter(filename=filepath, board=board)
  • Get a ballot
from draughts.ballots import Ballots
ballots = Ballots('english')
ballot1 = ballots.get_ballot()
ballot2 = ballots.get_ballot()
  • Run tournaments
from draughts.tournament import RoundRobin
player1 = (["scan.exe", "hub"], "hub", {}, None)
player2 = ("kr_hub.exe", "hub", {}, None)
players = [player1, player2]
tournament = RoundRobin("tournament.pdn", players, start_time=20, increment=0.2, games_per_pair=4)
scores = tournament.play()
print(scores)
tournament.print_standings()

Acknowledgements

Thanks to fishnet which was modified to add support for Hub engines. Thanks to akalverboer for their DXC100_draughts_client which was modified to add support for DXP engines.

License

pydraughts is licensed under The MIT License. Check out LICENSE for the full text. The licenses of the other projects that pydraughts uses are in the other_licenses folder.

About

A draughts (checkers) library for Python with move generation, PDN reading and writing, engine communication and balloted openings

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages