Skip to main content

python-shogi: a pure Python shogi library

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 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())
    後手の持駒:
      9 8 7 6 5 4 3 2 1
    +---------------------------+
    |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': 'w'}
    >>> shogi.KIF.Exporter.kif(sfen_summary)
    開始日時: \r
    終了日時: \r
    手合割:平手\r
    先手:羽生善治\r
    後手:藤井猛\r
    手数----指手---------消費時間-- \r
    1 7六歩(77) \r
    2 3四歩(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

How to test

> make test

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

> poetry run nosetests -s

How to release

poetry config repositories.testpypi https://test.pypi.org/legacy/
# poetry config pypi-token.testpypi "Test PyPI API Token"
make test-upload
# poetry config pypi-token.pypi "PyPI API Token"
make upload

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().

Release files for python-shogi 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-shogi 1.1.1
File Size Uploaded
python_shogi-1.1.1.tar.gz 70.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-shogi 1.1.1
File Interpreter ABI Platform
python_shogi-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 141.2 kB

Release files / python_shogi-1.1.1.tar.gz

Download URL python_shogi-1.1.1.tar.gz
Size 70.5 kB
Tags Source
SHA-256 checksum
How to use checksums
c33a53ff9decba2d31f21be3ff313548552706739ee50827ea902045900aaf0b
BLAKE2b-256 checksum
How to use checksums
754e8ce69bf56ae03df008c04351cffac186b4b1b0723985ea76ea02f9a91c2b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.11.7 Darwin/23.2.0

Release files / python_shogi-1.1.1-py3-none-any.whl

Download URL python_shogi-1.1.1-py3-none-any.whl
Size 70.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c626671fa7330ac1c9a4673ea6023c94a3ca9eb1d87aeb9ab2740453773dc821
BLAKE2b-256 checksum
How to use checksums
63b9d1ed6dd2912617b7d716010b3eac7fc4bceee003260f24857ca53a9721ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.11.7 Darwin/23.2.0

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.16

1 release file

1.0.15

1 release file

1.0.14

1 release file

1.0.13

1 release file

1.0.12

1 release file

1.0.11

1 release file

1.0.10

1 release file

1.0.9

1 release file

1.0.8

1 release file

1.0.7

1 release file

1.0.6

1 release file

1.0.5

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.0.1

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page