Skip to main content

A pure Python shogi library with move generation and validation and handling of common formats.

Project description

https://travis-ci.org/gunyarakun/python-shogi.svg https://coveralls.io/repos/gunyarakun/python-shogi/badge.svg https://badge.fury.io/py/python-shogi.svg https://github.com/gunyarakun/python-shogi/actions/workflows/pythonpackage.yml/badge.svg https://github.com/gunyarakun/python-shogi/actions/workflows/codeql-analysis.yml/badge.svg

Introduction

This is the module for shogi written in Pure Python. It’s based on python-chess commit

This is the scholars mate in python-shogi:

>>> import shogi

>>> board = shogi.Board()

>>> board.push(shogi.Move.from_usi('7g7f'))

>>> board.push_usi('3c3d')
Move.from_usi('3c3d')
>>> board.push_usi('8h2b+')
Move.from_usi('8h2b+')
>>> board.push_usi('4a5b')
Move.from_usi('4a5b')
>>> board.push_usi('B*4b')
Move.from_usi('B*4b')
>>> board.push_usi('5a4a')
Move.from_usi('5a4a')
>>> board.push_usi('2b3a')
Move.from_usi('2b3a')
>>> board.is_checkmate()
True

Features

  • Supports Python 2.7 and Python 3.3+.

  • Supports standard shogi (hon shogi)

  • Legal move generator and move validation.

    >>> shogi.Move.from_usi("5i5a") in board.legal_moves
    False
  • Make and unmake moves.

    >>> last_move = board.pop() # Unmake last move
    >>> last_move
    Move.from_usi('2b3a')
    
    >>> board.push(last_move) # Restore
  • Show a simple ASCII board.

    >>> print(board)
     l  n  s  g  .  k +B  n  l
     .  r  .  .  g  B  .  .  .
     p  p  p  p  p  p  .  p  p
     .  .  .  .  .  .  p  .  .
     .  .  .  .  .  .  .  .  .
     .  .  P  .  .  .  .  .  .
     P  P  .  P  P  P  P  P  P
     .  .  .  .  .  .  .  R  .
     L  N  S  G  K  G  S  N  L
    <BLANKLINE>
     S*1
  • Show a KIF style board.

    >>> print(board.kif_str())
    後手の持駒
              
    +---------------------------+
    |v香v桂v銀v金 v玉 馬v桂v香|
    | v飛  v金    |
    |v歩v歩v歩v歩v歩v歩 v歩v歩|
    |      v歩  |
    |         |
    |         |
    |         |
    |         |
    |         |
    +---------------------------+
    先手の持駒 
  • Detects checkmates, stalemates.

    >>> board.is_stalemate()
    False
    >>> board.is_game_over()
    True
  • Detects repetitions. Has a half move clock.

    >>> board.is_fourfold_repetition()
    False
    >>> board.move_number
    8
  • Detects checks and attacks.

    >>> board.is_check()
    True
    >>> board.is_attacked_by(shogi.BLACK, shogi.A4)
    True
    >>> attackers = board.attackers(shogi.BLACK, shogi.H5)
    >>> attackers
    SquareSet(0b111000010000000000000000000000000000000000000000000000000000000000000000000000)
    >>> shogi.H2 in attackers
    True
    >>> print(attackers)
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . 1 .
    . . . 1 1 1 . . .
  • Parses and creates USI representation of moves.

    >>> board = shogi.Board()
    >>> shogi.Move(shogi.E2, shogi.E4).usi()
    '2e4e'
  • Parses and creates SFENs

    >>> board.sfen()
    'lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b - 1'
    >>> board.piece_at(shogi.I5)
    Piece.from_symbol('K')
  • Read KIFs.

    >>> import shogi.KIF
    
    >>> kif = shogi.KIF.Parser.parse_file('data/games/habu-fujii-2006.kif')[0]
    
    >>> kif['names'][shogi.BLACK]
    '羽生善治'
    >>> kif['names'][shogi.WHITE]
    '藤井猛'
    >>> kif['moves'] # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
    ['7g7f',
     '3c3d',
     ...,
     '9a9b',
     '7a7b+']
    >>> kif['win']
    'b'
  • Export to KIFs.

    >>> import shogi
    >>> import shogi.KIF
    
    >>> board = shogi.Board()
    >>> shogi.KIF.Exporter.kif_move_from('7g7f', board)
    '7六歩(77)'
    
    >>> sfen_summary = {'moves': ['7g7f', '3c3d'], 'sfen': 'lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b - 1', 'names': ['羽生善治', '藤井猛'], 'win': 'b'}
    >>> shogi.KIF.Exporter.kif(sfen_summary)
    開始日時 \r
    終了日時 \r
    手合割平手\r
    先手羽生善治\r
    後手藤井猛\r
    手数----指手---------消費時間-- \r
    1 六歩(77) \r
    2 四歩(33) \r
    3 投了 \r
    まで2手で後手の勝ち\r
  • Communicate with a CSA protocol.

    Please see random_csa_tcp_match.

  • Parse professional shogi players’ name

    >>> import shogi.Person
    
    >>> shogi.Person.Name.is_professional('羽生 善治 名人・棋聖・王位・王座')
    True
    

Performance

python-shogi is not intended to be used by serious shogi engines where performance is critical. The goal is rather to create a simple and relatively highlevel library.

You can install the gmpy2 or gmpy modules in order to get a slight performance boost on basic operations like bit scans and population counts.

python-shogi will only ever import very basic general (non-shogi-related) operations from native libraries. All logic is pure Python. There will always be pure Python fallbacks.

Installing

  • With pip:

    pip install python-shogi
  • From current source code:

    python setup.py sdist
    sudo python setup.py install

How to test

> nosetests
or
> python setup.py test # requires python setup.py install

If you want to print lines from the standard output, execute nosetests like following.

> nosetests -s

If you want to test among different Python versions, execute tox.

> pip install tox
> tox

How to release

rm -rf dist
python setup.py sdist
twine upload dist/*

ToDo

  • Support board.generate_attacks() and use it in board.is_attacked_by() and board.attacker_mask().

  • Remove rotated bitboards and support Shatranj-style direct lookup like recent python-chess.

  • Support %MATTA etc. in CSA TCP Protocol.

  • Support board.is_pinned() and board.pin().

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-shogi-1.0.16.tar.gz (67.9 kB view details)

Uploaded Source

File details

Details for the file python-shogi-1.0.16.tar.gz.

File metadata

  • Download URL: python-shogi-1.0.16.tar.gz
  • Upload date:
  • Size: 67.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for python-shogi-1.0.16.tar.gz
Algorithm Hash digest
SHA256 62aa0ce3fb1a45decc91e7637333e3513d9b08003be266610811434fe4fd55db
MD5 4c3fa9574ded6414d75e4e765313195b
BLAKE2b-256 079ea5628f0acfb81766f4e817176bac6f2cb6f689916afbc14c04c533e69551

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page